利用phpmailer 发送邮件代码[发送html内容]

我们利用 phpmailer功能实现邮件发送功能哦,这里还利用了模板呢,就是读取指定文件内容再发送给朋友.

  1. <?php
  2. @session_start();
  3. include(dirname(__FILE__).'./inc/function.php');
  4. require(dirname(__FILE__)."/mail/class.phpmailer.php");
  5. $array = array_unique(Get_value('mail',1));
  6. $type = Get_value('type',1);
  7. $mail = new PHPMailer();
  8. $count =0;
  9. $bad =0;
  10. $mail->IsSMTP(); // set mailer to use SMTP
  11. $mail->Host = "smtp.163.com"; // smtp1.example.com;smtp2.example.comspecify main and backup server
  12. $mail->SMTPAuth = true; // turn on SMTP authentication
  13. $mail->Username = "mailangel123"; // SMTP username
  14. $mail->Password = "******"; // SMTP password
  15. $mail->From = "mailangel123@163.com";
  16. $mail->FromName = "你的好友来信";
  17. $MailBody = GetContent($type);
  18. //$array =explode('|',$rs['mail']);
  19. foreach( $array as $tmpmail ){
  20. if( @preg_match("/w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/",$tmpmail)
  21. || strlen($User_Mail)<6 )
  22. {
  23. $mail->AddReplyTo("mailangel123@163.com", "44");
  24. $mail->AddAddress($tmpmail,'您好!');
  25. $mail->WordWrap = 50;
  26. $mail->CharSet="GB2312";
  27. //$mail->AddAttachment("/var/tmp/file.tar.gz");
  28. //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
  29. $mail->IsHTML(true);
  30. $mail->Subject = "你的朋友邀请你一起合影!";
  31. $mail->Body = $MailBody;
  32. if(!$mail->Send())
  33. {
  34. $bad++;
  35. $mail->ClearAddresses();
  36. $mail->ClearAttachments();
  37. }
  38. else
  39. {
  40. $count++;
  41. }
  42. }
  43. ShowMsg("result:$count");
  44. }
  45. 下面这个文章是读取html 文档并进行html发送哦,
  46. function GetContent($type){
  47. if( $type )
  48. {
  49. if(file_exists('./mail_room.html') )
  50. {
  51. $content = file_get_contents( './mail_room.html');
  52. }
  53. else
  54. {
  55. ShowMsg('file can' read fail ');
  56. }
  57. }
  58. else
  59. {
  60. if( file_exists( './mail_person.html') )
  61. {
  62. $content = file_get_contents( './mail_person.html');
  63. }
  64. else
  65. {
  66. ShowMsg('person file read fail!');
  67. }
  68. }
  69. return $content;
  70. }
  71. /* echo "<script>alert('发关".$count."邮件成功');</script>"; */
  72. ?>