1. PHPMailer 介紹
phpMailer 是一個非常強(qiáng)大的 php 發(fā)送郵件擴(kuò)展包,可以設(shè)定發(fā)送郵件地址、回復(fù)地址、郵件主題、html郵件內(nèi)容和上傳附件等,使用起來非常方便。它目前有著有近 4 千萬的下載量,是 PHP 開發(fā)者實現(xiàn)郵件發(fā)送功能的首選擴(kuò)展包
它對 PHP 版本的要求也很低,只要 PHP 版本大于等于 5.5 就能使用,是一個非常優(yōu)秀的郵件發(fā)送擴(kuò)展包
Packagist 傳送地址 : https://packagist.org/packages/phpmailer/phpmailer
composer require phpmailer/phpmailer
補(bǔ)充:下面這個擴(kuò)展包也能發(fā)送郵件,不過我還沒有測試
composer require sendgrid/sendgrid
2. 發(fā)送郵件代碼示例
下面是發(fā)送郵件比較完整的代碼,以 QQ郵箱作為 SMTP 服務(wù)器發(fā)送郵件
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
try {
$mail = new PHPMailer(true);
//設(shè)定郵件編碼,默認(rèn)ISO-8859-1,如果發(fā)中文此項必須設(shè)置,否則亂碼
$mail->CharSet = 'UTF-8';
# 服務(wù)器設(shè)置
//啟用詳細(xì)調(diào)試輸出
//$mail->SMTPDebug = SMTP::DEBUG_SERVER;
// 使用SMTP發(fā)送
$mail->isSMTP();
//要發(fā)送的SMTP服務(wù)器
$mail->Host = 'smtp.qq.com';
//啟用SMTP身份驗證
$mail->SMTPAuth = true;
//SMTP用戶名
$mail->Username = '23426945@qq.com';
//SMTP密碼
$mail->Password = 'mftcnkcrrcixxxxx';
//啟用隱式TLS加密
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
//要連接到的TCP端口
$mail->Port = 465;
#發(fā)件人信息
$mail->setFrom('23426945@qq.com', '又拍云');
#收件人列表,可將郵件發(fā)送給多個郵箱,命令格式:
//$mail->addAddress('收件人郵箱', '姓名');
$mail->addAddress('848978691@qq.com', '李知恩');
$mail->addAddress('3276205785@qq.com', '灰太狼的羊');
#回復(fù)地址
$mail->addReplyTo('23426945@qq.com', '李鐘碩');
#抄送人列表
$mail->addCC('itqaqcom@163.com', '你的欲夢');
# 郵件內(nèi)容
//電子郵件格式設(shè)置為HTML
$mail->isHTML(true);
//郵件標(biāo)題
$mail->Subject = '又拍云密碼重置校驗';
//郵件內(nèi)容
$code = mt_rand(100000, 999999);
$mail->Body = "您的驗證碼為: <b>{$code}</b>";
//這個是設(shè)置純文本方式顯示的正文內(nèi)容,如果不支持Html方式,就會用到這個,基本無用
$mail->AltBody = "您的驗證碼為: {$code}";
#發(fā)送郵件
$mail->send();
echo '郵件發(fā)送成功';
} catch (\Exception $e) {
// $e->getMessage() // 異常信息
// $mail->ErrorInfo // 郵件發(fā)送失敗錯誤信息
echo "郵件發(fā)送失敗: {$mail->ErrorInfo}";
}
3. 郵件內(nèi)容樣式模板
郵件的內(nèi)容一般都不是純文本,會具有一定的樣式,如下圖所示,那么具有內(nèi)容的樣式應(yīng)該怎么來做呢 ?
發(fā)送郵件時,郵件內(nèi)容支持設(shè)置帶有 HTML 標(biāo)簽,我們只需要傳入帶有樣式的 HTML 內(nèi)容
可以提前編寫好樣式,最后壓縮為一行,部分內(nèi)容作為變量(如: 驗證碼),然后作為郵件內(nèi)容發(fā)送
在線壓縮 HTML 代碼 : https://tool.lu/html
<!-- 編寫的樣式 -->
<style>
.box {
text-align: center;
border: 1px solid red;
padding: 15px 0;
width: 450px;
margin: 0 auto;
}
.title {
color: red;
font-weight: bold;
text-align: center;
margin-bottom: 10px;
}
</style>
<div class="box">
<div class="title">密碼重置</div>
<div class="code">驗證碼: 123456</div>
</div>
<!-- 壓縮后 -->
<style>.box{text-align:center;border:1px solid red;padding:15px 0;width:450px;margin:0 auto}.title{color:red;font-weight:700;text-align:center;margin-bottom:10px}</style><div class="box"><div class="title">密碼重置</div><div class="code">驗證碼: 123456</div></div>
當(dāng)我們不想自己編寫郵件內(nèi)容模板樣式時,可以扒取其他平臺的郵件模板
扒取又拍云郵件模板舉例 : 我要做一個通過郵件找回密碼的功能,又不想自己寫這個郵件模板樣式,于是我找到 又拍云 的重置密碼郵件模板,扒取下來,在它的基礎(chǔ)上進(jìn)行修改,省時省力,何樂而不為
在又拍云平臺重置密碼,得到一個重置密碼的郵件,在控制臺查看元素可發(fā)現(xiàn):郵件內(nèi)容為下圖所標(biāo)記的部分,將元素復(fù)制出來后進(jìn)行修改即可
4. 驗證碼郵件模板
又拍云驗證碼模板
<div style="padding: 66px 0; width: 100%; background-color: #ededed; color: #777;" align="center">
<div style="width: 700px; background-color: #fff; text-align: left;margin: 0 auto;padding: 20px 0;">
<div style="margin: 30px 60px; width:580px; ">
<strong>親愛的 duxiu,</strong>
<p style="font-size: 14px; color: #777; line-height: 26px;">您正在通過郵件找回又拍云密碼。</p>
<p style="font-size: 14px; color: #777; line-height: 26px;">請在重置密碼的頁面中輸入以下 <strong>驗證碼</strong>
和新的密碼,完成密碼重設(shè):</p>
<p
style="font-size: 20px; color: #777; line-height: 50px; border: 1px solid #24b0cf; background: #fafafa; text-align: center;">
<span style="color: #24b0cf; font-weight: bold;">657475</span>
</p>
<p style="font-size: 14px; color: #aaa; line-height: 16px;">(此驗證碼有效時間為 60 分鐘,若超時請重新獲取郵件) </p>
<p style="font-size: 14px; color: #777; line-height: 26px;">如果您要放棄重設(shè)密碼,或者未曾申請密碼重設(shè),請忽略此郵件。</p>
<p style="font-size: 14px; color: #777; line-height: 26px;">
<strong>為了您的賬戶安全,請您注意對此郵件內(nèi)容保密。</strong>
</p>
</div>
</div>
</div>