PHPMailer

下载并解压

PHPMailer项目地址:https://github.com/PHPMailer/PHPMailer

打开扩展

PHPMailer 需要 PHP 的 sockets 扩展支持,而登录 QQ 邮箱 SMTP 服务器则必须通过 SSL 加密,故 PHP 还得包含 openssl 的支持。

QQ邮箱设置

1.开启SMTP
这里写图片描述
2.读取授权码密码
这里写图片描述
3.SMTP服务器和端口
qq邮箱smtp服务器:smtp.qq.com
SSL启用端口:587/465
更多请参考:https://www.cnblogs.com/grefr/p/6089079.html

配置:

写了一个脚本测试,代码如下:

<?php
// 引入PHPMailer的核心文件
require 'class.phpmailer.php';
require 'class.smtp.php';
//设置时间
date_default_timezone_set('PRC');

// 实例化PHPMailer核心类
$mail = new PHPMailer;

//调试
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
// 邮件正文为html编码
$mail->Debugoutput = 'html';
// 使用smtp鉴权方式发送邮件
$mail->isSMTP();
// smtp需要鉴权 这个必须是true
$mail->SMTPAuth = true;
// 链接qq域名邮箱的服务器地址
$mail->Host = "smtp.qq.com";
// 设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl';
// 设置ssl连接smtp服务器的远程服务器端口号
$mail->Port = 465;
// 设置发送的邮件的编码
$mail->CharSet = 'UTF-8';
// smtp登录的账号 QQ邮箱即可
$mail->Username = "yun-***@qq.com";
// smtp登录的密码 使用生成的授权码
$mail->Password = "*****";
// 设置发件人邮箱地址 同登录账号
$mail->setFrom('yun-***@qq.com', 'First Last');
// 设置收件人邮箱地址
$mail->addAddress('yun_***@163.com', 'John Doe');
// 添加该邮件的主题
$mail->Subject = 'PHPMailer SMTP test';
// 添加邮件正文
$mail->msgHTML('hello word');

// 发送邮件 返回状态
if  (!$mail->send())  {
    echo "Mailer Error: " . $mail->ErrorInfo;
}  else  {
    echo "Message sent success!";
}

测试后部署到项目(ThinkPHP5框架)

1.phpmailer文件添加到项目下的extend下
2.依照TP5的规范修改文件名,如下:
这里写图片描述
3.Phpmailer.php和Smtp.php添加命名空间
这里写图片描述
注意:Phpmailer.php中修改:
这里写图片描述
4.新建Email发送邮件类

<?php
/**
 * 封装phpmailer发送邮件类
 */
namespace phpmailer;
class Email
{
    /**
     * @param string $to 收件人邮箱
     * @param string $subject 标题
     * @param string $content 邮箱内容
     * @return bool
     */
    public static function send($to, $subject, $content)
    {
        if (empty($to)) {
            return false;
        }
        date_default_timezone_set('PRC');
        try{ // 抛异常
            //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";
            // 设置使用ssl加密方式登录鉴权
            $mail->SMTPSecure = 'ssl';
            //Set the SMTP port number - likely to be 25, 465 or 587
            $mail->Port = 465;
            //Whether to use SMTP authentication
            $mail->SMTPAuth = true;
            //Username to use for SMTP authentication
            $mail->Username = "yun-***@qq.com";
            //Password to use for SMTP authentication
            $mail->Password = "****";
            //Set who the message is to be sent from
            $mail->setFrom('yun-***@qq.com');
            //Set who the message is to be sent to
            $mail->addAddress($to);
            //Set the subject line
            $mail->Subject = $subject;
            //convert HTML into a basic plain-text alternative body
            $mail->msgHTML($content);
            //send the message, check for errors
            if  (!$mail->send())  {
                return false;
                //echo "Mailer Error: " . $mail->ErrorInfo;
            }  else  {
                return true;
                //echo "Message sent success!";
            }
        }
        catch (phpmailerException $e)
        {
            return false;
        }
    }
}

大功告成,使用时直接调用。
这里写图片描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值