ThinkPHP 3.2使用PHPMailer發送郵件

來源:互聯網
上載者:User

標籤:http   io   ar   os   使用   sp   for   on   檔案   

首先你需要下載最新版的phpmailer,然後添加至TP的第三方類庫包目錄,如

第二步

在config.php添加如下代碼。這樣就可以讓第三步的函數擷取phpmailer的配置資訊了

  1.  
  2. <?php
  3. return array(
  4. // 配置郵件發送伺服器
  5. ‘MAIL_HOST‘ =>‘smtp.exmail.qq.com‘,
  6. ‘MAIL_SMTPAUTH‘ =>TRUE, //啟用smtp認證
  7. ‘MAIL_USERNAME‘ =>‘[email protected]‘,
  8. ‘MAIL_FROM‘ =>‘[email protected]‘,
  9. ‘MAIL_FROMNAME‘ =>‘cnsecer‘,
  10. ‘MAIL_PASSWORD‘ =>‘********‘,
  11. ‘MAIL_CHARSET‘ =>‘utf-8‘,
  12. ‘MAIL_ISHTML‘ =>TRUE, // 是否HTML格式郵件
  13. );
  14. ?>
  15.  
  16. 第三步

    然後就該引入這個類庫了,在 Application/common建立一個function.php,然後添加下面這個函數

    1.  
    2. /**
    3. * 郵件發送函數
    4. */
    5. function sendMail($to, $subject, $content) {
    6.  
    7. Vendor(‘PHPMailer.PHPMailerAutoload‘);
    8.  
    9. $mail = new PHPMailer(); //執行個體化
    10. $mail->IsSMTP(); // 啟用SMTP
    11. $mail->Host=C(‘MAIL_HOST‘); //smtp伺服器的名稱(這裡以126郵箱為例)
    12. $mail->SMTPAuth = C(‘MAIL_SMTPAUTH‘); //啟用smtp認證
    13. $mail->Username = C(‘MAIL_USERNAME‘); //你的郵箱名
    14. $mail->Password = C(‘MAIL_PASSWORD‘) ; //郵箱密碼
    15. $mail->From = C(‘MAIL_FROM‘); //寄件者地址(也就是你的郵箱地址)
    16. $mail->FromName = C(‘MAIL_FROMNAME‘); //寄件者姓名
    17. $mail->AddAddress($to,"name");
    18. $mail->WordWrap = 50; //設定每行字元長度
    19. $mail->IsHTML(C(‘MAIL_ISHTML‘)); // 是否HTML格式郵件
    20. $mail->CharSet=C(‘MAIL_CHARSET‘); //設定郵件編碼
    21. $mail->Subject =$subject; //郵件主題
    22. $mail->Body = $content; //郵件內容
    23. $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //郵件內文不支援HTML的備用顯示
    24. if(!$mail->Send()) {
    25. echo "Message could not be sent. <p>";
    26. echo "Mailer Error: " . $mail->ErrorInfo;
    27. exit();
    28. } else {
    29. echo "Message has been sent";
    30. }
    31. }
    32.  
    33. 最後

      在任意地方調用下面函數,就可收到郵件

      1.  
      2. sendMail("[email protected]","標題","內容");
      3.  

    34. 相關檔案下載http://pan.baidu.com/s/1mg9ILM4

ThinkPHP 3.2使用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.