ThinkPHP中使用PHPMailer郵件類

來源:互聯網
上載者:User

標籤:blog   io   ar   os   使用   檔案   div   on   log   

第一步、添加PHPMailer類庫
將下載後的檔案解壓,將PHPMail目錄移動至ThinkPHP目錄中的Vendor內。(請確保class.phpmailer.php檔案就在ThinkPHP\Vendor\PHPMailer\class.phpmailer.php)
第二步、添加發送郵件函數
在項目目錄中的Common檔案夾中的common.php檔案(如果沒有請建立)添加如下代碼:

<?php/********** * 發送郵件 * **********/function SendMail($address,$title,$message){    vendor(‘PHPMailer.class#PHPMailer‘);$mail=new PHPMailer();    // 設定PHPMailer使用SMTP伺服器發送Email    $mail->IsSMTP();    // 設定郵件的字元編碼,若不指定,則為‘UTF-8‘    $mail->CharSet=‘UTF-8‘;    // 添加收件者地址,可以多次使用來添加多個收件者    $mail->AddAddress($address);    // 設定郵件內文    $mail->Body=$message;    // 設定郵件標頭的From欄位。    $mail->From=C(‘MAIL_ADDRESS‘);    // 設定寄件者名字    $mail->FromName=‘LilyRecruit‘;    // 設定郵件標題    $mail->Subject=$title;    // 設定SMTP伺服器。    $mail->Host=C(‘MAIL_SMTP‘);    // 設定為"需要驗證"    $mail->SMTPAuth=true;    // 設定使用者名稱和密碼。    $mail->Username=C(‘MAIL_LOGINNAME‘);    $mail->Password=C(‘MAIL_PASSWORD‘);    // 發送郵件。    return($mail->Send());}?>

第三步、配置郵箱資訊
編輯Conf目錄下的config.php,在return array添加如下內容

‘MAIL_ADDRESS‘=>‘[email protected]‘, // 郵箱地址‘MAIL_SMTP‘=>‘smtp.126.com‘, // 郵箱SMTP伺服器‘MAIL_LOGINNAME‘=>‘xxx‘, // 郵箱登入帳號‘MAIL_PASSWORD‘=>‘xxx‘, // 郵箱密碼

第四步、在Action中發送郵件
由於ThinkPHP會自動載入common.php中的函數,所以在需要發送郵件的時候,只需要使用如下代碼即可
SendMail("[email protected]","郵件標題","郵件內文");

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.