Thinkphp使用PHPMailer發送郵件遇到的問題

來源:互聯網
上載者:User

標籤:

第一次使用ThinkPHP編寫”找回密碼“功能,按照網上的DEMO做完後,試了好幾個郵箱都不管用,試了半天終於成功了,以下是我的實現過程:

第一步:準備PHPMailer

 

首先下載PHPMailer擴充,Google一下就有了。然後將下載的PHPMailer整個檔案夾放到ThinkPHP檔案夾裡面的Vendor目錄下。

第二步:修改設定檔

向conf.php設定檔中添加以下內容:

 

‘THINK_EMAIL‘ => array(        ‘SMTP_HOST‘   => ‘smtp.sina.com‘, //SMTP伺服器        ‘SMTP_PORT‘   => ‘25‘, //SMTP伺服器連接埠        ‘SMTP_USER‘   => ‘[email protected]‘, //SMTP伺服器使用者名稱        ‘SMTP_PASS‘   => ‘password‘, //SMTP伺服器密碼        ‘FROM_EMAIL‘  => ‘[email protected]‘, //寄件者EMAIL        ‘FROM_NAME‘   => ‘name‘, //寄件者名稱        ‘REPLY_EMAIL‘ => ‘‘, //回複EMAIL(留空則為寄件者EMAIL)        ‘REPLY_NAME‘  => ‘‘, //回複名稱(留空則為寄件者名稱)    )

目前只有新浪郵箱測試成功,並且郵箱須開啟SMTP服務,新浪郵箱SMTP連接埠為25。

 

 

第三步:將郵件發送函數放到common.php檔案

注意:新版ThinkPHP須把common.php改為function.php。

 

 

接著將以下函數添加到到function.php檔案中。

 

/** * 系統郵件發送函數 * @param string $to    內送郵件者郵箱 * @param string $name  內送郵件者名稱 * @param string $subject 郵件主題  * @param string $body    郵件內容 * @param string $attachment 附件列表 * @return boolean  */ function think_send_mail($to, $name, $subject = ‘‘, $body = ‘‘, $attachment = null){    $config = C(‘THINK_EMAIL‘);    vendor(‘PHPMailer.class#phpmailer‘); //從PHPMailer目錄導class.phpmailer.php類檔案    $mail             = new \PHPMailer(); //PHPMailer對象,注意要添加‘ \ ‘    $mail->CharSet    = ‘UTF-8‘; //設定郵件編碼,預設ISO-8859-1,如果發中文此項必須設定,否則亂碼    $mail->IsSMTP();  // 設定使用SMTP服務    $mail->SMTPDebug  = 0;                     // 關閉SMTP調試功能                                               // 1 = errors and messages                                               // 2 = messages only    $mail->SMTPAuth   = true;                  // 啟用 SMTP 驗證功能    $mail->SMTPSecure = ‘tls‘;                 // 使用安全性通訊協定,新浪郵箱使用‘tls‘安全性通訊協定,有些郵箱為‘ssl‘協議    $mail->Host       = $config[‘SMTP_HOST‘];  // SMTP 伺服器    $mail->Port       = $config[‘SMTP_PORT‘];  // SMTP伺服器的連接埠號碼    $mail->Username   = $config[‘SMTP_USER‘];  // SMTP伺服器使用者名稱    $mail->Password   = $config[‘SMTP_PASS‘];  // SMTP伺服器密碼    $mail->SetFrom($config[‘FROM_EMAIL‘], $config[‘FROM_NAME‘]);    $replyEmail       = $config[‘REPLY_EMAIL‘]?$config[‘REPLY_EMAIL‘]:$config[‘FROM_EMAIL‘];    $replyName        = $config[‘REPLY_NAME‘]?$config[‘REPLY_NAME‘]:$config[‘FROM_NAME‘];    $mail->AddReplyTo($replyEmail, $replyName);    $mail->Subject    = $subject;    $mail->AltBody    = "為了查看該郵件,請切換到支援 HTML 的郵件用戶端";     $mail->MsgHTML($body);    $mail->AddAddress($to, $name);    if(is_array($attachment)){ // 添加附件        foreach ($attachment as $file){            is_file($file) && $mail->AddAttachment($file);        }    }    return  $mail->Send() ? true : $mail->ErrorInfo; }

 

第四步:使用該函數發送郵件

 $r = think_send_mail(‘要發送的郵箱‘,‘發送人名稱,即你的名稱‘,‘檔案標題‘,‘郵件內容‘);

 

註:使用phpmailer發送郵件報錯

php.ini中去掉extension=php_openssl.dll前面的分號

並將allow_url_fopen = Off 改為 allow_url_fopen = On

 

內容來源:http://www.zkh.esy.es/2016/08/10/Thinkphp使用PHPMailer發送郵件遇到的問題

Thinkphp使用PHPMailer發送郵件遇到的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.