Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an email queue in PHP?

Tags:

php

email

mime

I am currently creating a quote system which works like this:

User submits data through form --> data sent to db --> pdf generated --> email sent

I am using the htmlmimemail5 library to do this and the email generated is sent to a mail exchange on a separate server on the local network.

Sadly the negative side of this is that the php script takes around a minute to fully execute as the script waits for confirmation from the MX that the email has been sent or has failed.

So what I was hoping would be possible is to have a separate PHP application that handles all the email processing and means that the user does not have to wait a minute and can instead jump to the next page where they can view information etc.

This application would happily work away on an email queue that has been built up from user process requests and if the queue is empty would possibly shut down?

I'm guessing it would need to be some sort of email queue (like a print queue).

However, because I am a junior PHP dev and all the other devs are too busy to babysit, I am completely lost and do not even know where to begin from or if this is even the right strategy?

More information:

  • Server is a BSD jail
  • Apache 2.2 /PHP5

All internal and external emails are routed through POSTINI, which adds a delay as well.

Any ideas or suggestions welcome! :)

like image 560
tomaytotomato Avatar asked Dec 14 '11 16:12

tomaytotomato


1 Answers

Yep, this is pretty straightforwards to implement. Rather than immediately sending, save the e-mail data to a database. Have a script (running every minute via cron, or better yet a daemonized PHP script) that takes the queued e-mails and sends them out.

Be sure to have flags in your database to indicate progress, i.e. a column for "sending" and one for "sent", so your script knows not to try to repeatedly send one that's already in the process of being sent.

like image 99
ceejayoz Avatar answered Sep 21 '22 14:09

ceejayoz