2

My script sends notification emails of a new comment, this could be to 50 members and 50 emails need to be sent, which could take 20 seconds which is way to long for the user to wait! What's the best way in PHP to do this, is there a way to do asynchronously?

Jake
  • 3,326
  • 7
  • 39
  • 59
  • That is too long! You can schedule a CRON to run a PHP script periodically and send the emails. – Jared Farrish Dec 30 '12 at 18:48
  • Take a look at: http://stackoverflow.com/questions/9595181/is-php-mail-a-good-option-for-mass-mailing – Terry Harvey Dec 30 '12 at 18:49
  • Also, if you're sending emails with attachments you'll want to batch them in small numbers. Symfony's Switftmailer has [antiflood control](http://swiftmailer.org/docs/plugins.html#antiflood-plugin) as well as [alternative transports](http://swiftmailer.org/docs/sending.html#quick-reference-for-sending-a-message). Note, you do not need the Symfony framework to use Swiftmailer (or it's bundles/components, if they were written to spec). Oh, there's also a Logger plugin included so you can track success. – Jared Farrish Dec 30 '12 at 18:59
  • (And a web-based [cron table manager...](https://github.com/michelsalib/BCCCronManagerBundle)) – Jared Farrish Dec 30 '12 at 19:07

5 Answers5

3

A simple way might be to store the necessary information (email addresses, content) in a database and have a batch process run every minute or so with a cron job. The batch process can query the database for pending emails and, if any are to be sent, go through them and then delete the database entries.

Charles Burns
  • 10,310
  • 7
  • 64
  • 81
2

is there a way to do asynchronously?

Yes, there is!

exec('wget PATH_TO_YOUR_SCRIPT_THAT_SENDS_THE_NEWSLETTER > /dev/null &');

Note that the database alternative is a pretty good one too. But this should work too if you're on Linux (and doesn't require a database).

mbinette
  • 5,094
  • 3
  • 24
  • 32
2

I'd use something like RabbitMQ. Your website acts like a producer sending the email requests to Rabbit; then have a consumer running that processes the requests from Rabbit.

Advantages - if your consumer falls over then when you restart it will pickup from where it left off (last acknowledged request).

Eden Akhavi
  • 101
  • 2
  • Especially if you need the emails to get through as often as possible. Sending emails is tricky with all the spam defense out there. – Jared Farrish Dec 30 '12 at 19:10
1

Indeed it can be done asynchronously.

The simplest way is to insert the email data into a database rather than actually running the emails, and then have a cron job that periodically actually sends the emails.

There are of course other ways too, but that will probably be the most straight forward.

Corbin
  • 33,060
  • 6
  • 68
  • 78
0

You can use cURL POST to start an asynchronous script. Set the timeout to a short period so your script can resume after the POST request has been made. You can set the email information in the POST request or store it in a data base table.

Ray Paseur
  • 2,106
  • 2
  • 13
  • 18