简体   繁体   English

在while循环中发送电子邮件

[英]Sending email in while loop

When I try to send email from the while loop with PHPMailer, it sometimes sends 2, sometimes 3 copies of the same email (it is like random) to each recipient. 当我尝试使用PHPMailer从while循环发送电子邮件时,有时会向每个收件人发送2份,有时是3份相同电子邮件的副本(就像随机的一样)。

Here is my code. 这是我的代码。 Do you think it has problems? 您认为它有问题吗?

 $list = $_POST['list'];
    $items = rtrim($_POST['items'],",");
    $query = "SELECT * FROM `mail` WHERE `ID` IN ($items)";
    $result = mysql_query($query);
    $from = "donotreply@mysite.net";
    $fromname = "mysite";

    $mail = new PHPMailer(true); 

    $mail->IsSendmail(); 

    $mail->From       = $from;
    $mail->FromName   = $fromname;

    $mail->Subject  = "Your subscription was confirmed";

while ($row = mysql_fetch_array ($result))
{
    // HTML body
    $body .= "<p>Hi ". $row['name'] ." <br /><br />";
    $body .= "Your subscription request to " . $l_name ."'s list was confirmed.<br /><br />";
    $body .= "Thank You !<br /><br />";

    // Plain text body (for mail clients that cannot read HTML)
    $text_body  = "To view the message, please use an HTML compatible email viewer!";

    $mail->Body    = $body;
    $mail->AltBody = $text_body;
    $mail->AddAddress($row['email']);


    $mail->Send();
    $mail->ClearAddresses();

}

Do you think should I put that mail->send(); 你认为我应该把那个mail->send(); out of the while loop and get all the emails from an array? 走出while循环并从数组中获取所有电子邮件?

Or do you think it is the problem with the MySQL query? 还是您认为这是MySQL查询的问题?

Edit: I checked database, no problem about database but i figured out that (lets say there are 2 mail in the array) it sends first email normally but second one goes with dublicated $body variable, i mean it sends $body variable dublicated. 编辑:我检查了数据库,关于数据库没有问题,但是我发现(让我说数组中有2封邮件)它正常发送第一封电子邮件,但是第二封邮件带有$ body变量,这意味着它发送了$ body变量。

FIX: hey, I done, I just added $body = ""; FIX:嘿,我完成了,我只添加了$body = ""; and it works perfect now! 现在可以完美使用!

只需在查询中输入"SELECT DISTINCT"就不会再出现数据库问题。

I think more than likely to be duplicate data in the database. 我认为很可能是数据库中的重复数据。

Also i'm concerned about the lack of validation (or non at all) on the POST array. 我也担心在POST数组上缺少验证(或根本没有验证)。

May be worth you looking at this: 也许值得您关注一下:

cleaning $_POST variables 清理$ _POST变量

Update: While you can just use DISTINCT on the query i would question how the duplicates got there in the first place and look at that as a seperate issue. 更新:虽然您可以在查询上使用DISTINCT,但我会质疑重复项是如何首先到达那里的,并将其视为单独的问题。

You mentioned the fix at the end of your question, but here's why that makes a difference: 您在问题的结尾提到了修复程序,但这就是为什么会有所不同的原因:

The .= operator appends to the existing value, whereas = overwrites it. .=运算符会附加到现有值上,而=覆盖它。 At the end of the first iteration and start of the second, $body contains the email body, during the second iteration, you then append to the existing value. 在第一次迭代的末尾和第二次迭代的开始, $body包含电子邮件正文,在第二次迭代期间,您将追加到现有值。 Each time the loop executes, you add another copy to the end of the email. 每次执行循环时,您都会在电子邮件末尾添加另一个副本。 As you said, setting $body = "" fixes it because it empties the body of the email. 如您所说,设置$body = ""解决此问题,因为它会清空电子邮件的正文。

Another way to fix it would be to make the first assignment = instead of .= : 解决该问题的另一种方法是进行第一个赋值=而不是.=

while ($row = mysql_fetch_array ($result))
{
  // HTML body
  $body = "<p>Hi ". $row['name'] ." <br /><br />"; // This line only has '='
  $body .= "Your subscription request to " . $l_name ."'s list was confirmed.<br /><br />";
  $body .= "Thank You !<br /><br />";

  // etc...
}

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

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