日期:2014-05-16  浏览次数:20368 次

phpmailer循环发送邮件失败
本帖最后由 an5464684 于 2013-12-27 18:20:21 编辑
使用的是phpmailer,账户和密码均正确,并测试通过。
1.单次发送会成功
2.但是当编写一个循环发邮件时,只有第一次发送成功,后面的都发送失败,查看Log显示:Could not authenticate,验证没有通过。
这是什么原因?

try 
{
$mail = new PHPMailer();
$mail->IsSMTP();   // set mailer to use SMTP
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->SMTPDebug  = 0;

$mail->Host = "smtp.126.com";  // specify main and backup server
$mail->Port = 25;

$mail->Username = "sent@126.com";  // SMTP username
$mail->Password = "******"; // SMTP password

$mail->From = $mail->Username;
$mail->FromName = "myname";
$mail->AddAddress("receive@163.com", "toname");

$mail->WordWrap = 50;         // set word wrap to 50 characters
$mail->IsHTML(true);          // set email format to HTML

$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if(!$mail->Send())
{
echo "Mailer Error: ".$mail->ErrorInfo;
return false;
}
else 
{
return true;
}

                       
} catch (phpmailerException $e) 
{
echo "Send mail failed: ".$e->errorMessage();
return false;
}

------解决方案--------------------
单次成功的话程序是没有问题的,因为你用的是126的邮件服务器那它肯定不会让你不间断的循环发送的,所以你可以再发送完成一封之后让程序sleep几秒钟
------解决方案--------------------
一般来说,像qq、163、126等邮箱,他们不会允许你连续发送的,所以如果你要循环发送的话,可以sleep 几秒,但每发一封邮件sleep几秒,效率肯定不高,也可以同时给多个用户发送,然后sleep几秒
------解决方案--------------------

<?php
    set_time_limit(0);
    ini_set("max_execution_time", "18000000");
    include 'PHPMailer/class.phpmailer.php';
    $sendmail = '';//收件人
    $title='我要发邮件';
    $remark='这是邮件内容';
$mailer=new PHPMailer();
$mailer->CharSet = "utf-8";
$mailer->ContentType = 'text/html';
$mailer->IsSMTP();
$mailer->SMTPDebug  = 0;
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = 'ssl';
$mailer->Host = 'smtp.163.com';
$mailer->Port = '465';
$mailer->Username = '';//发件人邮箱
$mailer->Password = 'xxx';//发件人密码
$mailer->SetFrom('','');
$mailer->AddAddress($sendmail);
$mailer->Subject =$title;
$mailer->MsgHTML($remark);
for($i = 0; $i< 10; $i++){
$mailer->send();
sleep(3);
    }