使用phpmailer發送郵件的例子

來源:互聯網
上載者:User
  1. require_once(dirname(__FILE__)."/../PHPMailer/class.phpmailer.php");

  2. //包含class.phpmailer.php

  3. /**
  4. * @param string $send_to_mail 目標郵件
  5. * @param stinrg $subject 主題
  6. * @param string $body 郵件內容
  7. * @param string $extra_hdrs 附加資訊
  8. * @param string $username 收件者
  9. * @param string $replyname 回複人
  10. * @param string $replymail 回複地址
  11. * @return array(bealoon,string) 返回數組包括兩個元素,bealoon表示是否成功,string為提示資訊
  12. */
  13. function SendMail($send_to_mail,$subject,$body,$extra_hdrs,$username,$replyname="reply",$replymail="reply@reply.com"){
  14. $mail=new PHPMailer();
  15. $mail->IsSMTP(); //郵件發送方式
  16. $mail->Host="smtp.host.com"; //SMTP伺服器主機地址
  17. $mail->SMTPAuth=true; //是否為可信任的SMTP
  18. $mail->Username="reply@reply.com"; //SMTP 使用者名稱 注意:普通郵件認證不需要加 @網域名稱
  19. $mail->Password="******"; //SMTP 使用者密碼
  20. $mail->From="send@send.com"; //寄件者郵件地址
  21. $mail->FromName="send"; //寄件者
  22. $mail->CharSet="GB2312"; //指定字元集
  23. $mail->Encoding="base64";
  24. $mail->AddAddress($send_to_mail,$username); //添加發送目標地址
  25. $mail->AddReplyTo($replymail,$replyname); //添加回複地址
  26. $mail->IsHTML(true); //郵件類型為HTML格式
  27. $mail->Subject=$subject; //郵件主題
  28. //郵件內容
  29. $mail->Body="
  30. ".$body."
  31. ";
  32. $mail->AltBody="text/html"; //內容文字格式設定
  33. if (@!$mail->Send()) {
  34. $results=array("result"=>false,"message"=>$mail->ErrorInfo);
  35. return $results;
  36. }else{
  37. $results = array("result"=>true,"message"=>"郵件已經發送到{$send_to_mail}!");
  38. return $results;
  39. }
  40. }

  41. $send_mail=SendMail($to,$subject,$content,$headers,$name);

  42. if($send_mail["result"]){
  43. echo $send_mail["message"];
  44. }else{
  45. echo $send_mail["message"];
  46. }
  47. exit();
  48. ?>

複製代碼

例2:

  1. include ('class/class.phpmailer.php');

  2. $config = array(

  3. 'host'=>'smtp.163.com',
  4. 'port'=>'25',
  5. 'user'=>'***',
  6. 'passwd'=>'****',
  7. 'from'=>'juva_zz@163.com',
  8. 'fromname'=>'鄭州',

  9. );

  10. $subject = 'this is a test mail';
  11. $body = '
    測試內容
    這是內容
    ';
  12. $address='379018082@qq.com';
  13. $username='本人';

  14. $mail = new PHPMailer();

  15. $mail->CharSet = 'gb2312';
  16. $mail->IsSMTP();
  17. $mail->Host = $config['host'];
  18. $mail->Port = $config['port'];

  19. $mail->From = $config['from'];

  20. $mail->FromName = $config['fromname'];
  21. $mail->SMTPAuth = true;

  22. $mail->Username = $config['user'];

  23. $mail->Password = $config['passwd'];

  24. $mail->Subject=$subject;

  25. $mail->AltBody="text/html";
  26. $mail->MsgHTML($body);

  27. $mail->AddAddress($address,$username);

  28. if(!$mail->Send())

  29. {
  30. echo "Mail Error :".$mail->ErrorInfo;
  31. }else
  32. {
  33. echo "恭喜發送成功!";
  34. }

複製代碼
  • 聯繫我們

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