PHPMailer

是一個功能豐富的函式庫,以下是用 PHPMailer 通過遠端 SMTP 認證發送郵件的例子:

  1. // 建立 PHPMailer 物件及設定 SMTP 登入資訊
  2. require("../phpMailer/class.phpmailer.php");
  3. $mail = new PHPMailer();
  4. $mail->IsSMTP(); // send via SMTP
  5. $mail->Host = "remote.smtp.server"; // SMTP servers
  6. $mail->SMTPAuth = true; // turn on SMTP authentication
  7. $mail->Username = "me@localhost"; // SMTP username
  8. $mail->Password = "123456"; // SMTP password
  9.  
  10. $mail->From = "myemail@localhost";
  11. $mail->FromName = "My Name";
  12.  
  13. // 執行 $mail->AddAddress() 加入收件者,可以多個收件者
  14. $mail->AddAddress("to@email.com","Josh Adams");
  15. $mail->AddAddress("to2@email.com"); // optional name
  16.  
  17. $mail->AddReplyTo("jyu@aemtechnology.com","AEM");
  18.  
  19. $mail->WordWrap = 50; // set word wrap
  20.  
  21. // 執行 $mail->AddAttachment() 加入附件,可以多個附件
  22. $mail->AddAttachment("path_to/file"); // attachment
  23. $mail->AddAttachment("path_to_file2", "INF");
  24.  
  25. // 電郵內容,以下為發送 HTML 格式的郵件
  26. $mail->IsHTML(true); // send as HTML
  27. $mail->Subject = "testing email";
  28. $mail->Body = "This is the HTML body";
  29. $mail->AltBody = "This is the text-only body";
  30.  
  31. if(!$mail->Send())
  32. {
  33.     echo "Message was not sent

    "

    ;
  34.     echo "Mailer Error: " . $mail->ErrorInfo;
  35.     exit;
  36. }
  37.  
  38. echo "Message has been sent";
  39. ?>

轉貼自http://www.real-blog.com/programming/416

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 herb123456 的頭像
    herb123456

    PHP、MySQL、ExtJS、Linux、CentOS、Ubuntu

    herb123456 發表在 痞客邦 留言(1) 人氣()