本文實例為大家分享了ThinkPHP5封裝郵件發(fā)送服務(wù)的具體代碼,供大家參考,具體內(nèi)容如下
1.Composer安裝phpmailer
composer require phpmailer/phpmailer
2.ThinkPHP中封裝郵件服務(wù)類
我把它封裝在擴展目錄 extend/Mail.php 文件里,內(nèi)容如下:
<?php /** * 郵件服務(wù)類 */ class Mail extends \PHPMailer { function __construct() { date_default_timezone_set('PRC'); // 默認時區(qū)設(shè)置 $this->CharSet = config('mail.charset'); // 郵件編碼設(shè)置 $this->isSMTP(); // 啟用SMTP服務(wù) $this->SMTPDebug = config('mail.smtp_debug'); // Debug模式級別 $this->Debugoutput = config('mail.debug_output'); // Debug輸出類型 $this->Host = config('mail.host'); // SMTP服務(wù)器地址 $this->Port = config('mail.port'); // 端口號 $this->SMTPAuth = config('mail.smtp_auth'); // SMTP登錄認證 $this->SMTPSecure = config('mail.smtp_secure'); // SMTP安全協(xié)議 $this->Username = config('mail.username'); // SMTP登錄郵箱 $this->Password = config('mail.password'); // SMTP登錄密碼 $this->setFrom(config('mail.from'), config('mail.from_name')); // 發(fā)件人郵箱和名稱 $this->addReplyTo(config('mail.reply_to'), config('mail.reply_to_name')); // 回復(fù)郵箱和名稱 } /** * 發(fā)送郵件 * @param [type] $toMail 收件人地址 * @param [type] $toName 收件人名稱 * @param [type] $subject 郵件主題 * @param [type] $content 郵件內(nèi)容,支持html * @param [type] $attachment 附件列表。文件路徑或路徑數(shù)組 * @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()){ // 發(fā)送 return $this->ErrorInfo; } else{ return true; } } }
注意:如果發(fā)送附件,建議使用英文路徑。中文路徑可能會導(dǎo)致附件發(fā)送失敗,收到的郵件沒有附件。
上面需要的一些配置參數(shù),我把它們放在擴展配置目錄 application/extra/mail.php 文件里 ,內(nèi)容如下:
<?php /** * 郵件服務(wù)相關(guān)配置 */ return [ 'charset' => 'utf-8', // 郵件編碼 'smtp_debug' => 0, // Debug模式。0: 關(guān)閉,1: 客戶端消息,2: 客戶端和服務(wù)器消息,3: 2和連接狀態(tài),4: 更詳細 'debug_output' => 'html', // Debug輸出類型。`echo`(默認),`html`,或`error_log` 'host' => 'smtp.126.com', // SMTP服務(wù)器地址 'port' => 465, // 端口號。默認25 'smtp_auth' => true, // 啟用SMTP認證 'smtp_secure' => 'ssl', // 啟用安全協(xié)議。''(默認),'ssl'或'tls',留空不啟用 'username' => 'yourname@example.com', // SMTP登錄郵箱 'password' => 'yourpassword', // SMTP登錄密碼。126郵箱使用客戶端授權(quán)碼,QQ郵箱用獨立密碼 'from' => 'from@example.com', // 發(fā)件人郵箱 'from_name' => 'name', // 發(fā)件人名稱 'reply_to' => '', // 回復(fù)郵箱的地址。留空取發(fā)件人郵箱 'reply_to_name' => '', // 回復(fù)郵箱人名稱。留空取發(fā)件人名稱 ];
注意:一般默認端口 25。如果使用了安全協(xié)議 ssl,那么端口號一般是 465 或 587。譬如 126 郵箱。建議使用安全協(xié)議,因為像阿里云服務(wù)器就禁止了非安全協(xié)議的 25 端口。
更多配置參數(shù),可以看看源碼: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 郵箱,安全協(xié)議 ssl,端口號 465,發(fā)送 html 內(nèi)容,測試成功:
參考鏈接:phpmail 的 SMTP 郵件實例
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
【網(wǎng)站聲明】本站除付費源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。