繁体   English   中英

通过SMTP发送两封电子邮件

[英]Two emails when sent via SMTP

我使用了两个PHP电子邮件脚本,并通过SMTP服务器路由它,尽管这样做会发送两个相同的电子邮件。

当我使用mail()时,这不会发生,但是我宁愿使用SMTP。

为什么会发生这种情况的任何想法?

如果您多次设置“收件人”和/或“收件人”标头,则SMTP服务器会将其解释为单独的电子邮件地址,因此您将收到多封电子邮件。

我建议使用PEAR Mail类。 使用非常简单,可以为您处理许多工作。 它支持包括SMTP在内的多个后端。 同样,如果要扩展类以发送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!
    }
}   

到目前为止,这是当前功能

因此,如果仅使用PHPMailer而不编辑其代码,那不是脚本的错。 也许检查您的SMTP服务器的配置?

根据您的代码,如果是错误的类,则希望两次获得“ Message Sent OK”(消息发送正常)的信息(不过我看不出为什么会发生这种情况)。 如果您不这样做,那么我将在看您的SMTP服务器(也许通过致电支持人员)。

我假设您在这种情况下已禁用Reply-to以将其排除在外? 注意:我不建议这样做会影响任何事情(除了您可能被归类为垃圾邮件之外)。

顺便说一句,我前段时间从PHPMailer移到Swift Mailer ,并且从没有回过头。 如果您不能从支持中获得任何快乐,那么我将至少尝试使用Swift Mailer进行测试。

我同意da5id所说的话,为什么不删除第二条错误消息。 此外,您是否检查过接收方,他们是否真的收到2条消息?

暂无
暂无

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

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