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;
        }
    }
}

大功告成,使用時直接調用。
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章