简体   繁体   English

通过SMTP发送两封电子邮件

[英]Two emails when sent via SMTP

I've used two PHP email scripts and routing it through my SMTP server, when I do this though it sends two of the same email. 我使用了两个PHP电子邮件脚本,并通过SMTP服务器路由它,尽管这样做会发送两个相同的电子邮件。

When I use mail() this doesn't happen, but I'd much rather use SMTP. 当我使用mail()时,这不会发生,但是我宁愿使用SMTP。

Any ideas why this may be occuring? 为什么会发生这种情况的任何想法?

If you're setting the 'To' and/or 'Recipient' header multiple times, the SMTP server could interpret that as separate e-mail address, thus you'll receive the multiple e-mails. 如果您多次设置“收件人”和/或“收件人”标头,则SMTP服务器会将其解释为单独的电子邮件地址,因此您将收到多封电子邮件。

I'd recommend using the PEAR Mail class. 我建议使用PEAR Mail类。 Very simple to use, and handles much of the work for you. 使用非常简单,可以为您处理许多工作。 It supports multiple backends including SMTP. 它支持包括SMTP在内的多个后端。 Likewise, if you want to expand your class to send HTML emails, the Mail_Mime class handles this very nicely, providing methods to set the plain-text body and the HTML body (in case the recipient doesn't support HTML). 同样,如果要扩展类以发送HTML电子邮件,则Mail_Mime类可以很好地处理此问题,提供设置纯文本正文和HTML正文的方法(以防收件人不支持HTML)。

function send_email($from, $fromname, $to, $subject, $body, $alt = '')
{
    require_once('class.phpmailer.php');
    //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

    $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

    $mail->IsSMTP(); // telling the class to use SMTP

    try
    {
        $mail->Host       = 'localhost'; // SMTP server
        $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
        //$mail->AddReplyTo($from, $fromname);
        $mail->AddAddress($to);
        $mail->SetFrom($from, $fromname);
        $mail->Subject = $subject;
        //$mail->AltBody = $alt; // optional - MsgHTML will create an alternate automatically
        $mail->MsgHTML($body);
        $mail->Send();
        echo 'Message Sent OK';
    }
    catch (phpmailerException $e)
    {
        echo $e->errorMessage(); //Pretty error messages from PHPMailer
    }
    catch (Exception $e)
    {
        echo $e->getMessage(); //Boring error messages from anything else!
    }
}   

That's the current function so far 到目前为止,这是当前功能

So if you're only using PHPMailer without editing it's code, it's not your script's fault. 因此,如果仅使用PHPMailer而不编辑其代码,那不是脚本的错。 Maybe check your SMTP server's configuration? 也许检查您的SMTP服务器的配置?

Based on your code, if it's the class which is at fault, you'd expect to get 'Message Sent OK' twice (I can't see why that would happen though). 根据您的代码,如果是错误的类,则希望两次获得“ Message Sent OK”(消息发送正常)的信息(不过我看不出为什么会发生这种情况)。 If you don't, then I'd be looking at your SMTP server (maybe via a call to support). 如果您不这样做,那么我将在看您的SMTP服务器(也许通过致电支持人员)。

I'm assuming you've disabled Reply-to to rule it out as a cause in this case? 我假设您在这种情况下已禁用Reply-to以将其排除在外? Note: I'm not suggesting that would affect anything (other than you likely being classified as spam). 注意:我不建议这样做会影响任何事情(除了您可能被归类为垃圾邮件之外)。

Incidentally, I moved from PHPMailer to Swift Mailer some time ago & have never looked back. 顺便说一句,我前段时间从PHPMailer移到Swift Mailer ,并且从没有回过头。 If you don't get any joy from support then I'd try at least testing with Swift Mailer. 如果您不能从支持中获得任何快乐,那么我将至少尝试使用Swift Mailer进行测试。

I agree with what da5id said, why dont you take the second error message out. 我同意da5id所说的话,为什么不删除第二条错误消息。 Further have you checked the receiver whether they REALLY get 2 messages? 此外,您是否检查过接收方,他们是否真的收到2条消息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM