How to Send Email Using PHP - Part 1

Send Emails with PHP - Part 1
Alex Kachalov Alex Kachalov 15 june 2021, 12:41 1073
For experts

Introduction

Let's say you are writing a web service that needs to send an email with PHP language. Sending an email is not an easy process. In order to do everything right, you need to follow many standards such as SMTP, MIME, Base64, SPF, DKIM, and others. Fortunately, over the time of email and the PHP programming language’s existence, many libraries have been written to facilitate this process. There are also Internet services that help to solve any emerging problems and make the process even easier.

However, the variety of solutions makes it challenging to choose the right service. Besides, in order to send emails correctly, you will have to solve the related issues of the domain setup and tracking statistics. We've prepared a series of articles to help you figure it all out. The series consists of the following topics:

  • Transport
  • PHP code
  • Composing a complex letter
  • How to avoid spam
  • Statistics
  • Bulk sending

Transport

First, let’s speak about the transport layer since it affects how to send an email via PHP code. Emails are sent using SMTP, and there are a variety of methods to do it:

  • directly to the recipient's SMTP
  • via your ISP's SMTP server
  • via public SMTP server (e.g., Gmail, Hotmail, mail.ru)
  • by configuring your SMTP server
  • via a commercial SMTP service (SendGrid, Mailgun, UniOne)

Directly to the Recipient's SMTP Server

Spoiler alert: This is the worst way you can choose. It is technically possible to connect directly to the recipient's SMTP server, but this is usually not the case. It is much more convenient and efficient to send through an intermediate, your SMTP server called SMTP-relay. First, you pass the email to be sent to it, and it is engaged in sending it to the recipient. Why is it worth doing this?

  • You don’t have to implement complex retry logic for yourself since the recipient's server may be unavailable or temporarily overloaded; it may deliberately refuse the connection on the first attempt to reduce the amount of spam (so-called greylisting).
  • You don’t need to do additional work on receiving and processing the A/MX records of the addressee to find out where to send the email.
  • Your ISP may close outgoing connections to SMTP port 25 to reduce spam.
  • SMTP-relay is designed to handle large volumes of incoming and outgoing mail and will most likely do it more efficiently than a homebrew solution.

If you are not an ESP service developer and not a spammer, then we advise you to forget about this method.

Your ISP's SMTP Server

In the vast majority of cases, PHP is used on commercial web hosting. It means that the hosting provider already has a configured SMTP server. Sending email via this server is possible either using the Unix Sendmail program running on the hosting or directly via the SMTP protocol through the connection data provided by ISP. Unlike the previous method, this one is quite convenient. However, it has some disadvantages:

  • The provider can limit the number of messages to be sent through it.
  • If your SMTP server gets blacklisted due to the hosting neighbors, your mail may end up in spam.
  • You need to set up SPF, DKIM, DMARC.
  • You also have to track the statistics of reading/clicks.

Public SMTP Server

If you have a personal or corporate account on any public mail service like Gmail or mail.ru, you can send mail from your program via this service.

In this case, you get some benefits:

  • The email deliverability rate will be higher because recipients have more trust in the SMTP servers of familiar services.
  • Setting SPF, DKIM, DMARC is already done correctly.

There are still some drawbacks:

  • The number of emails sent per hour/day is limited.
  • You are in charge of reading/clicks statistics.

Your Own SMTP Server

This is a good way. However, it is only suitable for those who have their own system administrator in the staff. Unfortunately, installing and configuring an SMTP server is not enough. You also have to monitor that the IP address or domain does not get blacklisted. You have to communicate with the domain owners so that your server is removed from the blacklist. You also have to make sure that the server works stably and is not overloaded. You also need to update it in time to avoid recurring vulnerabilities.

Commercial SMTP Service

There are many services that are responsible for solving most of the problems that arise when sending an email. Among them are SendGrid, Mailgun, UniOne, etc. Using one of these services has a lot of advantages:

  • You save time on setting up and maintaining the SMTP server.
  • You get maximum delivery speed.
  • Usually, the services include both viewing general statistics and tracking the results of delivery/reading/clicking on the link of individual emails.
  • Optional unsubscribe option.
  • In case of technical problems, you will be notified about them, and the support will help you solve them.
  • Deliverability is better than the provider's own SMTP server or the provider's general SMTP server.

In fact, there is only one drawback you have to pay for such a service. Fortunately, due to the competition, the pricing is affordable. Most of the services have either a free package or an extended trial period to understand if the service suits you.

Part 2

Related Articles

Blog
For beginners
How to Send Mass Email Campaigns in Outlook: Step-by-Step Guide
Outlook is one of the most popular email clients, and it can be used to send mass emails. Many wonder how to do that, and this article is here to explain the procedure. We’ll show you the exact steps to take for most effective results.
Alex Kachalov
25 october 2023, 14:389 min
Blog
For beginners
Cold Email Marketing 101
A cold email is an email you send to someone who has no prior connection with you or your business. At first glance, the concept of cold emails falls into the very definition of spam. However, in certain cases, which we’ve discussed in our previous article, cold emails are perfectly legal. Just be sure to follow the cold emailing best practices, which we will dive into below.
Alex Kachalov
02 february 2024, 08:158 min
Blog
For beginners
Guide on Dark Mode in Email
Having caused quite a stir in the tech industry, especially in UI/UX departments across companies, dark mode has finally made its way to email.
Valeriia Dziubenko
08 september 2022, 11:509 min