本文實例為大家分享了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 style="color: #f60; font-weight: 700;">恭喜,郵件成功!/p>', 'C:/Users/Administrator/Desktop/body.bmp');
var_dump($ok);
}
這里我使用 126 郵箱,安全協議 ssl,端口號 465,發送 html 內容,測試成功:

參考鏈接:phpmail 的 SMTP 郵件實例
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- PHP使用gearman進行異步的郵件或短信發送操作詳解
- PHP發送郵件確認驗證注冊功能示例【修改別人郵件類】
- ThinkPHP3.2.3框架郵件發送功能圖文實例詳解
- PHP示例演示發送郵件給某個郵箱
- php判斷電子郵件是否正確方法
- PHP實現SMTP郵件的發送實例
- PHP使用SMTP郵件服務器發送郵件示例
- 實例分析PHP中PHPMailer發郵件
- ThinkPHP3.2利用QQ郵箱/163郵箱通過PHPMailer發送郵件的方法
- 詳解thinkphp5+swoole實現異步郵件群發(SMTP方式)
- PHP使用POP3讀取郵箱接收郵件的示例代碼