SMTP vs REST API for sending emails

By default, WordPress uses PHPMailer class to send all emails. And, by default, this class uses PHP mail() function for email sending. PHPMailer class support different methods for email delivery, most popular being SMTP. This is a better solution than basic mail() function because using SMTP you can send your emails through third-party SMTP servers using popular mail delivery servers like Amazon SES, SendGrid or others.

Delivery through these services has a lot of advantages: better deliverability, various security protections, handling of spam, tracking and other things. And, there are two ways all third-party mail services work: using SMTP relay and REST API (or some variant of).

SMTP Relay

All popular services offer a very easy method for delivering emails through SMTP relay, making that the easiest way to set up email delivery in WordPress, just by using SMTP to connect to different third-party servers, deliver email to the service using SMTP, and the service handles email from that point on. Depending on the service, through SMTP headers you can send various control actions or additional data.

Part of the SMTP handled email MIME message

To connect to SMTP relay, all services have some method to generate username and password to connect, that can be API key, API key pairs… As for the connection, it can be secure (SSL or TLS), so make sure to use services that provide secure connection method.

SMTP is easy to use, but it has one major flaw that can be a contributing factor for using REST API. A lot of hosting companies might stop traffic on the SMTP reserved ports, or impose some sort of limit on using SMTP service, making it hard to send a lot of emails through SMTP.

REST API

REST is a popular method for securely connecting to servers to get or send the information. And, all email services implement REST API (or some similar method), to allow users to connect and send emails using the API. Connecting to the API always require API keys for authentication purposes. But, the biggest issue is that there are two ways to work with the API: create and format request manually to send using CURL or some other library, or use the SDK provided by the third-party service and work with it. And, some of the SDK’s can be huge and complex (AWS SDK for PHP is 12MB).

This is the biggest issue with using the REST API: it requires a lot more coding to work with, every service has own API and own way of doing things (it is not straightforward or universal as SMTP), and it will require regular maintenance to make sure your code is up to date with the API changes that companies introduce. On top of everything, authentication can be very complex (Google services age great example for this).

What is better?

I have reached to various mail services, and they all claim that connection, speed of email delivery is the same using SMTP Relay or REST API. In terms of getting started, SMTP is better hands down. But, if SMTP limitations are a factor with your website server, REST API is the only viable solution that will allow you to deliver as many emails as you need.

GD Mail Queue plugin supports any SMTP server with PHPMailer class – you need to provide server information with authentication and that’s it. In GD Mail Queue Pro, the plugin includes direct support for SMTP services where you need to provide only authentication for service you want to use, and the plugin does the rest. And, GD Mail Queue Pro also supports REST API services, some built into the plugin and some available as addons (this is done because the size of some API libraries can be huge).