小編給大家分享一下ThinkPHP5郵件發送服務封裝的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體內容如下
1.Composer安裝phpmailer
composer require phpmailer/phpmailer
2.ThinkPHP中封裝郵件服務類
我把它封裝在擴展目錄 extend/Mail.php 文件里,內容如下:
<?php
/**
* 郵件服務類
*/
class Mail extends \PHPMailer
{
function __construct()
{
date_default_timezone_set('PRC'); // 默認時區設置
$this->CharSet = config('mail.charset'); // 郵件編碼設置
$this->isSMTP(); // 啟用SMTP服務
$this->SMTPDebug = config('mail.smtp_debug'); // Debug模式級別
$this->Debugoutput = config('mail.debug_output'); // Debug輸出類型
$this->Host = config('mail.host'); // SMTP服務器地址
$this->Port = config('mail.port'); // 端口號
$this->SMTPAuth = config('mail.smtp_auth'); // SMTP登錄認證
$this->SMTPSecure = config('mail.smtp_secure'); // SMTP安全協議
$this->Username = config('mail.username'); // SMTP登錄郵箱
$this->Password = config('mail.password'); // SMTP登錄密碼
$this->setFrom(config('mail.from'), config('mail.from_name')); // 發件人郵箱和名稱
$this->addReplyTo(config('mail.reply_to'), config('mail.reply_to_name')); // 回復郵箱和名稱
}
/**
* 發送郵件
* @param [type] $toMail 收件人地址
* @param [type] $toName 收件人名稱
* @param [type] $subject 郵件主題
* @param [type] $content 郵件內容,支持html
* @param [type] $attachment 附件列表。文件路徑或路徑數組
* @return [type] 成功返回true,失敗返回錯誤消息
*/
function sendMail($toMail, $toName, $subject, $content, $attachment = null)
{
$this->addAddress($toMail, $toName);
$this->Subject = $subject;
$this->msgHTML($content);
if($attachment) { // 添加附件
if(is_string($attachment)){
is_file($attachment) && $this->AddAttachment($attachment);
}
else if(is_array($attachment)){
foreach ($attachment as $file) {
is_file($file) && $this->AddAttachment($file);
}
}
}
if(!$this->send()){ // 發送
return $this->ErrorInfo;
}
else{
return true;
}
}
}注意:如果發送附件,建議使用英文路徑。中文路徑可能會導致附件發送失敗,收到的郵件沒有附件。
上面需要的一些配置參數,我把它們放在擴展配置目錄 application/extra/mail.php 文件里 ,內容如下:
<?php /** * 郵件服務相關配置 */ return [ 'charset' => 'utf-8', // 郵件編碼 'smtp_debug' => 0, // Debug模式。0: 關閉,1: 客戶端消息,2: 客戶端和服務器消息,3: 2和連接狀態,4: 更詳細 'debug_output' => 'html', // Debug輸出類型。`echo`(默認),`html`,或`error_log` 'host' => 'smtp.126.com', // SMTP服務器地址 'port' => 465, // 端口號。默認25 'smtp_auth' => true, // 啟用SMTP認證 'smtp_secure' => 'ssl', // 啟用安全協議。''(默認),'ssl'或'tls',留空不啟用 'username' => 'yourname@example.com', // SMTP登錄郵箱 'password' => 'yourpassword', // SMTP登錄密碼。126郵箱使用客戶端授權碼,QQ郵箱用獨立密碼 'from' => 'from@example.com', // 發件人郵箱 'from_name' => 'name', // 發件人名稱 'reply_to' => '', // 回復郵箱的地址。留空取發件人郵箱 'reply_to_name' => '', // 回復郵箱人名稱。留空取發件人名稱 ];
注意:一般默認端口 25。如果使用了安全協議 ssl,那么端口號一般是 465 或 587。譬如 126 郵箱。建議使用安全協議,因為像阿里云服務器就禁止了非安全協議的 25 端口。
更多配置參數,可以看看源碼:https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php
3.測試
在控制器里方法里,添加測試代碼:
public function mail()
{
$mail = new \Mail;
$ok = $mail->sendMail('xxxxxxxxx@qq.com', 'mingc', '郵件來了', '<p >恭喜,郵件成功!</p>', 'C:/Users/Administrator/Desktop/body.bmp');
var_dump($ok);
}
這里我使用 126 郵箱,安全協議 ssl,端口號 465,發送 html 內容,測試成功:

看完了這篇文章,相信你對“ThinkPHP5郵件發送服務封裝的示例分析”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。