I'm trying to use the phpemailer to send emails from my server. It's working fine except one thing:
<?php
$serverName = "xxx.x.x.xxx"; //serverName\instanceName
$connectionInfo = array( "Database"=>"test", "UID"=>"xxxxxx", "PWD"=>"xxxxxx");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sqll=sqlsrv_query($conn,"select bla from table");
//while($row=sqlsrv_fetch_array($sql))
// {
// $cnp=substr($row['bla'],-6);
// echo $bla;
// echo "<br>";
// }
$con=mysqli_connect("localhost","root","","database");
$sql=mysqli_query($con,"select * from table");
if(isset($_POST['submitted'])){
require 'phpmailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "xxxxxx";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = xxx;
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "xxxxxx";
//Password to use for SMTP authentication
$mail->Password = "xxxxxx";
//Set who the message is to be sent from
$mail->setFrom('xxxxxx');
//Set an alternative reply-to address
$mail->addReplyTo('xxxxxx');
//Set who the message is to be sent to
while($row=mysqli_fetch_array($sql))
{
echo $row['bla'];
echo "<br>";
$mail->AddBCC($row['bla2']);
//Set the subject line
$mail->Subject = 'text text';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//Replace the plain text body with one created manually
$mail->Body = "test test<br>link:<a href='http://localhost:8181/?cod=".$bla."'>click</a>";
$mail->IsHTML(true);
$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
}
?>
<form name="contact" method="post" action="">
<input type="submit" name="submitted" value="Submit">
</form>
The problem is: I'm trying to send an email with a different link to every email I've got in my database. I had put this part between the while:
$mail->AddBCC($row['email']);
//Set the subject line
$mail->Subject = 'text text';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//Replace the plain text body with one created manually
$mail->Body = "test test<br>link:<a href='http://localhost:8181/?cod=".$bla."'>click</a>";
$mail->IsHTML(true);
$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
The emails are being sent but for example if i have 3 emails in my table then to email1 is sent the email 3 times, to email2 is sent 2 times and the email3 is sent one time. Why is that? why does the email being sent 3 times to the first email in my table?