Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial phpmailer循环发送邮件失败

phpmailer循环发送邮件失败

Jun 23, 2016 pm 01:46 PM

使用的是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;}
Copy after login


回复讨论(解决方案)

单次成功的话程序是没有问题的,因为你用的是126的邮件服务器那它肯定不会让你不间断的循环发送的,所以你可以再发送完成一封之后让程序sleep几秒钟

一般来说,像qq、163、126等邮箱,他们不会允许你连续发送的,所以如果你要循环发送的话,可以sleep 几秒,但每发一封邮件sleep几秒,效率肯定不高,也可以同时给多个用户发送,然后sleep几秒

加了sleep后,测试也是没有起作用,麻烦哪位大神能给个循环发送测试通过的例子,急用

for($i=0; $i<10; $i++){	SendEmail();	sleep(20);}
Copy after login

<?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);    }?>
Copy after login

我试过是没有问题的。

不好意思,这个是我没有表述清楚。
我想实现的是:当用户点击时才发送邮件,发送的邮件始终是同一个,但是收件人的邮箱地址是不一样的。
(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);
Copy after login

我编写的是传参数进来,然后再发送的。。。。
(1)我将发送邮件写成是一个函数的,假设为SendEmail($address);
(2)外部调用假设为

for($i = 0; $i< 10; $i++){        SendEmail($address);        sleep(3);    }
Copy after login

结果只有第一封能发出去。。。。。

那你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);    }}
Copy after login

具体是这样的

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;	}}
Copy after login


在另外一个文件会循环调用它进行发送:
for($i = 0; $i< 10; $i++){        //这里会获取到邮箱地址$address,收件人姓名$toname,发送的内容$info        SendEmail($address,$toname,$info);        sleep(3);    }
Copy after login


结果就会只有第一封发送成功,后边的都通不过验证

你是想这么做?
发给张三,邮件发送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);    } }
Copy after login
Copy after login


调用:
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);    } }
Copy after login
Copy after login


调用:
SendEmail($sendmail,$title,$remark);



布局好界面,按照我说的这个,你测试下。

(1)我用你的代码,注释掉$mailer->SMTPSecure = 'ssl';这句才能连接邮箱服务器成功
(2)同样的问题,结果:
点击给李三发送,返回成功;
继续点击给张三,返回成功;
……
第四次失败
第五次失败
……

平均发送5封有2封失败,原因和我之前的一样通不过验证

一般的,应处理一下可能出现的错误

if(!$mailer->Send()){    echo "邮件发送失败. <p>";    echo "错误原因: " . $mail->ErrorInfo;    exit; //如果这里不是退出,而是条件重入呢?}
Copy after login
调试代码要有耐心!
别人只能给你一个思路,对不对,得由你验证

(1)我用你的代码,注释掉$mailer->SMTPSecure = 'ssl';这句才能连接邮箱服务器成功
(2)同样的问题,结果:
点击给李三发送,返回成功;
继续点击给张三,返回成功;
……
第四次失败
第五次失败
……

平均发送5封有2封失败,原因和我之前的一样通不过验证


打印出错误信息,自己分析下。

每次发送完一次,关闭一次。
查看phpmailer属性。
使用 Smtpclose();方法就可以了。

每次发送完一次,关闭一次。
查看phpmailer属性。
使用 Smtpclose();方法就可以了。



OK,解决


每次发送完一次,关闭一次。
查看phpmailer属性。
使用 Smtpclose();方法就可以了。



OK,解决
还有可能是重复包含了邮件类
循环函数 SendEmail 中 包含语句include 改为 include_once 
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles