PHPMailer 发送邮件
如果你的PHP版本是5.5以上, 请查看 https://blog.csdn.net/yule117737767/article/details/98599314
本篇内容支持PHP 5.0 - 7.0版本发送邮件.
首先下载PHPMailer 5.2版本分支, https://github.com/PHPMailer/PHPMailer/tree/5.2-stable
PHPMailer 需要开启 socket 和 openssl 扩展, 不会开启的请查看上一篇文章
依然使用SMTP来发送邮件, 使用smtp.qq.com, 具体开启操作查看上一篇文章
下载的PHPMailer 为5.2版, 需要PHP5.3版本以上
里面有一个examples例子, 这里面例子非常齐全 .主页是: ./PHPMailer-5.2-stable/examples/index.html
里面包含各种例子, 最后浏览器带翻译插件, 直接在浏览器翻译成中文
每一个里面都有实例. 可以自己研究, 最简单的就是 查看smtp.phps,
From_Name | 名称 |
From_Email | 电子邮件地址 |
To_Name | 收件人姓名 |
To_Email | 收件人电子邮件地址 |
CC | 抄送 |
BCC | 密抄送 |
Message /Subject | 内容 / 标题 |
Test Type | 邮件类型 SMTP |
SMTP Debug | 启用SMTP调试 0,不显示 1,客户端 2,客户端和服务器 |
SMTP Server | 设置邮件服务器 |
SMTP Port | SMTP端口号 |
SMTP Security | 设置登录鉴权 SSL |
SMTP Authenticate | 是否使用SMTP身份验证, 1,0 |
Authenticate Username | SMTP身份验证的账号 |
Authenticate Password | SMTP身份验证的密码 |
SMTP发送邮件例子
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';//引入加载
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "smtp.qq.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 465;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
// 设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl';
//Username to use for SMTP authentication
$mail->Username = "1177@qq.com";
//Password to use for SMTP authentication
$mail->Password = "qqclxpjpuyjhbjdg";
//Set who the message is to be sent from
$mail->setFrom('1177@qq.com', 'admin');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last2');
//Set who the message is to be sent to
$mail->addAddress('376064028@qq.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';//标题
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));//内容
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');//附件
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
这个版本的PHPMailer 存在发送中文部分邮箱不兼容问题. 我同时发送给2个邮箱, qq邮箱显示中文正常,而另一个公司邮箱显示乱码, 网上找的方式试过, 都无效, 如有解决方法的, 可以留个言,
其它的跟.6.6版本没区别