Note that there are some explanatory texts on larger screens.

plurals
  1. POSending email in while loop
    primarykey
    data
    text
    <p>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.</p> <p>Here is my code. Do you think it has problems?</p> <pre><code> $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-&gt;IsSendmail(); $mail-&gt;From = $from; $mail-&gt;FromName = $fromname; $mail-&gt;Subject = "Your subscription was confirmed"; while ($row = mysql_fetch_array ($result)) { // HTML body $body .= "&lt;p&gt;Hi ". $row['name'] ." &lt;br /&gt;&lt;br /&gt;"; $body .= "Your subscription request to " . $l_name ."'s list was confirmed.&lt;br /&gt;&lt;br /&gt;"; $body .= "Thank You !&lt;br /&gt;&lt;br /&gt;"; // Plain text body (for mail clients that cannot read HTML) $text_body = "To view the message, please use an HTML compatible email viewer!"; $mail-&gt;Body = $body; $mail-&gt;AltBody = $text_body; $mail-&gt;AddAddress($row['email']); $mail-&gt;Send(); $mail-&gt;ClearAddresses(); } </code></pre> <p>Do you think should I put that <code>mail-&gt;send();</code> out of the while loop and get all the emails from an array?</p> <p>Or do you think it is the problem with the MySQL query?</p> <p>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.</p> <p>FIX: hey, I done, I just added <code>$body = "";</code> and it works perfect now!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload