Viro Viro - 1 month ago 15
PHP Question

Send an email to all users in a database

Is there a way that I can send an email to all my database users? I can get all users email addresses with a simple query

select 'email' from 'users'


From here, is there a way for me to send a mass email to all my users from my server using
mail()
? If so, how can I go about doing this?

Answer

Yes you can do it. follow the given code.

while($row = mysql_fetch_array($result))
{
    $addresses[] = $row['email'];
}
$to = implode(", ", $addresses);
$subject = 'the subject';
$message = 'hello';
mail($to, "Your Subject", "A message set by you.", "If header information.");

First you have to gather all the email address as comma separated. after then just use a mail function to send mail to all user at the same time.

If you want to send mail separately then use:

while($row = mysql_fetch_array($result))
{
    $to = $row['email'];   
    $subject = 'the subject';
    $message = 'hello';
    mail($to, "Your Subject", "A message set by you.", "If header information.");
}
Comments