ThinkPHP5 封装邮件发送服务(可发附件)
生活随笔
收集整理的這篇文章主要介紹了
ThinkPHP5 封装邮件发送服务(可发附件)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、Composer 安裝 phpmailer
| 1 | composer require phpmailer/phpmailer |
?
2、ThinkPHP 中封裝郵件服務(wù)類
我把它封裝在擴展目錄 extend/Mail.php?文件里,內(nèi)容如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <?php /** * 郵件服務(wù)類 */ class?Mail?extends?\PHPMailer { ????function?__construct() ????{ ????????date_default_timezone_set('PRC');??????????????????????????// 默認(rèn)時區(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登錄認(rèn)證 ????????$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)容如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php /** ?* 郵件服務(wù)相關(guān)配置 ?*/ return?[ ????'charset'?=>?'utf-8',??????????????????// 郵件編碼 ????'smtp_debug'?=> 0,?????????????????????// Debug模式。0: 關(guān)閉,1: 客戶端消息,2: 客戶端和服務(wù)器消息,3: 2和連接狀態(tài),4: 更詳細(xì) ????'debug_output'?=>?'html',??????????????// Debug輸出類型。`echo`(默認(rèn)),`html`,或`error_log` ????'host'?=>?'smtp.126.com',??????????????// SMPT服務(wù)器地址 ????'port'?=> 465,?????????????????????????// 端口號。默認(rèn)25 ????'smtp_auth'?=> true,???????????????????// 啟用SMTP認(rèn)證 ????'smtp_secure'?=>?'ssl',????????????????// 啟用安全協(xié)議。''(默認(rèn)),'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ā)件人名稱 ]; |
?
注意:一般默認(rèn)端口 25。如果使用了安全協(xié)議 ssl,那么端口號一般是 465 或 587。譬如 126 郵箱。建議使用安全協(xié)議,因為像阿里云服務(wù)器就禁止了非安全協(xié)議的 25 端口。
更多配置參數(shù),可以看看源碼:https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php
?
3、測試
在控制器里方法里,添加測試代碼:
| 1 2 3 4 5 6 | 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 的 STMP 郵件實例
轉(zhuǎn)載于:https://www.cnblogs.com/shenzikun1314/p/7467198.html
總結(jié)
以上是生活随笔為你收集整理的ThinkPHP5 封装邮件发送服务(可发附件)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 8月31日笔记
- 下一篇: php里的抽象类和接口