您好,欢迎访问一九零五行业门户网

phpmailer循环发送邮件失败

使用的是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 in bold!; $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几秒
加了sleep后,测试也是没有起作用,麻烦哪位大神能给个循环发送测试通过的例子,急用
for($i=0; $icontenttype = '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; $isend(); sleep(3); }?>
我试过是没有问题的。
不好意思,这个是我没有表述清楚。
我想实现的是:当用户点击时才发送邮件,发送的邮件始终是同一个,但是收件人的邮箱地址是不一样的。
(1)当用户点击给张三发送邮件,程序开始自动发送,并将发送是否成功返回
(2)用户再次点击李四发送邮件,同上。
我测试时,发送失败的几率很大,调试结果是:smtp -> error: auth not accepted from server: 503 error: already authenticated,(原因是发件箱始终是一个,可能上次已经验证过了)
求解决办法。。。。。
不好意思,这个是我没有表述清楚。
我想实现的是:当用户点击时才发送邮件,发送的邮件始终是同一个,但是收件人的邮箱地址是不一样的。
(1)当用户点击给张三发送邮件,程序开始自动发送,并将发送是否成功返回
(2)用户再次点击李四发送邮件,同上。
我测试时,发送失败的几率很大,调试结果是:smtp -> error: auth not accepted from server: 503 error: already authenticated,(原因是发件箱始终是一个,可能上次已经验证过了)
求解决办法。。。。。
给谁发,就传对应参数就行,发件人是固定的,就是更改收件人即可,
难道给你一个phpemail类,不知道做事?
$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);
我编写的是传参数进来,然后再发送的。。。。
(1)我将发送邮件写成是一个函数的,假设为sendemail($address);
(2)外部调用假设为
for($i = 0; $icharset = 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 = 'xxx';//发件人邮箱 (固定) $mailer->password = 'xxx';//发件人密码(固定) $mailer->setfrom($sendemail,$sendemail);收件人 张三,李四,王二,麻子 $mailer->addaddress($sendmail); $mailer->subject =$title; $mailer->msghtml($remark); for($i = 0; $isend(); sleep(3); }}
具体是这样的
function sendemail($address,$toname,$info){ 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($address, $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 = $info; $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; }}
在另外一个文件会循环调用它进行发送:
for($i = 0; $icharset = 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 = 'xxx';//发件人邮箱 (固定) $mailer->password = 'xxx';//发件人密码(固定) $mailer->setfrom($sendemail,$sendemail);收件人 张三,李四,王二,麻子 $mailer->addaddress($sendmail); $mailer->subject =$title; $mailer->msghtml($remark); for($i = 0; $isend(); sleep(3); } }

调用:
sendemail($sendmail,$title,$remark);
不是的,sendemail函数中只发送一次的
是外面调用sendemail,调用了10次
即:给张三发一封,给李四发一封,……,总共发了10封的
不是的,sendemail函数中只发送一次的
是外面调用sendemail,调用了10次
即:给张三发一封,给李四发一封,……,总共发了10封的
(1)当用户点击给张三发送邮件,程序开始自动发送,并将发送是否成功返回
(2)用户再次点击李四发送邮件,同上。
按照你的需求,你是点击谁,就给谁发邮件,那你要for循环10次干嘛?
直接调用sendemail(xx,xx,xx)封装的方法不就是的。
对是的。就是点击谁,就给谁发邮件,但是点击上三四个人就发送失败了。
我是为了测试发送失败的原因,自己写了个循环调用,看看是哪里出错了。结果循环中只能第一次成功,后面的都失败了。。。。。
你是想这么做?
发给张三,邮件发送10次?
发送李四,邮件发送10次?
你如果要这么做,干嘛不按照我上面写的方法,直接传递参数呢?
function sendemail($sendmail,$title,$remark){ include phpemail/phpemail.class.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 = 'xxx';//发件人邮箱 (固定) $mailer->password = 'xxx';//发件人密码(固定) $mailer->setfrom($sendemail,$sendemail);收件人 张三,李四,王二,麻子 $mailer->addaddress($sendmail); $mailer->subject =$title; $mailer->msghtml($remark); for($i = 0; $isend(); sleep(3); } }

调用:
sendemail($sendmail,$title,$remark);
布局好界面,按照我说的这个,你测试下。
(1)我用你的代码,注释掉$mailer->smtpsecure = 'ssl';这句才能连接邮箱服务器成功
(2)同样的问题,结果:
点击给李三发送,返回成功;
继续点击给张三,返回成功;
……
第四次失败
第五次失败
……
平均发送5封有2封失败,原因和我之前的一样通不过验证
一般的,应处理一下可能出现的错误
if(!$mailer->send()){ echo 邮件发送失败. ; echo 错误原因: . $mail->errorinfo; exit; //如果这里不是退出,而是条件重入呢?}
调试代码要有耐心!
别人只能给你一个思路,对不对,得由你验证
(1)我用你的代码,注释掉$mailer->smtpsecure = 'ssl';这句才能连接邮箱服务器成功
(2)同样的问题,结果:
点击给李三发送,返回成功;
继续点击给张三,返回成功;
……
第四次失败
第五次失败
……
平均发送5封有2封失败,原因和我之前的一样通不过验证
打印出错误信息,自己分析下。 每次发送完一次,关闭一次。
查看phpmailer属性。
使用 smtpclose();方法就可以了。
每次发送完一次,关闭一次。
查看phpmailer属性。
使用 smtpclose();方法就可以了。
ok,解决
每次发送完一次,关闭一次。
查看phpmailer属性。
使用 smtpclose();方法就可以了。
ok,解决
还有可能是重复包含了邮件类
循环函数 sendemail 中 包含语句include 改为 include_once
其它类似信息

推荐信息