I am trying to send emails using PHP using data from a database. The issue I am having is that the emails are being combined for each subsequent result match. So if I have 3 users that have the same match criteria, Yes/Yes for example, the first email is clean, the 2nd email contains the contents of the first then second email and the third recipient contains the first, second then third. I am not sure why it is combining messages, but obviously would like it to stop. :-)
<?php
$servername = "intentionally empty";
$username = "intentionally empty";
$password = "intentionally empty";
$dbname = "intentionally empty";
$tblname = "intentionally empty";
$email_to = "";
$email_subject = "";
$email_message = "";
$email_from = "";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Unable to Connect to '$dbhost' - Please email support@domain.com");
$sql = "SELECT * FROM $tblname WHERE Active='Yes' AND Reqested='Yes'";
$result = mysqli_query ($conn, $sql);
if (!mysqli_query($conn, $sql))
{
echo "Error: " . mysqli_query_error();
}
while($row = mysqli_fetch_array($result)){
$first_name = $row['First'];
$last_name = $row['Last'];
$dob = $row['DOB'];
$variableA = $row['VarA'];
$variableB = $row['VarB'];
$email_to = $row['Email'];
$email_from = "support@domain.com";
$email_subject = "Reminder: Requested information for $dob - $varA";
$email_intro .= "Hello $first_name $last_name,
";
$email_intro .= "
";
$email_intro .= "This is your monthly request reminder that for $dob - $varA
";
$email_intro .= "
";
$email_var .= "If you have already sent your updates then you can disregard.
";
$email_end .= "
";
$email_end .= "Please let us know if you have any question.
";
$email_end .= "
";
$email_end .= "Thank you,
";
$email_end .= "Management.
";
$email_message .= "$email_intro$email_var$email_end";
// create email headers
if (@mail($email_to, $email_subject, $email_message, 'From:' . $email_from));
echo 'Email sent to: ' . $email_to. '<br>';
}
?>