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

查看:30
本文介绍了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 workaround. 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); //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. 
";  
    $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();  
}

上面的代码回显了消息已发送";确认,但随后发送两封电子邮件.如果我注释掉 $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.

或者,如果它对您来说更清楚,或者如果您需要在其他地方进行一些处理,这取决于消息是否成功发送,您可以执行基本等效的操作:

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天全站免登陆