使用的是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

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft