PHPMailer 发送双重电子邮件 [英] PHPMailer sending double emails

查看:37
本文介绍了PHPMailer 发送双重电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发从数据库获取客户电子邮件的应用程序,将它们存储在一个数组中,遍历该数组并分别向每个客户端发送电子邮件.

I am working on theapplication that fetches clients emails from DB, stores them in a array, loop through the array and send email to each client individually.

当 PHPMailer 向客户发送双封电子邮件时出现问题,并且在其中一封电子邮件收件人"字段中包含收件人地址,在第二封电子邮件中也有第二封客户电子邮件.

The problem occurred when PHPMailer is sending a double email to clients and also in and in one of emails "to" field contains both recepient address and in second email there is also a second clients emails as well.

我认为这是一个漏洞.

这是我的代码:$array = [ 0 => 'email@gmail.com' , 1 => 'email2@gmail.com' ];//示例

Here is my code: $array = [ 0 => 'email@gmail.com' , 1 => 'email2@gmail.com' ]; //Example

foreach ($array as $key => $value) {
    $mail->addAddress($value);
    if (!$mail->send())
    {
        throw new Exception($mail->ErrorInfo);

    } else
    {
        $mail->addAddress(NULL); //Attempt to unset 
        header('Location: ../public/email.php'); //Redirect to start page

    }

}

总结一下:运行时,它会向email@gmail.com"和email2@gmail.com"发送电子邮件.

So to sum it up: When this is run, it sends email to 'email@gmail.com' and 'email2@gmail.com'.

第一封电子邮件将收到一份电子邮件副本.第二封电子邮件将获得同一封电子邮件的两份副本,第一封将自己显示为收件人,第二封将自己显示为收件人 + 其他客户的电子邮件.

First email will get one copy of email. Second email will get two copies of the same email, first with showing itself as recipient, and second one with itself recipient + other clients email.

我只用 2 个获取的客户端对此进行了测试,我猜还会有更多的重复.谢谢各位!

I've tested this with only 2 fetched clients, with even more I guess there will be even more repetitions. Thank you guys!

推荐答案

这里有两个问题:

  • 您并没有从邮件中删除地址,因此当您添加第二个地址时,第一个地址仍然存在,并且双方都会看到另一个地址.
  • 您在循环中使用 header 重定向而不终止脚本.这可能会导致重定向后的代码运行,但无法保证运行多少和多长时间.
  • You are not removing the address from the mail, so when you add a second one, the first one is still there and both will see the other address.
  • You are using a header redirect in your loop without terminating your script. This can cause code after the redirect to run, but there is no guarantee for how much and how long.

您可以通过在循环结束时清除收件人来解决的第一个问题:

The first problem you can solve by clearing the recipients at the end of the loop:

$mail->ClearAllRecipients();

至于第二个问题,你不应该在循环内的任何地方重定向,当你在所有消息发送后重定向时,你应该使用 exit; 退出你的 scipt 以便之后没有任何东西被执行.

As for the second problem, you should not redirect anywhere inside the loop and when you redirect after all messages have been sent, you should exit your scipt using exit; so that nothing gets executed after that.

这篇关于PHPMailer 发送双重电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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