DEV Community

Andriy Zapisotskyi for Mailtrap

Posted on

PHP built-in mail function vs PHP mailing packages

The article on Sending Emails in PHP was originally published at Mailtrap's blog.

PHP built-in mail function ()

There are two basic ways of sending emails with PHP: built-in mail function and external mail packages.

PHP’s built-in mail function () is very simple but at the same time, it provides a limited functionality for sending emails. You won’t be able to add attachments to your email, and building a beautiful HTML template with embedded images will be a tricky task as well.

The other side of the PHP mail function () is that the email is sent from your web server, which may cause issues with deliverability due to security concerns such as suspicion of spam and blacklisting. The best way to overcome this problem is sending messages via an SMTP server, however, this functionality is limited as well. PHP mail() does not usually allow you to use the external SMTP server and it does not support SMTP authentication.

In other words, I don’t recommend using the PHP built-in mail function() and advise you to avoid the headaches it may cause by installing an external mailer package.

If you are still committed to the PHP built-in mail function() and are ready to accept the challenge, let’s take a look at the basic code and its main parameters.

Syntax and parameters
The PHP mail syntax is pretty simple:

Enter fullscreen mode Exit fullscreen mode

It uses the following parameters:

  • “$to” = your message recipient(s). The email address format may be or User If there are several recipients, separate them with comma(-s).
  • “$subject” = your message’s subject “$message” = the body of your message. Lines should be separated with a CRLF (\r\n). Each line should not exceed 70 characters.
  • “[$headers]” = additional recipients of your message, which can be included in CC or BCC. Headers are optional, except for the “from” header: it must be specified, otherwise, you will receive an error message like Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing.

For more details and additional parameters, refer to the PHP documentation.

Sending HTML email using PHP mail() function

The body of the message can be written in HTML. However, as I’ve mentioned above, it should be simple. In the PHP mail function(), the HTML part will look like this:

// Message
$message = '
  <title>Review Request Reminder</title>
  <p>Here are the cases requiring your review in December:</p>
      <th>Case title</th><th>Category</th><th>Status</th><th>Due date</th>
      <td>Case 1</td><td>Development</td><td>pending</td><td>Dec-20</td>
      <td>Case 1</td><td>DevOps</td><td>pending</td><td>Dec-21</td>
Enter fullscreen mode Exit fullscreen mode

It’s important to remember that to send HTML mail, you need to set the Content-type header...

Continue reading the full guide on sending emails in PHP originally on Mailtrap blog.

Top comments (0)