继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

PHP定时发送邮件

大吉大利今晚学习
关注TA
已关注
手记 265
粉丝 36
获赞 142


     

  点击(此处)折叠或打开

     

  1.   <?php
     

  2.       require_once ('class.phpmailer.php');

  3.       //发送Email函数

  4.       function smtp_mail($sendto_email, $subject, $body,$extra_hdrs, $user_name){

  5.           $mail = new PHPMailer();

  6.           $mail->IsSMTP();

  7.           $mail->Host="smtp.163.com";

  8.           $mail->SMTPAuth = true;

  9.           $mail->Username = "name" //SMTP username,普通邮件认证部需要加@域名

  10.           $mail->Password = "123456";

  11.           $mail->From = "name@163.com"

  12.           $mail->FromName="XX网站";

  13.           $mail->CharSet="GB2312";

  14.           $mail->Encoding = "base64";

  15.           $mail->AddAddress($sendto_email,$user_name); //$user_name可省略

  16.           $mail->AddReplyTo("name@qq.com","XX科技公司"); //回复给name@qq.com邮箱

  17.  

  18.           $mail->wordwrap = 50;

  19.           $mail->AddAttachment("/var/tmp/file.tgz"); //附件1

  20.           $mail->AddAttachment("/tmp/image.jpg","new.jpt"); //附件2

  21.  

  22.           $mail->IsHTML(true);

  23.           $mail->Subject = $subject;

  24.           $mail->Body = <<<EOT

  25.           <html>

  26.           <head>

  27.           <meta http-equiv="Content-Language" c>

  28.           <meta http-equiv="Content-Type" c>

  29.           </head>

  30.           <body>

  31.               {$user_name}您好,欢迎你注册本站用户!

  32.           </body>

  33.           </html>

  34.           EOT;

  35.  

  36.           $mail->AltBody="text/html";

  37.           if($mail-Send()){    

  38.               info_write("ok.txt","$user_name发送成功");

  39.           }else{

  40.               info_write("falied.txt","$user_name发送失败,错误信息$mail->ErrorInfo");

  41.           }

  42.  

  43.       }

  44.  

  45.       function info_write($filename,$info_log){

  46.           $info.= $info_log;

  47.           $info.="\r\n";

  48.           $fp = fopen($filename,a);

  49.           fwrite($fp,$info);

  50.           fclose($fp);

  51.       }

  52.   //定时跳转到页面函数,1000是1秒,可自定义

  53.       function redirect($url){

  54.           echo "<script>
     

  55.           functon redirect(){

  56.               window.location.replace('$url');

  57.           }

  58.               window.setTimeout('redirect();',15000);

  59.  

  60.           </sctipt>";

  61.       }

  62.  

  63.   //读取文本邮件地址,也可以读数据库

  64.       $filename = "email.txt";

  65.       $fp = fopen($filename,"r");

  66.       $contents = fread($fp,filesize($filename));

  67.       $num_email=explode("\r\n",$contents);

  68.       $len = count($num_email);

  69.       fclose($fp);

  70.   //参数说明(发送到,邮件主题,内容,附件,用户名)

  71.       $i = $_GET['action'];

  72.       $i++;

  73.       if($i<$len){

  74.           $rs=explode("@",$num_email[$i]);

  75.           $user_name = $rs['0'];

  76.           echo "正在发送第{$i}封({$num_email[$i]})邮件。。。";

  77.           smtp_mail($num_email[$i],'XX周刊第十期',

  78.               $body,'http://jc258.com',$user_name);

  79.           redirect("?action=$i");

  80.       }else{

  81.           echo "邮件全部发送完毕";

  82.           exit;

  83.       }

  84.  

  85.   ?>

 

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP