Php send batch email

Possible duplicate:
Sending bulk email using PHP

I have a PHP script that sends an individual letter to all users of my database, for example a monthly / weekly newsletter.

The code I use is as follows:

$subject = $_POST['subject']; $message = $_POST['message']; // Get all the mailing list subscribers. $query = $db->prepare("SELECT * FROM maildb"); $query->execute(); // Loop through all susbcribers, and send and individual email. foreach ($query as $row) { // Setting maximum time limit to infinite. set_time_limit(0); $newMessage = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body>'; // Search for the [unsubscribe] tag and replace it with a URL for the user to unsubscribe $newMessage .= str_replace("[unsubscribe]", "<a href='".BASE_URL."unsubscribe/".$row['hash']."/".$row['email']."'>unsubscribe</a>", $message); $newMessage .= '</body></html>'; $to = $row['email']; // Establish content headers $headers = "From: info@domain.com "."\n"; $headers .= "Reply-To: bounce@domain.com "."\n"; $headers .= "X-Mailer: PHP v.". phpversion()."\n"; $headers .= "MIME-Version: 1.0"."\n"; $headers .= "Content-Type: text/html; charset=iso-8859-1"."\n"; $headers .= "Content-Transfer-Encoding: 8bit;"; mail($to, $subject, $newMessage, $headers); // Send email to each individual user } 

This code works fine with a REALLY small database ... I recently populated my test db with 200k + users, and obviously this script crashes, goes out of memory and dies ...

I know this is a bad way to send so many emails, which is why I would like to ask you for much more efficient ways to do this!

Thank you very much!

+4
source share
5 answers

See this URL: -

Sending bulk email using PHP

Firstly, using the mail () function that comes with PHP is not an optimal solution. It is easily flagged as spam, and you need to adjust the header to make sure that you are sending HTML messages correctly. As for whether the code snippet will work, it will, but I doubt that you will get the HTML code inside it correctly, specifying additional headers

I suggest you take a look at SwiftMailer , which supports HTML, supports different types of mime and SMTP authentication (which is less likely to mark your mail as spam).

0
source

The timeout you are experiencing is due to Apache and PHP execution limits.

You need to run it as a CLI application using set_time_limit(0);

php /path/to/app/script.php is something like this right in the console.

If you do not have access to SSH, run it using shell_exec as follows:

 shell_exec("php /path/to/app/script.php > /dev/null 2>/dev/null &"); 

This ensures that the call to the script that calls it does not hang until completion.

+2
source

You can configure the Cron scheduler to run every minute or some interval. Each execution of the script will select several records from the database and delete them from db or set as inactive. Send mail to a small piece and let the script die. Another cron call will select a few other entries and die. You can also use the exec () function.

+1
source

Make them in packages, so you only send a few instances at a time. Your database has a field in which it checks to see if they sent the newsletter this month, and check it when the newsletter is sent to this user, then you can just continue to run the script until it is sent to everyone.

+1
source

I would prefer to implode users in a variable and send an email at a time

 $to_array = array(); foreach ($query as $row) { $to_array[] = $row['email']; } $to = implode(', ', $to_array); //do your email stuff here mail($to, $subject, $newMessage, $headers); // Send email to all users at once 

hope this helps :-)

0
source

All Articles