Reputation: 486
I have this mailing list from php mailer examples: https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps
so I put the foreach loop within a while loop so it iterates through the entire table, I'm using a token array to replace the name and any dynamic content, the content is working, but it keeps putting the wrong name in the email.
The name and email are inserted into vac_mailing_system
table correctly. but when it's selected it seems to grab a random name and assign it to a random email address. So this makes me think there is an issue with the select statement.
$finish_table = $dbh->prepare("SELECT * FROM vac_mailing_system WHERE sent_status = false AND retry_attempts < 5");
if($finish_table->execute()) {
$finish_table->setFetchMode(PDO::FETCH_ASSOC);
}
while($row = $finish_table->fetch()) {
$vac_id = $row['vac_id'];
$vac_name = $row['vac_name'];
$vac_comp = $row['vac_comp'];
$full_name = $row['full_name'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = '';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Port = ;
$mail->Username = '';
$mail->Password = '';
$mail->setFrom('', 'Information');
$mail->addReplyTo('', ' Information');
$mail->Subject = "Information";
$mail->Body = strtr(file_get_contents('new_vac_email.html'), array('%full_name%' => $full_name, '%vac_id%' => $vac_id, '%vac_name%' => $vac_name, '%vac_comp%' => $vac_comp));
//Same body for all messages, so set this before the sending loop
//If you generate a different body for each recipient (e.g. you're using a templating system),
//set it inside the loop
//msgHTML also sets AltBody, but if you want a custom one, set it afterwards
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
//Connect to the database and select the recipients from your mailing list that have not yet been sent to
//You'll need to alter this to match your database
$mysql = mysqli_connect('host', 'user', 'pass');
mysqli_select_db($mysql, 'db_name');
$result = mysqli_query($mysql, "SELECT * FROM vac_mailing_system WHERE sent_status = false AND retry_attempts < 5 LIMIT 0, 50");
foreach ($result as $row) { //This iterator syntax only works in PHP 5.4+
$mail->addAddress($row['email_address'], $full_name);
if (!$mail->send()) {
echo "Mailer Error (" . str_replace("@", "@", $row["email_address"]) . ') ' . $mail->ErrorInfo . '<br />';
//Mark it as sent in the DB
mysqli_query(
$mysql,
"UPDATE vac_mailing_system SET sent_status = false, retry_attempts = +1 WHERE email_address = '" .
mysqli_real_escape_string($mysql, $row['email_address']) . "'"
);
break; //Abandon sending
} else {
echo "Message sent to :" . $full_name . ' (' . str_replace("@", "@", $row['email_address']) . ')<br />';
//Mark it as sent in the DB
mysqli_query(
$mysql,
"UPDATE vac_mailing_system SET sent_status = true WHERE email_address = '" .
mysqli_real_escape_string($mysql, $full_name) . "'"
);
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
$mail->clearAttachments();
}
sleep(60);
echo "emails sent";
}
Upvotes: 1
Views: 2172
Reputation: 41
Try this code:
// first we will create small helper function
function get_include_contents($filename, $variablesToMakeLocal) {
extract($variablesToMakeLocal);
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "You have an event today";
// rename your new_vac_email.html file to your new_vac_email.php
// create a data array which contains replace values
// here user_name is variable name in new_vac_email.php
//Adarsh hatkar is value which is shown in place of <?php echo ($user_name); ?>
$data = array('user_name' => 'Adarsh hatkar' );
$mail->Body = get_include_contents('new_vac_email.php', $data);
$mail->Send(); // send message
demo new_vac_email.php file code for example:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Email Confirmation</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body >
<p>Hello <?php echo ($user_name); ?> </P>
</body>
</html>
Upvotes: 0
Reputation: 4205
Try this code:
Yes, very easily with include and a short helper function:
function get_include_contents($filename, $variablesToMakeLocal) {
extract($variablesToMakeLocal);
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = get_include_contents('new_vac_email.php', $data); // HTML -> PHP!
$mail->Send(); // send message
Upvotes: 3