How To Send Emails Using PHP Mail

How to send emails using php mail

Sending emails from web applications is a common task that nearly every website will need to perform. But did you know you can send emails directly from PHP and you don't require any third-party software or paid service? In this guide, we will show you how you can send both text-only and HTML-formatted emails using just PHP.

PHP Mail

Inside the PHP core belongs a powerful yet underrated function called, mail. Mail is PHP's answer to allowing website owners the option to send fully customisable emails from the programming language. Let's explore how it works.

The PHP mail function takes up to 5 different parameters, of which returns a boolean on completion. Let's break down what each parameter does.

  1. String "$to" - this required parameter is the email address you wish to send the message
  2. String "$subject" - this required parameter sets the email subject line
  3. String "$message" - this required parameter sets the email's content otherwise known as the message
  4. Array or String "$additional_headers" - this optional parameter allows web developers to set extra email headers
  5. String "$additional_params" - this optional parameter allows web developers to set extra email parameters associated with sending
mail(
 string $to,
 string $subject,
 string $message,
 array|string $additional_headers = [],
 string $additional_params = ""
): bool

The "To" string parameter

This required parameter is an important part of the PHP mail function. You cannot send an email to someone without telling PHP who it is meant to go to. PHP allows you to add a single email address, multiple emails, the recipient's name, and email, or multiple recipient names and emails. The formatting of the sender to the field must comply with the RFC 2822 standard. RFC 2822 is a technical standard that was published in 2001 to define the format of email messages used on the internet. It is crucial because it ensures that email messages are interoperable across various email clients, servers, and platforms, meaning that there is consistency in the way email messages are sent and received. This standard helps maintain a cohesive email ecosystem on the internet.

The allowed format is as follows including sending to multiple recipients.

To: <address>
To: <address>, <address-2>
To: [display-name] <address>
To: [display-name] <address>, [display-name-2] <address-2>

The "Subject" string parameter

The second required parameter and an important part of the PHP mail function is the subject (subject line) parameter. This informs the recipient as to what the email is generally about. Typically, it's uncommon not to set a subject line, therefore PHP enforces this parameter. PHP follows the RFC 2047 standard for subject lines which describes how non-ASCII characters are handled and safely maintained in email messages. It enforces a number of rules for these characters to ensure they're encoded and decoded correctly.

Subject: Dev Newsletter

The "Message" string parameter

The message string parameter makes up the content you wish to send to an email address. Bar the subject line, this is the main section of an email. Although you can send an email with no message, in PHP that's not the best practice. Within the message, it can contain plain text or HTML markup but each line length must not exceed 70 characters. If a line does exceed this length it must be separated with a carriage return and line feed (CRLF). The CRLF (\r\n) is a sequence of two control characters that are used to represent the end of a line. This differs per operating system but this applies to Windows-based systems whereas line feed (\n) is only required when working with Linux-based operating systems.

As PHP allows you to enter a string, it is also possible to make your message in HTML format. This takes the standard text-only message to a new level. You can add images, colours and format the email message accordingly. It is important to note that in Windows, full stops at the start of the line are removed when talking to SMTP directly.

Message: <p>Welcome to my newsletter...</p>

The "Additional headers" string/array parameter

The optional parameter, additional headers, allows developers to add extra headers to their email messages. It allows for a string or an array of headers. You might wish to add additional headers such as encoding type, Cc, and Bcc fields. Just like the message format, new lines must be separated with CRLF. When passing an array, the key must be the header name and the value the header value.

Additional headers: ['cc' => '[email protected]']

The "Additional params" string/array parameter

The last optional parameter the additional params, allows developers to add extra params when sending emails using PHP. This allows the addition of extra flags such as the return path. You might wish to set the return path to an inbox that you monitor for cases where the email should bounce. When an email bounces it means the email failed to be delivered for a handful of reasons. Generally split between soft and hard bounces, and some reasons include, rejection by the recipient's mail server or an invalid email address. There's a wealth of different reasons (and status codes) why emails fail to reach their intended recipient, but that's outside of sending emails with PHP.

Additional params: [email protected] # no space after -f is intentional

If you're looking to go beyond the simple PHP mail function, there are other open-source libraries out there that allow you to achieve the same thing in this guide with some added functionality. A good example of this is PHPMailer. Common in a lot of open-source frameworks such as WordPress, PHPMailer gives you added functionality such as connecting to an SMTP with ease.

Conclusion

PHP's mail function is a very simple and easy way to start sending emails from your PHP application. Whilst using PHP mail doesn't guarantee it will be delivered 100% of the time, (mainly due to email spam issues globally), but gives you a starting point to understand how a programming language such as PHP can interact with email services and allow you to communicate to your users, directly from your application.

  • Always add a To, Subject, and Message to your PHP mail function
  • Use CRLF to break up long strings
  • Follow the RFC where possible to ensure your message arrives in the way you intend

Senior PHP developer. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience, and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all