使用PHPMailer群发邮件
PHPMailer是一个用于发送电子邮件的PHP函数包。它提供的功能包括:
*.在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址
*.支持多种邮件编码包括:8bit,base64,binary和quoted-printable
*.支持SMTP验证
*.支持冗余SMTP服务器
*.支持带附件的邮件和Html格式的邮件
*.自定义邮件头
*.支持在邮件中嵌入图片
*.调试灵活
*.经测试兼容的SMTP服务器包括:Sendmail,qmail,Postfix,Imail,Exchange等
*.可运行在任何平台之上
PHPMailer的官方网站是:http://phpmailer.worxware.com/ 下载:https://github.com/Synchro/PHPMailer
一、使用示例:
testmail.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?php /** * Simple example script using PHPMailer with exceptions enabled * @package phpmailer * @version $Id$ */ require './phpmailer/class.phpmailer.php' ; try { $mail = new PHPMailer(true); //New instance, with exceptions enabled $body = file_get_contents ( 'contents.html' ); $body = preg_replace( '/\\/' , '' , $body ); //Strip backslashes $mail ->IsSMTP(); // tell the class to use SMTP $mail ->SMTPAuth = true; // enable SMTP authentication $mail ->Port = 25; // set the SMTP server port $mail ->Host = "smtp.qq.com" ; // SMTP server $mail ->Username = "gxlkan@qq.com" ; // SMTP server username $mail ->Password = "password" ; // SMTP server password // $mail->IsSendmail(); // tell the class to use Sendmail $mail ->AddReplyTo( "gxlkan@qq.com" , "First Last33" ); $mail ->From = "gxlkan@qq.com" ; $mail ->FromName = "First Last" ; $to = "3wqqq23@qq.com" ; $mail ->AddAddress( $to ); $mail ->Subject = "First PHPMailer Message11" ; $mail ->AltBody = "To view the message, please use an HTML compatible email viewer!11" ; // optional, comment out and test $mail ->WordWrap = 80; // set word wrap $mail ->MsgHTML( $body ); $mail ->IsHTML(true); // send as HTML $mail ->Send(); echo 'Message has been sent.' ; } catch (phpmailerException $e ) { echo $e ->errorMessage(); } ?> |
二、群发邮件
indexmail.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | <?php header ( 'Content-Type: text/html; charset=utf-8' ); require ( "phpmailer/class.phpmailer.php" ); error_reporting ( E_ERROR ); $handle_err = fopen ( 'error.log' , 'a+b' ); $handle_rig = fopen ( 'right.log' , 'a+b' ); $counter = 0; $mailconfig = array ( 'FromName' => '管理员' , 'SMTPAuth' => true, 'CharSet' => 'utf8' , 'Encoding' => 'base64' ); //Mail STMP 服务器 配置可能需要大量的账号,不然易被邮箱服务器给屏蔽掉了 $mailservers = array ( array ( 'host' => 'smtp.163.com' , 'username' => 'test1@163.com' , 'password' => 'test1' ), array ( 'host' => 'smtp.163.com' , 'username' => 'test2@163.com' , 'password' => 'test2' ), array ( 'host' => 'smtp.163.com' , 'username' => 'test3@163.com' , 'password' => 'test3' ) ); // 邮件内容 $body = file_get_contents ( 'contents.html' ); $sendto_email_arr = array ( 'gxwwn@qq.com' , '39sw23@qq.com' , '399823@qq.com' , '3aaa9823@qq.com' , '3qq3@qq.com' , 'www3@qq.com' , ); foreach ( $sendto_email_arr as $key => $val ){ smtp_mail ( $val , '欢迎你的到来' , $body , array ( 'email.txt' )); } function smtp_mail( $sendto_email , $subject , $body , $att = array ()) { global $handle , $mailconfig , $mailservers , $counter ; $mail = new PHPMailer (); $mail ->IsSMTP (); $mailserver = $mailservers [ $counter % count ( $mailservers )]; $mail ->Host = $mailserver [ 'host' ]; $mail ->Username = $mailserver [ 'username' ]; $mail ->Password = $mailserver [ 'password' ]; $mail ->FromName = $mailconfig [ 'FromName' ]; $mail ->SMTPAuth = $mailconfig [ 'SMTPAuth' ]; $mail ->From = $mail ->Username; $mail ->CharSet = $mailconfig [ 'CharSet' ]; $mail ->Encoding = $mailconfig [ 'Encoding' ]; $mail ->AddAddress ( $sendto_email ); // 对附件文件的处理 foreach ( $att as $key => $val ) { if (! empty ( $val )) { $mail ->AddAttachment ( $val ); // 注意要给绝对路径 } } $mail ->IsHTML ( true ); $mail ->Subject = $subject ; $mail ->Body = $body ; $mail ->AltBody = "text/html" ; if (! $mail ->Send ()){ //将错误写入到错误日志文件 fwrite ( $handle_err , $sendto_email . "--" .( $mail ->From). "rn" ); } else { echo "邮件发送成功给:$sendto_email -- $counter<br/>" ; fwrite ( $handle_rig , $sendto_email . "--" .( $mail ->From). "rn" ); } $counter ++; } // for ($i=0;$i<count($mailservers);$i++){ // 参数说明(发送地址, 邮件主题, 邮件内容,附件绝对路径) // smtp_mail ( '88888@qq.com', '欢迎你的到来', $body, array ('email.txt') ); // } fclose( $handle ); ?> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· .NET Core GC压缩(compact_phase)底层原理浅谈
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 深度学习基础理论————CV中常用Backbone(Resnet/Unet/Vit系列/多模态系列等