php使用phpMailer發送郵件的例子

來源:互聯網
上載者:User
  1. require("phpmailer/class.phpmailer.php");
  2. function smtp_mail( $sendto_email, $subject, $body, $extra_hdrs, $user_name){
  3. $mail = new PHPMailer();
  4. $mail->IsSMTP(); // send via SMTP
  5. $mail->Host = "smtp.163.com"; // SMTP servers
  6. $mail->SMTPAuth = true; // turn on SMTP authentication
  7. $mail->Username = "xuchao842363331"; // SMTP username 注意:普通郵件認證不需要加 @網域名稱 這裡是我的163郵箱
  8. $mail->Password = "password"; // SMTP password 在這裡輸入郵箱的密碼
  9. $mail->From = "jbxue123@163.com"; // 寄件者郵箱
  10. $mail->FromName = "管理員"; // 寄件者
  11. $mail->CharSet = "UTF-8"; // 這裡指定字元集! 指定UTF-8後郵件的標題和寄件者等等不會亂碼,如果是GB2312標題會亂碼
  12. $mail->Encoding = "base64";
  13. $mail->AddAddress($sendto_email,"username"); // 收件者郵箱和姓名
  14. $mail->AddReplyTo("yourmail@yourdomain.com","yourdomain.com");
  15. //$mail->WordWrap = 50; // set word wrap 換行字數
  16. //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 附件
  17. //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
  18. //$mail->IsHTML(true); // send as HTML
  19. // 郵件主題
  20. $mail->Subject = $subject;
  21. // 郵件內容
  22. $mail->Body = "hello!PHPMailer";
  23. //$mail->AltBody ="text/html";
  24. if(!$mail->Send())
  25. {
  26. echo "error

    ";

  27. echo "error: " . $mail->ErrorInfo;
  28. exit;
  29. }
  30. else {
  31. echo"success!";
  32. }
  33. }
  34. // 參數說明(發送到, 郵件主題, 郵件內容, 附加資訊, 使用者名稱)
  35. ?>
複製代碼

說明:當字元集指定為GB2312的時候標題會亂碼,這裡指定為UTF-8不會出現亂碼。其實,PHPMailer還有很多功能,比如可以添加附件等等,這裡就不再示範了。 

這樣在需要用郵件功能時,便可以調用這個函數:

  1. require("mail.php");
  2. smtp_mail("790896688@qq.com", "歡迎訪問程式員之家", "", "", "username");
  3. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.