Reputation: 61
I am trying to send email to multiple users using php but encounter some issues.i can send fine to the email address but it shows all the email addresses in the to field. I would like it to a loop and send the email to each of the user separately.
my php code is
here I am grabbing all info from MYSQL.
$r=mysql_query("SELECT * FROM user WHERE USER_ID='$user_id");
$r=mysql_fetch_array($r);
$emailfrom=$r['EMAIL_FROM'];
$emailpriority=$r['EMAIL_PRIORITY'];
$emailsubject=$r['EMAIL_SUBJECT'];
$emailto=$r['EMAIL_TO']; --> data stored here is the email addresses of the users.
it is also formatted already -> example: [email protected],[email protected],[email protected] etcc
email headers
$subject = "$emailsubject";
$mailer = "$emailfrom";
$headers = "From: $mailer \r\n";
$headers .= "Reply-To: $mailer\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Importance: $emailpriority\r\n";
mail($emailto, $subject, $message, $headers);
thank you so much in advance.
Upvotes: 0
Views: 1668
Reputation: 54
You can loop through as said by zeantsoi or you can use PHPMailer library and which suits your requirement better.
You can refer to the documentation here.
Upvotes: 0
Reputation: 26193
Start by using explode()
to parse each address in the string $emailto
as an member of an array. Since your string is comma-delimited, you'll want to pass the string ','
as the first argument:
$addresses = explode(',', $emailto);
This results in an array of addresses, which you can loop through and execute mail()
separately for each iteration:
foreach ($addresses as $recipient) {
mail($recipient, $subject, $message, $headers);
}
Upvotes: 1