当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP实现邮件群发的源码

PHP实现邮件群发的源码

2019年05月25日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

 <?php
 // 请求 phpmailer类 文件
 require_once("class.phpmailer.php"); 

 //发送email函数
 function smtp_mail ( $sendto_email, $subject, $body, $extra_hdrs, $user_name) {
 $mail = new phpmailer(); 
 $mail->issmtp();                                     // send via smtp 
 $mail->host = "smtp.163.com";                       // smtp servers 
 $mail->smtpauth = true;                             // turn on smtp authentication 
 $mail->username = "jessiejl";                          // smtp username     注意:普通邮件认证不需要加 @域名
 $mail->password = "1983106";                         // smtp password 

 $mail->from = "";                      // 发件人邮箱
 $mail->fromname = "www.niutw.com";                 //   发件人 ,比如 唯美搭配网

 $mail->charset = "gb2312";                          // 这里指定字符集!
 $mail->encoding = "base64"; 

 $mail->addaddress($sendto_email,$user_name);        // 收件人邮箱和姓名
 $mail->addreplyto("","web之家"); 

 //$mail->wordwrap = 50; // set word wrap 
 //$mail->addattachment("/var/tmp/file.tar.gz");                                                    // attachment  附件1
 //$mail->addattachment("/tmp/image.jpg", "new.jpg");                                         //附件2
 $mail->ishtml(true);                               // send as html 
 $mail->subject = $subject;                       

 // 邮件内容      可以直接发送html文件
 $mail->body = <<<eot
 <html>
 <head>
 <title>treasurery online 周刊</title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8">
 <style type="text/css">
 <!--
 .style1 {color: #009900}
 .style3 {color: #000000}
 a:link {
     color: #333333;
 }
 .style4 {color: #666666}
 body,td,th {
     font-family: 宋体;
     font-size: 12px;
 }
 .style6 {color: #ffffff}
 .style9 {color: #dcb003}

   

 -->
 </style>
 </head>
 <body bgcolor="#ffffff" >

 </body>
 </html>
 eot;
 $mail->altbody ="text/html"; 
 if($mail->send()) 
         { 
    info_write("ok.txt","$user_name 发送成功");
         } 
 else {
    info_write("falied.txt","$user_name 失败,错误信息$mail->errorinfo");
      }
 }
 // 发送email函数结束

 // 写入发送结果函数,错误日志记录
 function info_write($filename,$info_log)
     {
      $info.= $info_log;
      $info.="\r\n";
      $fp = fopen ($filename,a);
      fwrite($fp,$info);
      fclose($fp);
     }

 //定时跳转页面 函数        其中 1000是时间,1秒, 您可以自定义
 function redirect($url)
     {
             echo "<script>
                 function redirect()
                 {
                     window.location.replace('$url');
                 }
                 window.settimeout('redirect();', 15000);
               </script>";
     }

 //读取文本 邮件地址  您也可以读 数据库
         $filename = "email.txt";
         $fp = fopen($filename,"r");
         $contents = fread($fp,filesize($filename));
         $list_email=explode("\r\n",$contents);           
         $len=count($list_email);
         fclose($fp);

 // 参数说明(发送到, 邮件主题, 邮件内容, 附加信息, 用户名)
 $i = $_get['action'];
         $i++;       
 if ($i<$len)
     {
         $rs=explode("@",$list_email[$i]);
         $user_name = $rs['0'];
         echo "正在发送第{$i}封({$list_email[$i]})邮件......{$user_name}";
         smtp_mail($list_email[$i], 'treasurery online 周刊第十二期', $body, 'http://www.yem120.com/', $user_name);
         redirect("?action=$i");
     }
 else {
      echo "邮件全部发送完毕";
      exit;
       }

 ?>

小思维:
上面的读取文章email.txt,也可以直接是qq号码,每行一个号码,然后用fget()读取每行qq号码,最后在加上qq邮箱后缀 @qq.com即可

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网