使用 PHPMailer 發送郵件

PHPMailer 的官方網站:http://phpmailer.worxware.com/php

PHPMailer 最新類庫下載地址:【點擊下載html

PHPMailer GitHub 下載地址:https://github.com/Synchro/PHPMailer,該頁面也提供了使用例子,不過不是很全面。git

使用方法,詳見代碼清單:github

 

[php]  view plain  copy
 
  1. <?php  
  2. header('Content-Type:text/html;Charset=utf-8');  
  3. require './PHPMailerAutoload.php';  
  4. $mail = new PHPMailer;  
  5. $mail->isSMTP();                                      // 設置郵件使用SMTP  
  6. $mail->Host = 'mail.wanzhao.com';                     // 郵件服務器地址  
  7. $mail->SMTPAuth = true;                               // 啓用SMTP身份驗證  
  8. $mail->CharSet = "UTF-8";                             // 設置郵件編碼  
  9. $mail->setLanguage('zh_cn');                          // 設置錯誤中文提示  
  10. $mail->Username = 'wanzhao@wanzhao.com';              // SMTP 用戶名,即我的的郵箱地址  
  11. $mail->Password = 'www123456';                        // SMTP 密碼,即我的的郵箱密碼  
  12. $mail->SMTPSecure = 'tls';                            // 設置啓用加密,注意:必須打開 php_openssl 模塊  
  13. $mail->Priority = 3;                                  // 設置郵件優先級 1:高, 3:正常(默認), 5:低  
  14. $mail->From = 'liruxing@wanzhao.com';                 // 發件人郵箱地址  
  15. $mail->FromName = '李茹星';                     // 發件人名稱  
  16. $mail->addAddress('mail1@163.com', 'Lee');     // 添加接受者  
  17. $mail->addAddress('ellen@example.com');               // 添加多個接受者  
  18. $mail->addReplyTo('info@example.com', 'Information'); // 添加回復者  
  19. $mail->addCC('mail2@sina.com');                // 添加抄送人  
  20. $mail->addCC('mail3@qq.com');                     // 添加多個抄送人  
  21. $mail->ConfirmReadingTo = 'liruxing@wanzhao.com';     // 添加發送回執郵件地址,即當收件人打開郵件後,會詢問是否發生回執  
  22. $mail->addBCC('mail4@qq.com');                    // 添加密送者,Mail Header不會顯示密送者信息  
  23. $mail->WordWrap = 50;                                 // 設置自動換行50個字符  
  24. $mail->addAttachment('./1.jpg');                      // 添加附件  
  25. $mail->addAttachment('/tmp/image.jpg', 'one pic');    // 添加多個附件  
  26. $mail->isHTML(true);                                  // 設置郵件格式爲HTML  
  27. $mail->Subject = 'Here is the 主題';  
  28. $mail->Body    = 'This is the HTML 信息 body <b>in bold!</b>. Time:'.date('Y-m-d H:i:s');  
  29. $mail->AltBody = 'This is the 主體 in plain text for non-HTML mail clients';  
  30.   
  31. if(!$mail->send()) {  
  32.     echo 'Message could not be sent.';  
  33.     echo 'Mailer Error: ' . $mail->ErrorInfo;  
  34.     exit;  
  35. }  
  36.   
  37. echo 'Message has been sent';  
相關文章
相關標籤/搜索