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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

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

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

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

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

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-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

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

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

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' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

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.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

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

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

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

See all articles