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

Most of the time 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 that 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.

Sending Emails from PHP has its advantages and disadvantages.
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 receive 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 inline 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 adds 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. PHP mail from address barely makes sense here.

While other providers like Gmail, accept the emails but filter those under the spam folder.

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



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, Gmail.

Postfix is definitely one of the easiest ones to integrate especially with the mail function in PHP, but most of the time you will not find it as a one sort deployment, where you installed postfix and it just started working smoothly.


If you are sending bulk emails, then please be careful with the server IP address and the PHP mail 'from' address used, because by default PHP mail() function using the server's default IP and hostname as the 'from'.

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.*