教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHPMailer批量发送邮件的实例代码

PHPMailer批量发送邮件的实例代码

发布时间:2015-09-16   编辑:jiaochengji.com
使用phpMailer发送邮件还是很方便的,这里介绍一个批量发送邮件的代码,供大家学习参考。

为大家介绍一个PHPMailer批量发送邮件的代码。
 

复制代码 代码示例:

<?php
/**
 * phpMailer批量发送邮件
 * by http://www.jiaochengji.com
*/
header ( 'Content-Type: text/html; charset=utf-8' );
require ("class.phpmailer.php");
error_reporting ( E_ERROR );
$handle = fopen ( 'error.log', 'a+b' );

$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'
  )
);


$counter = 0;
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, $sendto_email."--".($mail->From)."\r\n" );
 else
  echo "邮件发送成功! $counter\n";
 $counter ++;
}

// 邮件内容
$body = file_get_contents ( 'mail_content.php' );
for ($i=0;$i<count($mailservers);$i++){
 // 参数说明(发送地址, 邮件主题, 邮件内容,附件绝对路径)
 //smtp_mail ( '887799999@qq.com', '欢迎光临教程集', $body, array ('email.txt') );
}
fclose($handle);
?>

您可能感兴趣的文章:
phpmailer发送yahoo邮件的例子
phpmailer 类发送邮件乱码解决方法
PHPmailer邮件群发的入门例子
PHPMailer发送邮件代码实例(ubuntu系统)
phpmailer发送网易126邮箱的例子
phpmailer发送gmail邮件的例子
PHPMailer发送邮件中文附件名乱码的解决办法
PHPMailer邮件标题中文乱码的解决方法
phpmailer发邮件中文乱码问题如何解决
phpmailer类实现邮件群发的实例代码

关键词: PHPMailer  发送邮件   
[关闭]
~ ~