Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Building scalable emailing for PHP-based web projects

Sunday, May 22nd, 2016 by Servage

send-emailsHave you ever wondered how big websites handle email sending within their PHP applications? Popular websites can send hundreds of thousands, even millions of emails to their users each month. Instead of sending so many emails locally on a web server, it is recommended to use an infrastructure that is built specifically for sending emails. Even if you are not operating or coding a large website yet, it is considered good practice to have a scalable email infrastructure from the beginning. In this article we will take a look at some alternatives to the traditional mail() function for building a reliable and cost-effective email infrastructure.

Two types of email services

There are many reasons to send an email using PHP. They can be sign-up emails, marketing emails, important reminders and so on. Because of this, there are different types of email infrastructure providers. The two most common types are transactional email providers and subscription-based providers.

Subscription-based providers are designed for handling email marketing campaigns and sending a large amount of emails at once.

Transactional emails are emails that are sent when something happens inside an application. For instance, a user requests a password reset and the instructions to reset the user’s password are sent using a transactional email service. This is a common type of email infrastructure found in many PHP applications.

The benefits of the services

So why use these kind of services? As you know, it is also possible to send emails using the same web server that serves your PHP files. The benefit of using an email infrastructure provider is that they are easily scalable as a website grows and also save a lot of development time thanks to the bonus features they offer. A typical service includes email logs so that you can monitor your email traffic and quickly find out if emails are not reaching their destination. You can also view statistics of your emails and see how effective your marketing emails are, for example how often a specific link is clicked.

Using an external email infrastructure

Now you may be wondering how to get started with a service like this. There are many choices, and popular ones include Mailgun, MailChimp and SendGrid. For a PHP application, you may be more interested in a transactional email provider like Mailgun.

It is simple to integrate a transactional email provider into your existing PHP application. You can send emails using SMTP or via an API of the email service provider. In fact, you can even use the mail() function and send emails through SMTP and have your emails delivered through the service provider. If you have already been using SMTP in your applications, it will be easy for you to modify your code to support the new provider. APIs vary between providers, but a popular way is to use the cURL library in PHP.

In both of these cases, it only requires a few lines of code to send an email. For more information on the technical side of the SMTP or API, refer to the documentation of the email service provider. Either way, using a specialized mailing service for large scale sending will increase the amount of mail that reaches its destination.

Building scalable emailing for PHP-based web projects, 3.3 out of 5 based on 4 ratings
Categories: Software & Webapps, Tips & Tricks

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.