Send an email to all users in a database

How does one go about sending emails to every user within a database without being flagged by any spam filters? I am assuming you would have to send the emails throughout the day in bursts over the course of a month or less. Of course, right off the bat, I can get all users email addresses with a simple query select 'email' from 'users' but implementing the proper way to do such a task is where I seem to be lost... In the past I have used services such as sendgrid, but what guidelines do these companies follow in order to stay clear of all spam filters?

3

3 Answers

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.");
}

Updates:As mysql_* deprecated please use mysqli_* instead of all functions.

while($row = mysqli_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.");

And

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

Please read the manual of mysqli_*.

6

Once you have your result set, you can loop through and call mail() on each 1. Let's hope that your server can handle this.

foreach($result as $user) { mail($user->email,"My subject",$msg);
}
2

I used PHPMailer to send emails. It's easy. Simply iterate all emails from DB and send using this library.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like