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 <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几秒
加了sleep后,测试也是没有起作用,麻烦哪位大神能给个循环发送测试通过的例子,急用
for($i=0; $i<10; $i++){ SendEmail(); sleep(20);}
<?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); }?>
我试过是没有问题的。
不好意思,这个是我没有表述清楚。
我想实现的是:当用户点击时才发送邮件,发送的邮件始终是同一个,但是收件人的邮箱地址是不一样的。
(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; $i< 10; $i++){ SendEmail($address); sleep(3); }
结果只有第一封能发出去。。。。。
那你SendEmail()怎么定义?如下: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; $i< 10; $i++){ $mailer->send(); 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; $i< 10; $i++){ //这里会获取到邮箱地址$address,收件人姓名$toname,发送的内容$info SendEmail($address,$toname,$info); sleep(3); }
结果就会只有第一封发送成功,后边的都通不过验证
你是想这么做?
发给张三,邮件发送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; $i< 10; $i++){ $mailer->send(); 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; $i< 10; $i++){ $mailer->send(); sleep(3); } }
调用:
SendEmail($sendmail,$title,$remark);
布局好界面,按照我说的这个,你测试下。
(1)我用你的代码,注释掉$mailer->SMTPSecure = 'ssl';这句才能连接邮箱服务器成功
(2)同样的问题,结果:
点击给李三发送,返回成功;
继续点击给张三,返回成功;
……
第四次失败
第五次失败
……
平均发送5封有2封失败,原因和我之前的一样通不过验证
一般的,应处理一下可能出现的错误
if(!$mailer->Send()){ echo "邮件发送失败. <p>"; echo "错误原因: " . $mail->ErrorInfo; exit; //如果这里不是退出,而是条件重入呢?}
别人只能给你一个思路,对不对,得由你验证
(1)我用你的代码,注释掉$mailer->SMTPSecure = 'ssl';这句才能连接邮箱服务器成功
(2)同样的问题,结果:
点击给李三发送,返回成功;
继续点击给张三,返回成功;
……
第四次失败
第五次失败
……
平均发送5封有2封失败,原因和我之前的一样通不过验证
打印出错误信息,自己分析下。
每次发送完一次,关闭一次。
查看phpmailer属性。
使用 Smtpclose();方法就可以了。
每次发送完一次,关闭一次。
查看phpmailer属性。
使用 Smtpclose();方法就可以了。
OK,解决
每次发送完一次,关闭一次。
查看phpmailer属性。
使用 Smtpclose();方法就可以了。
OK,解决
还有可能是重复包含了邮件类
循环函数 SendEmail 中 包含语句include 改为 include_once

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
