I have a table below in MYSQL
Mmeber_Email---------------Member_Name------Data---
mike01@yahoo.com -------- Mike ---------- 100 ------
jacknick@gmail.com --------Jack -----------50 ----
jillwag@hotmail.com -------Jill ---------- 75 ------
jnash@gmail.com -------- John ---------- 10 ------
Now, I have managed to extract the data from the database using PHP. but i don't know how to email the data of each member on their respective email using PHP Mail function. How should i tackle the problem of sending emails to multiple recipients using the PHP mail function below i.e Mike Should get the email containing the message 100, Jack 5 and so on..
<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = //data extracted from database using a common query comes here
$headers = "From: webmaster@example.com";
mail($to,$subject,$txt,$headers);
?>
You would need to loop through the results returned from MySQL. Here is an example:
while($row = mysqli_fetch_array($results)) { // start loop
$to = $row['member_email'];
$subject = 'the subject';
$message = "Dear ". $row['member_name'] . "
";
$message .= "Here is your data: " . $row['member_data'] . "
";
$headers = 'From: webmaster@example.com' . "
" .
'Reply-To: webmaster@example.com' . "
" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
The data from your database is contained in the $row
array setup by the while{}
loop.
Make sure to pay attention to the details for mail in the docs.
Set $to
to a comma separated list of all the recipients:
$to = "somebody1@example.com, somebody2@example.com, somebody3@example.com";