PHPmailer重复电子邮件问题 - 条件语句与$ mail-> Send() [英] PHPmailer duplicate email issue - conditional statement with $mail->Send()

查看:356
本文介绍了PHPmailer重复电子邮件问题 - 条件语句与$ mail-> Send()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题,PHPmailer(5.1版),我正在努力解决。我在这里看到很多好的反馈,所以我以为我会试一试。我发现当我尝试使用基于 $ mail-> send()的条件语句创建一个自定义的确认消息时,我收到重复的电子邮件。我可以用phpmailer下载的通用testemail.php脚本来复制它。以下是代码:

I came across a bizarre issue with PHPmailer (version 5.1) that I'm trying to work around. I've seen quite a bit of good feedback on here, so I thought I would give it a try. I've found that when I attempt to create a customized confirmation message with a conditional statement based on $mail->send(), I receive duplicate emails. I can duplicate it with the generic testemail.php script that comes with the phpmailer download. Here's the code:


require'../class.phpmailer.php';

try {

$ mail = new PHPMailer(true); //新实例,启用异常

$ mail-> SMTPDebug = 1;

$ mail-> IsSMTP(); //告诉班级使用SMTP

$ mail-> SMTPAuth = true; //启用SMTP身份验证

$ mail-> Port = 25; //设置SMTP服务器端口

$ mail-> Host =mail.domain.com; // SMTP服务器

$ mail-> Username =username; // SMTP服务器用户名

$ mail-> Password =password; // SMTP服务器密码

require '../class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "mail.domain.com"; // SMTP server
$mail->Username = "username"; // SMTP server username
$mail->Password = "password"; // SMTP server password

$mail->IsSendmail();   
$mail->From       = "example_from@domain.com";  
$mail->FromName   = "First Last";  

$to = "example@domain.com";  
$mail->AddAddress($to);  

$mail->Subject  = "PHP Mailer test";  

$message = "This is a test. \n";  
$mail->Body = $message;  

$mail->Send();  
if ($mail->Send()) {  
    echo 'Message has been sent.';  
} else {  
    echo "Mailer Error: " . $mail->ErrorInfo;   
}  

} catch(phpmailerException $ e){

echo $ e-> errorMessage();

}

} catch (phpmailerException $e) {
echo $e->errorMessage();
}

上述代码回应已发送消息确认,但随后发送两封电子邮件。如果我注释掉 $ mail-> send()行,我仍然收到消息已发送确认,只收到一条消息。如果我删除条件语句,并留下 $ mail-> send()行注释掉,不会发送任何电子邮件。

The above code echoes the "Message has been sent" confirmation, but then sends two emails. If I comment out the $mail->send() line, I still receive the "message has been sent" confirmation and only get one message. If I remove the conditional statement and leave the $mail->send() line commented out, no email is sent.

为什么添加条件语句会导致发送电子邮件而不调用 $ mail-> send()方法?添加自定义确认消息的正确方法是什么?

Why does adding a conditional statement cause an email to be sent without calling the $mail->send() method? What is the proper way of adding a customized confirmation message?

推荐答案

当您将 $ mail-> Send()您有条件的,您实际上正在再次呼叫,发送另一条消息,并检查是否发送了第二条消息。

When you put $mail->Send() in your conditional, you're actually calling it again, sending another message, and checking if that second message was sent.

如果您只是保持

if ($mail->Send()) {  
    echo 'Message has been sent.';  
} else {  
    echo "Mailer Error: " . $mail->ErrorInfo;   
}

并摆脱原始的无条件发送电话,你应该没事。

and get rid of the original, unconditional Send call, you should be fine.

或者,如果您更清楚,或者您需要在其他地方进行某些处理,这取决于邮件是否成功发送,您可以基本上等效: p>

Alternatively, if it's clearer for you or if you need to do some processing elsewhere that depends on whether the message was successfully sent, you could do the essentially equivalent:

$status = $mail->Send();
if ($status) {  
    echo 'Message has been sent.';  
} else {  
    echo "Mailer Error: " . $mail->ErrorInfo;   
}

这篇关于PHPmailer重复电子邮件问题 - 条件语句与$ mail-> Send()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆