具体是这样的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 = "[email protected]"; // 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";
{
SendEmail();
sleep(20);
}
<?php
set_time_limit(0);
ini_set("max_execution_time", "18000000");
include 'PHPMailer/class.phpmailer.php';
$sendmail = '';//收件人
$title='我要发邮件';
$re='这是邮件内容';
$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($re);
for($i = 0; $i< 10; $i++){
$mailer->send();
sleep(3);
}
?>我试过是没有问题的。
我想实现的是:当用户点击时才发送邮件,发送的邮件始终是同一个,但是收件人的邮箱地址是不一样的。
(1)当用户点击给"张三"发送邮件,程序开始自动发送,并将发送是否成功返回
(2)用户再次点击"李四"发送邮件,同上。
我测试时,发送失败的几率很大,调试结果是:SMTP -> ERROR: AUTH not accepted from server: 503 Error: already authenticated,(原因是发件箱始终是一个,可能上次已经验证过了)
求解决办法。。
难道给你一个phpemail类,不知道做事?
$sendmail = '';//收件人 张三,李四,王二,麻子
$title='我要发邮件';
$re='这是邮件内容';
$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($re);
(1)我将发送邮件写成是一个函数的,假设为SendEmail($address);
(2)外部调用假设为for($i = 0; $i< 10; $i++){
SendEmail($address);
sleep(3);
}结果只有第一封能发出去。。
如下:function SendEmail($sendmail,$title,$re){
include "phpemail/phpemail.class.php";
$sendmail = '';//收件人 张三,李四,王二,麻子
$title='我要发邮件';
$re='这是邮件内容';
$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($re);
for($i = 0; $i< 10; $i++){
$mailer->send();
sleep(3);
}}
{ 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 = "[email protected]"; // 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,$re){
include "phpemail/phpemail.class.php";
$sendmail = '';//收件人 张三,李四,王二,麻子
$title='我要发邮件';
$re='这是邮件内容';
$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($re);
for($i = 0; $i< 10; $i++){
$mailer->send();
sleep(3);
}
}
调用:
SendEmail($sendmail,$title,$re);
是外面调用SendEmail,调用了10次即:给张三发一封,给李四发一封,……,总共发了10封的
(1)当用户点击给"张三"发送邮件,程序开始自动发送,并将发送是否成功返回
(2)用户再次点击"李四"发送邮件,同上。
按照你的需求,你是点击谁,就给谁发邮件,那你要for循环10次干嘛?
直接调用sendemail(xx,xx,xx)封装的方法不就是的。
(2)同样的问题,结果:
点击给李三发送,返回成功;
继续点击给张三,返回成功;
……
第四次失败
第五次失败
……平均发送5封有2封失败,原因和我之前的一样通不过验证
if(!$mailer->Send()){
echo "邮件发送失败. <p>";
echo "错误原因: " . $mail->ErrorInfo;
exit; //如果这里不是退出,而是条件重入呢?
}调试代码要有耐心!
别人只能给你一个思路,对不对,得由你验证
查看phpmailer属性。
使用 Smtpclose();方法就可以了。
OK,解决
还有可能是重复包含了邮件类
循环函数 SendEmail 中 包含语句include 改为 include_once
OK,解决$mail->Send();
$mail->smtpClose();
这样写不行.请问你怎么写的