PHPMailer发送邮件现在php开发者比较常用的一个邮件发送组件了,利用它我们几乎不需要考虑任何问题,只要简单的把代码放网上把邮箱用户名密码与stmp改一下就可以发邮件了.
PHPMailer是别人封装好的一个发送邮件的库,用起来很方便,其支持mail、sendmail和smtp的方式可以到https://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list去下载最新版本的,下面通过gmail smtp发送邮件为例来说明smtp使用方法.
<?php function sendMail($subject, $body, $to, $ccs = array()) { require_once './class.phpmailer.php'; $mail = new PHPMailer(); //设定邮件编码,默认ISO-8859-1,也可以直接去源代码中修改 $mail->CharSet = 'UTF-8'; // 使用smtp的方式发送 $mail->IsSMTP(); //smtp服务器需要认证 $mail->SMTPAuth = TRUE; //安全协议 gmail 是采用ssl的 $mail->SMTPSecure = "ssl"; //smtp服务器 $mail->Host = 'smtp.gmail.com'; //smtp服务器端口,普通是25 $mail->Port = 465; //smtp 认证用户名和密码 $mail->Username = 'yourgmailaccount@gmail.com'; $mail->Password = "yourpassword"; //发件人地址和名字,名字可以省略 $mail->SetFrom('yourgmailaccount@gmail.com', 'display name'); // 邮件标题 $mail->Subject = $subject; // 邮件内容,支持HTML格式 $mail->MsgHTML($body); // 收件人地址 $mail->AddAddress($to); // 抄送人 foreach ($ccs as $cc) { $mail->AddCC($cc); } if (!$mail->Send()) { echo "error info:" . $mail->ErrorInfo; } } ?>
上面是核心代码,下面我们综合一下实例,按如下示例编写代码即可实现php在线发送邮件.
一:前台表单,代码如下:
<html> <body> <h3 id="phpmailer-nbsp-Unit-nbsp-Test">phpmailer Unit Test</h3> 请你输入<font color="#FF6666">收信</font>的邮箱地址: <form name="phpmailer" action="send.php" method="post"> <input type="hidden" name="submitted" value="1"/> 邮箱地址: <input type="text" size="50" name="address" /> <br/> <input type="submit" value="发送"/> </form> </body> </html>
二:后台PHP程序,代码如下:
<?php require ("class.phpmailer.php"); //下载的文件必须放在该文件所在目录 $mail = new PHPMailer(); //建立邮件发送类 $address = $_POST['address']; $mail->IsSMTP(); // 使用SMTP方式发送 $mail->Host = "mail.xxxxx.com"; // 您的企业邮局域名 $mail->SMTPAuth = true; // 启用SMTP验证功能 $mail->Username = "user@xxxx.com"; // 邮局用户名(请填写完整的email地址) $mail->Password = "******"; // 邮局密码 $mail->From = "user@xxxx.com"; //邮件发送者email地址 $mail->FromName = "您的名称"; $mail->AddAddress("$address", ""); //收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名") //$mail->AddReplyTo("", ""); //$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件 //$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式 $mail->Subject = "PHPMailer测试邮件"; //邮件标题 $mail->Body = "Hello,这是测试邮件"; //邮件内容 $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略 if (!$mail->Send()) { echo "邮件发送失败. <p>"; echo "错误原因: " . $mail->ErrorInfo; exit; } echo "邮件发送成功"; ?>
永久地址:
转载随意~请带上教程地址吧^^

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment