通过SMTP发送两封电子邮件

时间:2021-06-26 18:14:31

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?

有什么想法可能会发生吗?

5 个解决方案

#1


So if you're only using PHPMailer without editing it's code, it's not your script's fault. Maybe check your SMTP server's configuration?

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

#2


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. Very simple to use, and handles much of the work for you. It supports multiple backends including 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).

我建议使用PEAR Mail类。使用非常简单,并为您处理大部分工作。它支持多种后端,包括SMTP。同样,如果要扩展类以发送HTML电子邮件,Mail_Mime类可以非常好地处理它,提供设置纯文本正文和HTML正文的方法(如果收件人不支持HTML)。

#3


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

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

#4


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). If you don't, then I'd be looking at your SMTP server (maybe via a call to support).

根据你的代码,如果它是错误的类,你希望两次得到“Message Sent OK”(我不明白为什么会发生这种情况)。如果你不这样做,那么我会看你的SMTP服务器(可能是通过支持电话)。

I'm assuming you've disabled Reply-to to rule it out as a cause in this case? 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. If you don't get any joy from support then I'd try at least testing with Swift Mailer.

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

#5


I agree with what da5id said, why dont you take the second error message out. Further have you checked the receiver whether they REALLY get 2 messages?

我同意da5id所说的,为什么不把第二条错误信息拿出来。你还检查了接收器他们是否真的得到2条消息?

#1


So if you're only using PHPMailer without editing it's code, it's not your script's fault. Maybe check your SMTP server's configuration?

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

#2


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. Very simple to use, and handles much of the work for you. It supports multiple backends including 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).

我建议使用PEAR Mail类。使用非常简单,并为您处理大部分工作。它支持多种后端,包括SMTP。同样,如果要扩展类以发送HTML电子邮件,Mail_Mime类可以非常好地处理它,提供设置纯文本正文和HTML正文的方法(如果收件人不支持HTML)。

#3


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

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

#4


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). If you don't, then I'd be looking at your SMTP server (maybe via a call to support).

根据你的代码,如果它是错误的类,你希望两次得到“Message Sent OK”(我不明白为什么会发生这种情况)。如果你不这样做,那么我会看你的SMTP服务器(可能是通过支持电话)。

I'm assuming you've disabled Reply-to to rule it out as a cause in this case? 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. If you don't get any joy from support then I'd try at least testing with Swift Mailer.

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

#5


I agree with what da5id said, why dont you take the second error message out. Further have you checked the receiver whether they REALLY get 2 messages?

我同意da5id所说的,为什么不把第二条错误信息拿出来。你还检查了接收器他们是否真的得到2条消息?