ThinkPHP中郵件發送功能

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

初次使用thinkphp架構,開發一個郵件發送功能,由於對架構不熟悉折騰了幾個小時終於成功了,以下是代碼記錄。

此函數只能在ThinkPHP中使用且需要phpmailer擴充的支援;
phpmail的:
https://code.google.com/a/apache-extras.org/p/phpmailer

將phpmailer解壓後放置擴充放置到第三方類庫擴充目錄下: ThinkPHP/Extend/Vendor/檔案夾下即可,並使用vendor方法來匯入。更詳細介紹參考:http://document.thinkphp.cn/manual_3_2.html#lib_extend

步驟1:建立think_send_mail方法

 /** * 系統郵件發送函數 * @param string $to    內送郵件者郵箱 * @param string $name  內送郵件者名稱 * @param string $subject 郵件主題  * @param string $body    郵件內容 * @return boolean  */function think_send_mail($to, $name, $subject = ‘‘, $body = ‘‘){    vendor(‘PHPMailer.class#phpmailer‘); //從PHPMailer目錄導class.phpmailer.php類檔案    $mail = new \PHPMailer;//此處必須加“\”號否則報錯        // Inform class to use SMTP        $mail->IsSMTP();        // Enable this for Testing        $mail->SMTPDebug  = 2;        // Enable SMTP Authentication        $mail->SMTPAuth   = true;        // Host of the SMTP Server        $mail->Host = ‘smtp.126.com‘;//SMTP伺服器使用者名稱        // Port of the SMTP Server        $mail->Port = 25;//SMTP伺服器連接埠        // SMTP User Name        $mail->Username   = "[email protected]";//郵箱地址        // SMTP User Password        $mail->Password = "********";//郵箱密碼        // Set From Email Address        $mail->SetFrom("[email protected]", $name);        // Add Subject        $mail->Subject= $subject;        // Add the body for mail        $mail->MsgHTML($body);        // Add To Address        $mail->AddAddress($to, $name);        // Finally Send the Mail        return $mail->Send() ? true : $mail->ErrorInfo;}

步驟2:在控制器調用即可

namespace Account\Controller;use Think\Controller;class AccountController extends Controller {    //調用發送郵件功能public function getPwd(){                 $toEmail =$_POST[‘email‘];         $subJect=‘郵件主題‘;         $body=‘郵件內容‘;         $name=‘寄件者名稱‘;         $result=$this->think_send_mail($toEmail,‘sever‘,$subJect,$body);         if($result)             echo "ok";         else             echo "failed";    }   } 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.