PHP發送郵件類庫PHPMailer的簡單使用

來源:互聯網
上載者:User

標籤:style   blog   http   os   io   使用   ar   檔案   div   

最近需要用到發送郵件的功能,原本是用PHP內建的mail()函數發送的。php mail()這個方法非常簡單、方便、易用,但是除了網易郵箱、QQ郵箱、GMAIL郵箱等常用的郵箱可以收到之外,經測試HOTMAIL、TOM、LIVE等郵箱是收不到此類郵件的。所以就轉而使用PHPMailer這個強大的郵件發送類。

使用官方內建的一些例子,有些會報 Mailer Error: Could not instantiate mail function. 這個錯誤。參考了一些資料之後,還是自己寫了一個方法。代碼很簡單,就不多解釋了。

function mailto($nickname, $address, $id, $activation_code){date_default_timezone_set(‘PRC‘); include_once("class.phpmailer.php");$mail = new PHPMailer(); // defaults to using php "mail()"$mail->IsSMTP();$mail->Host = "smtp.163.com";// SMTP 伺服器  $mail->SMTPAuth = true;            // 開啟SMTP 認證  $mail->Username = "[email protected]"; // 使用者名稱$mail->Password = "yourpassword";          // 密碼  //$body = file_get_contents(‘application/views/nmra/register.html‘);//$body = preg_replace(‘/\\\\/‘,‘‘, $body); //Strip backslashes$body = ‘<p><body style="margin: 10px;"></p>‘;$body .= ‘<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 14px; ">‘;$body .= ‘<div align="center"><img src="images/phpmailer.gif" style="height: 90px; width: 340px"></div>‘;$body .= ‘<p>‘.$nickname.‘,您好。</p>‘;$body .= ‘<p>恭喜你成為簡明現代魔法研究協會的第‘.$id.‘名會員。</p>‘;$body .= ‘<p>現代魔法研究協會(NowaMagic Research Association)是一個程式猿、攻城獅、設計獅和開發人員們技術交流、話題討論的社區。希望在這裡你能找到感興趣的話題與志同道合的朋友。</p>‘;$body .= ‘請點擊以下連結驗證您的郵箱,請注意網域名稱為nowamagic.net:<a href="http://www.nowamagic.net/librarys/accounts/activation/?code="‘.$activation_code.‘" target="_blank">http://www.nowamagic.net/librarys/accounts/activation/?code=‘.$activation_code.‘</a>‘;$body .= ‘<p>順祝工作學習愉快,生活舒心。</p>‘;$body .= ‘</div></body>‘;//echo $body;$mail->AddReplyTo("[email protected]","Gonn");$mail->SetFrom(‘[email protected]‘, ‘Gonn‘);$mail->AddReplyTo("[email protected]","Gonn");$address = "[email protected]";//$address = "[email protected]";$mail->AddAddress($address, $nickname);$subject = "收到來自簡明現代魔法的郵件";$mail->Subject = "=?UTF-8?B?".base64_encode($subject)."?=";// optional, comment out and test$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; $mail->MsgHTML($body);//$mail->AddAttachment("images/phpmailer.gif");      // attachment//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachmentif(!$mail->Send()) {//echo "Mailer Error: " . $mail->ErrorInfo;} else {//echo "Message sent!";}}

  

使用的時候只要引入兩個PHP類,然後自己寫個方法就OK了,兩個類很小,發送郵件速度也很快。

PHPMailer 是一個功能強大的郵件類,其主要功能特點:

  1. 支援郵件 s/mime加密的數位簽章
  2. 支援郵件多個 TOs, CCs, BCCs and REPLY-TOs
  3. 可以工作在任何伺服器平台,所以不用擔心WIN平台無法發送郵件的問題的
  4. 支援文本/HTML格式郵件
  5. 可以嵌入image映像
  6. 對於郵件用戶端不支援HTML閱讀的進行支援
  7. 功能強大的發送郵件調試功能debug
  8. 自訂郵件header
  9. 冗餘SMTP伺服器支援
  10. 支援8bit, base64, binary, and quoted-printable 編碼
  11. 文字自動換行
  12. 支援多附件發送功能
  13. 支援SMTP伺服器驗證功能
  14. 在Sendmail, qmail, Postfix, Gmail, Imail, Exchange 等平台測試成功
  15. 提供的下載檔案中,包括內容詳細的說明文檔及樣本說明,所以不用擔心難於上手的問題!
  16. PHPMailer 非常小巧、簡單、方便、快捷

原文連結:http://www.nowamagic.net/librarys/veda/detail/1944

PHP發送郵件類庫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.