使用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
<?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

<?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);
?>

转载来自:http://www.oschina.net/code/snippet_270698_18995


posted @ 2013-05-08 01:19  gxldan  阅读(601)  评论(0编辑  收藏  举报