ThinkPHP5 封裝郵件發送服務(可發附件)

1、 Composer 安裝 phpmailer

composer require phpmailer/phpmailer

 

2、ThinkPHP 中封裝郵件服務類

我把它封裝在擴展目錄 extend/Mail.php 文件裏,內容以下: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;
        }
    }
}

  

注意:若是發送附件,建議使用英文路徑。中文路徑可能會致使附件發送失敗,收到的郵件沒有附件。html

上面須要的一些配置參數,我把它們放在擴展配置目錄 application/extra/mail.php 文件裏 ,內容以下:git

<?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 端口。github

更多配置參數,能夠看看源碼: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 內容,測試成功:bash

 

參考連接:phpmail 的 SMTP 郵件實例服務器

相關文章
相關標籤/搜索