Best Methods to handle email notifications to users

So I was wondering what others use to handle sending emails from PHP to their users. My concerns are mainly performance and avoiding my emails going to the user’s spam folder.

Right now I am sending notifications to my users by connecting to gmail/smtp. I notice, however, that sometimes my emails end up in my spam folder when I receive the emails myself. My other issue is that sometimes a whole bunch of notifications are sent out at once, it can be very slow. Especially since the notification each user gets is customized to them, so I can’t just bcc a bunch of people on one email.

Does anyone have any suggestions outside of gmail/smtp. I would prefer not to use a third party/web service if possible, but would definitely consider it if the benefits are worth it.

Thanks in advance.

There are a couple of things to take into account:

  1. try building a mail queue, that way you can generate multiple e-mails at once, without trying the send them right away. They just wait in the queue until you’re ready to send them

  2. write a mail processor, which is started from a cron job every minute or so, which checks the queue to see if anything needs to be sent. If there are mails in the queue, grab a bunch of them (say, 10), and send them. Use a library like Swift to send them, it makes sure your mails are formatted properly etc. You’ll still need to use SMTP, of course.

  3. set up SPF records, that goes a long way when trying not be flagged as spam: SPF: Project Overview

Good idea on the queue and cron job. I think I will do that. Hadn’t heard of the SPF project, so I will look into that too. Thanks!

Great post Alex; excellent advice.

Love it.

:slight_smile:

Thanks, it’s my favourite way of working with e-mail actually.

For instance, at work every single mail the system sends out goes through the queue. That’s pretty important, because it means I can stop mails from being sent by simply updating the crontab or even with a single click on a button in our CMS.

Plus, as everything gets stored in the queue and is flagged as sent when it’s sent (as opposed to being deleted), we have a record of every mail sent out (as we’re dealing with financial information, that’s a pretty important part of the audit trail).

The only disadvantage is that you might have to wait a short while before you get your mail. Easily solved though:

Probably not the best solution, but hey, it works :slight_smile: