Search code examples
phpemailperformancescalingapplication-design

Should I queue emails in a database before sending, instead of on the fly?


I'm developing a site that sends email notifications to users after certain events/user actions (tagged in a photo, added as a friend, etc.)

From my experience, sending emails from a web server can be slow. For instance when creating a new user I will send an email to confirm/welcome the account. However, there is short pause sometimes waiting for the page to load while it finishes sending the email.

My fear is that, if I ever have a problem connecting to a mail server or the mail server is overloaded, it will bottleneck my application/page loads

So my question is, what is the best way to send emails from a web application that sends emails on consistent basis?

Should I create a queue system, by storing email content in a database and then run scheduled cron jobs to process all the undelivered emails?

Or should I just send the email directly after a user completes an action/event (creating a member account, tagged in a photo, etc...)?


Solution

  • No. Ask your sysadmin to configure mail server. Correctly configured mail server should be faster then storing messages on database.

    Use local sending to avoid network delays. [mail function] sendmail_path = /usr/sbin/sendmail in php.ini

    It would work even if mail server is down (message will be queued).