最近需要用到發(fā)送郵件的功能,原本是用PHP自帶的mail()函數(shù)發(fā)送的。php mail()這個方法非常簡單、方便、易用,但是除了網(wǎng)易郵箱、QQ郵箱、GMAIL郵箱等常用的郵箱可以收到之外,經(jīng)測試HOTMAIL、TOM、LIVE等郵箱是收不到此類郵件的。所以就轉(zhuǎn)而使用PHPMailer這個強大的郵件發(fā)送類。
使用官方自帶的一些例子,有些會報 Mailer Error: Could not instantiate mail function. 這個錯誤。參考了一些資料之后,還是自己寫了一個方法。代碼很簡單,就不多解釋了。
01 | function mailto( $nickname , $address , $id , $activation_code ) |
03 | date_default_timezone_set( 'PRC' ); |
04 | include_once ( "class.phpmailer.php" ); |
06 | $mail = new PHPMailer(); // defaults to using php "mail()" |
08 | $mail ->Host = "smtp.163.com" ; // SMTP 服務器 |
09 | $mail ->SMTPAuth = true; // 打開SMTP 認證 |
10 | $mail ->Username = "nowamagic@163.com" ; // 用戶名 |
11 | $mail ->Password = "yourpassword" ; // 密碼 |
13 | //$body = file_get_contents('application/views/nmra/register.html'); |
14 | //$body = preg_replace('/\\\\/','', $body); //Strip backslashes |
15 | $body = '<p><body style="margin: 10px;"></p>' ; |
16 | $body .= '<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 14px; ">' ; |
17 | $body .= '<div align="center"><img src="images/phpmailer.gif" style="height: 90px; width: 340px"></div>' ; |
18 | $body .= '<p>' . $nickname . ',您好。</p>' ; |
19 | $body .= '<p>恭喜你成為簡明現(xiàn)代魔法研究協(xié)會的第' . $id . '名會員。</p>' ; |
20 | $body .= '<p>現(xiàn)代魔法研究協(xié)會(NowaMagic Research Association)是一個程序猿、攻城獅、設(shè)計獅和開發(fā)者們技術(shù)交流、話題討論的社區(qū)。希望在這里你能找到感興趣的話題與志同道合的朋友。</p>' ; |
22 | $body .= '<p>順祝工作學習愉快,生活舒心。</p>' ; |
23 | $body .= '</div></body>' ; |
26 | $mail ->AddReplyTo( "nowamagic@163.com" , "Gonn" ); |
27 | $mail ->SetFrom( 'nowamagic@163.com' , 'Gonn' ); |
28 | $mail ->AddReplyTo( "nowamagic@163.com" , "Gonn" ); |
29 | $address = "252211974@qq.com" ; |
30 | //$address = "nowamagic@gmail.com"; |
31 | $mail ->AddAddress( $address , $nickname ); |
33 | $subject = "收到來自簡明現(xiàn)代魔法的郵件" ; |
34 | $mail ->Subject = "=?UTF-8?B?" . base64_encode ( $subject ). "?=" ; |
35 | // optional, comment out and test |
36 | $mail ->AltBody = "To view the message, please use an HTML compatible email viewer!" ; |
37 | $mail ->MsgHTML( $body ); |
39 | //$mail->AddAttachment("images/phpmailer.gif"); // attachment |
40 | //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment |
43 | //echo "Mailer Error: " . $mail->ErrorInfo; |
46 | //echo "Message sent!"; |
使用的時候只要引入兩個PHP類,然后自己寫個方法就OK了,兩個類很小,發(fā)送郵件速度也很快。
PHPMailer 是一個功能強大的郵件類,其主要功能特點:
- 支持郵件 s/mime加密的數(shù)字簽名
- 支持郵件多個 TOs, CCs, BCCs and REPLY-TOs
- 可以工作在任何服務器平臺,所以不用擔心WIN平臺無法發(fā)送郵件的問題的
- 支持文本/HTML格式郵件
- 可以嵌入image圖像
- 對于郵件客戶端不支持HTML閱讀的進行支持
- 功能強大的發(fā)送郵件調(diào)試功能debug
- 自定義郵件header
- 冗余SMTP服務器支持
- 支持8bit, base64, binary, and quoted-printable 編碼
- 文字自動換行
- 支持多附件發(fā)送功能
- 支持SMTP服務器驗證功能
- 在Sendmail, qmail, Postfix, Gmail, Imail, Exchange 等平臺測試成功
- 提供的下載文件中,包括內(nèi)容詳細的說明文檔及示例說明,所以不用擔心難于上手的問題!
- PHPMailer 非常小巧、簡單、方便、快捷
|