Help about Mail Body

Hi, in the code below, there are names and locations kept in database for the mail adresses, each mail adress could have many names and locations, in the code I wantto make if they enter email adress the querry searches the database and sends the name and location info to the that email adress, all are working but just it sends multiple mails to the user in each mail different info, but I want it to send all the info in 1 mail, all the different name infos in that mail body, waiting for ur help thx…

[php]<?php
include “baglan.php”;
require ‘admin/class.phpmailer.php’;
error_reporting(0);

$call=$_POST[‘email’];
$list=mysql_query(“select * from onaysiz where email=’$call’”);

while ($row=mysql_fetch_array($list)){

$name=$row[‘name’];
$location=$row[‘location’];

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = ‘smtp’;
$mail->SMTPAuth = true;
$mail->Host = ‘smtp.gmail.com’; // “ssl://smtp.gmail.com” didn’t worked
$mail->Port = 465;
$mail->SMTPSecure = ‘ssl’;// or try these settings (worked on XAMPP and WAMP)://
$mail->Port = 587;//
$mail->SMTPSecure = ‘tls’;

$mail->Username = “";
$mail->Password = "
”;
$mail->CharSet = “UTF-8”;
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.
$mail->From = “****************”;
$mail->FromName = “***********”;
$mail->addAddress(“gönderilecek mail adresi”,“User 1”);
$mail->addCC("",“User 2”);

$mail->Subject = "Hello ";

$mail->Body = " $name $location";

if(!$mail->Send())

echo "Message was not sent
PHPMailer Error: " .

$mail->ErrorInfo;

else echo “Message has been sent”;

}
?>[/php]

You need to pull the mailer stuff out of the while loop. Then concatenate a var with all the names and locations inside the while loop. Then you use the var in the $mail->Body.
Here is a reworked version.
[php]
$body = ‘’;
while ($row=mysql_fetch_array($list)){

$body .= 'Name: '.$row[‘name’].'
Location: '.$row[‘location’];
}

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = ‘smtp’;
$mail->SMTPAuth = true;
$mail->Host = ‘smtp.gmail.com’; // “ssl://smtp.gmail.com” didn’t worked
$mail->Port = 465;
$mail->SMTPSecure = ‘ssl’;// or try these settings (worked on XAMPP and WAMP)://
$mail->Port = 587;//
$mail->SMTPSecure = ‘tls’;

$mail->Username = “";
$mail->Password = "
”;
$mail->CharSet = “UTF-8”;
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.
$mail->From = “****************”;
$mail->FromName = “***********”;
$mail->addAddress(“gönderilecek mail adresi”,“User 1”);
$mail->addCC("",“User 2”);

$mail->Subject = "Hello ";

$mail->Body = $body;

if(!$mail->Send())
{ echo "Message was not sent
PHPMailer Error: " . $mail->ErrorInfo; }
else{ echo “Message has been sent”; }
[/php]
Also, you really should ALWAYS use {} for your if() and else{} stuff, cause without them the blocks will only run the first line after the if() line and not the other lines after that.

Sponsor our Newsletter | Privacy Policy | Terms of Service