Why shouldn’t I use PHP’s mail() function?

 In
Last Updated On August 02, 2019

Most of the times the general opinion is to not use the PHP's default mail() function to send emails. Instead, use some libraries where you will have much more flexibility and control over what you are sending.
There are a lot of headers which are actually not advanced but are not included as a part of standard mail() call, thus making it inconvenient for the developers to integrate.

The default function is probably too basic and doesn't really ready to support some of the key features like attaching a file or sending HTML or using Bcc/Cc.

Before concluding anything, let's first understand the key disadvantages of using the default PHP mail() function to send emails. There are cases when the email sent via PHP mail() didn't received by the recipient and you will not get any visual error too. The most common reason for this is as below:

  1. wrong format of mail header or content (e.g. differences in line break between Windows/Unix)
  2. sendmail not installed or configured on your server (php.ini)- This is one of the very common reasons for emails not getting delivered.
  3. the hosting provider of the sender does not allow emails sent by PHP mail(); This is the most common spam protection policy which almost every hosting provider add on their server. In this primarily they block the outgoing SMTP ports 25, 587 and 465.

Also, it's a common observation, that emails sent via default PHP mail() function have problems with the format of the header or content that can cause serious spam issues. In most of the cases, emails sent via default function tends to land in the spam folder of your recipient's mailbox or are sent back to the sender or simply rejected. GMX is the best example of a free mail provider who rejects or delete such emails without informing the recipient.
While other providers like Gmail, accept the emails but filter those under the spam folder.

In fact, you will find the below note on official PHP.net manual, which clearly says that mail() function is not suited for sending any substantial amount of emails.

Note

It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.
For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.

It's not only the mail() function which matters a lot, but also the SMTP server. There are different types of SMTP servers. Sendmail is most of the default one, but there are a couple of other open source SMTP servers like postfix, qmail.
Postfix is definitely one of the most easiest one to integrated especially with the PHP mail(), but most of the times you will not find it as a one sort deployment, where you installed postfix and it just started working smoothly.

Common problems like attachments getting corrupted, Bcc/CC emails not receiving or corrupted are bound to come. And, once you start adding fixes for all these, you will see it will start making your code little meshy and making it incompatible to work smoothly with other SMTP servers.

So, if you really have time to solve all these then go ahead with core mail(); function to send emails, else use for mail sending library like PHPMAILER which is open source or just switch to some good ESP (Email Service Provider).
Choosing a good ESP and following the best email practices will help you to safeguard your emails from spam.

Tags:
Recent Posts
Comments
  • Mike
    Reply

    Helpful insight

Leave a Comment

We are always looking for fresh talent to contribute to our blog. If you have a knack for writing and a passion for email marketing, we would love to have your post on our Guest Blog .

Submit a Guest Post
Pepi thinking

Start typing and press Enter to search

Thank you for your details!

Fill out your information below, and we will send you a PepiAlert, that will describe your domain’s email deliverability situation. Please note that your email address must match the domain, or the domain must be owned by the company matching the email address. We have the right to refuse the request, if we can’t verify the information.

*All fields are required