在 PHP 應用開發中,往往需要驗證用戶郵箱、發送消息通知,而使用 PHP 內置的 mail() 函數,則需要郵件系統的支持。
如果熟悉 IMAP/SMTP 協議,結合 Socket 功能就可以編寫郵件發送程序了,不過開發這樣一個程序並不容易。
好在 PHPMailer 封裝的足夠強大,使用它可以更加便捷的發送郵件,免去了我們很多額外的麻煩。
PHPMailer
PHPMailer 是一個封裝好的 PHP 郵件發送類,支持發送 HTML 內容的電子郵件,以及可以添加附件發送,並不像 PHP 本身 mail() 函數需要服務器環境支持,您只需要設置郵件服務器以相關信息就能實現郵件發送功能。
PHPMailer 項目地址:https://github.com/PHPMailer/PHPMailer
PHP擴展支持
PHPMailer 需要 PHP 的 sockets 擴展支持,而登錄 QQ 郵箱 SMTP 服務器則必須通過 SSL 加密,故 PHP 還得包含 openssl 的支持。
↑ 使用 phpinfo() 函數查看 socket 和 openssl 擴展信息(wamp server 默認啟用了該擴展)。
PHPMailer 核心文件
↑ 在本文中只需要 class.phpmailer.php 和 PHPMailer/class.smtp.php。
QQ 郵箱設置
所有的主流郵箱都支持 SMTP 協議,但並非所有郵箱都默認開啟,您可以在郵箱的設置里面手動開啟。
第三方服務在提供了賬號和密碼之后就可以登錄 SMTP 服務器,通過它來控制郵件的中轉方式。
開啟 SMTP 服務
↑ 選擇 IMAP/SMTP 服務,點擊開啟服務。
驗證密保
↑ 發送短信“配置郵件客戶端”至1069-0700-69。
獲取授權碼
↑ SMTP 服務器認證密碼,需要妥善保管(PS:密碼直接沒有空格)。
PHP發送郵件
基本代碼
下面的代碼演示了 PHPMailer 的使用方法,注意 PHPMailer 實例的配置過程。
// 引入PHPMailer的核心文件 require_once("PHPMailer/class.phpmailer.php"); require_once("PHPMailer/class.smtp.php"); // 實例化PHPMailer核心類 $mail = new PHPMailer(); // 是否啟用smtp的debug進行調試 開發環境建議開啟 生產環境注釋掉即可 默認關閉debug調試模式 $mail->SMTPDebug = 1; // 使用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'; // 設置發件人昵稱 顯示在收件人郵件的發件人郵箱地址前的發件人姓名 $mail->FromName = '發件人昵稱'; // smtp登錄的賬號 QQ郵箱即可 $mail->Username = '12345678@qq.com'; // smtp登錄的密碼 使用生成的授權碼 $mail->Password = '**********'; // 設置發件人郵箱地址 同登錄賬號 $mail->From = '12345678@qq.com'; // 郵件正文是否為html編碼 注意此處是一個方法 $mail->isHTML(true); // 設置收件人郵箱地址 $mail->addAddress('87654321@qq.com'); // 添加多個收件人 則多次調用方法即可 $mail->addAddress('87654321@163.com'); // 添加該郵件的主題 $mail->Subject = '郵件主題'; // 添加郵件正文 $mail->Body = '<h1>Hello World</h1>'; // 為該郵件添加附件 $mail->addAttachment('./example.pdf'); // 發送郵件 返回狀態 $status = $mail->send();
封裝方法
如果要直接使用 PHPMailer 發送郵件,則需要進行繁瑣的配置,這樣做多少會降低效率。
為了簡化調用過程,我在其基礎上進行了二次封裝,只需要配置賬號、密碼和昵稱,就可以定制你自己的 QQMailer 類了。

<?php require_once 'PHPMailer/class.phpmailer.php'; require_once 'PHPMailer/class.smtp.php'; class QQMailer { public static $HOST = 'smtp.qq.com'; // QQ 郵箱的服務器地址 public static $PORT = 465; // smtp 服務器的遠程服務器端口號 public static $SMTP = 'ssl'; // 使用 ssl 加密方式登錄 public static $CHARSET = 'UTF-8'; // 設置發送的郵件的編碼 private static $USERNAME = '123456789@qq.com'; // 授權登錄的賬號 private static $PASSWORD = '****************'; // 授權登錄的密碼 private static $NICKNAME = 'woider'; // 發件人的昵稱 /** * QQMailer constructor. * @param bool $debug [調試模式] */ public function __construct($debug = false) { $this->mailer = new PHPMailer(); $this->mailer->SMTPDebug = $debug ? 1 : 0; $this->mailer->isSMTP(); // 使用 SMTP 方式發送郵件 } /** * @return PHPMailer */ public function getMailer() { return $this->mailer; } private function loadConfig() { /* Server Settings */ $this->mailer->SMTPAuth = true; // 開啟 SMTP 認證 $this->mailer->Host = self::$HOST; // SMTP 服務器地址 $this->mailer->Port = self::$PORT; // 遠程服務器端口號 $this->mailer->SMTPSecure = self::$SMTP; // 登錄認證方式 /* Account Settings */ $this->mailer->Username = self::$USERNAME; // SMTP 登錄賬號 $this->mailer->Password = self::$PASSWORD; // SMTP 登錄密碼 $this->mailer->From = self::$USERNAME; // 發件人郵箱地址 $this->mailer->FromName = self::$NICKNAME; // 發件人昵稱(任意內容) /* Content Setting */ $this->mailer->isHTML(true); // 郵件正文是否為 HTML $this->mailer->CharSet = self::$CHARSET; // 發送的郵件的編碼 } /** * Add attachment * @param $path [附件路徑] */ public function addFile($path) { $this->mailer->addAttachment($path); } /** * Send Email * @param $email [收件人] * @param $title [主題] * @param $content [正文] * @return bool [發送狀態] */ public function send($email, $title, $content) { $this->loadConfig(); $this->mailer->addAddress($email); // 收件人郵箱 $this->mailer->Subject = $title; // 郵件主題 $this->mailer->Body = $content; // 郵件信息 return (bool)$this->mailer->send(); // 發送郵件 } }
require_once 'QQMailer.php'; // 實例化 QQMailer $mailer = new QQMailer(true); // 添加附件 $mailer->addFile('20130VL.jpg'); // 郵件標題 $title = '願得一人心,白首不相離。'; // 郵件內容 $content = <<< EOF <p align="center"> 皚如山上雪,皎若雲間月。<br> 聞君有兩意,故來相決絕。<br> 今日斗酒會,明旦溝水頭。<br> 躞蹀御溝上,溝水東西流。<br> 凄凄復凄凄,嫁娶不須啼。<br> 願得一人心,白首不相離。<br> 竹竿何裊裊,魚尾何簁簁!<br> 男兒重意氣,何用錢刀為!</p> EOF; // 發送QQ郵件 $mailer->send('123456789@qq.com', $title, $content);