php mail

Summary: in this tutorial, you’ll learn how to send email using the PHP mail() function.

Introduction to the PHP mail() function

To send mail, you use the mail() function.

On Linux or Unix systems, you can configure the mail() function to use the sednmail or Qmail program to send messages.

On Windows, you can install the sendmail and set the sendmail_path in php.ini file to point at the executable file.

However, it’s more convenient to set the SMTP server with a port and sendmail_from in the php.ini file on Windows like this:

[mail function]
SMTP=smtp.phptutorial.net
smtp_port=25
sendmail_from=contact@phptutorial.netCode language: PHP (php)

If the SMTP server requires authentication, you can add the following lines for the account to authenticate:

auth_username=smtp_user
auth_password=smpt_passwordCode language: PHP (php)

Once the configuration is ready, you need to restart the webserver.

The following illustrates the syntax of the mail() function:

mail(
    string $to,
    string $subject,
    string $message,
    array|string $additional_headers = [],
    string $additional_params = ""
): boolCode language: PHP (php)

The mail() function has the following parameters:

  • $to is the receiver of the email
  • $subject is the email subject.
  • $message is the email message. It can be plain text or HTML. If $message is plain text, you use a CRLF (\r\n) to separate lines. And each line should not exceed 70 characters.
  • $additional_headers is a string or an array inserted at the email’s header. It includes from, cc, bcc… If the header comes from an untrusted source, you should always sanitize it for security purposes.
  • $additional_params allows you to pass additional flags as the command-line options to the sendmail program.

The mail() function returns true if the mail was accepted for delivery. It doesn’t mean that the mail is successfully reached the intended receiver. If an error occurred, the mail() function return false.

PHP mail() function examples

Let’s take some examples of using the PHP mail() function.

1) Using the PHP mail() function to send a plain text email example

The following example uses the mail() function to send a simple email:

<?php

$subject = 'This is a test email';

$message = <<<MSG
    Hi,
    This is a simple email.
    It's sent from PHP.
MSG;

wordwrap($message, 70, "\r\n");

mail('[email protected]', $subject, $message);Code language: PHP (php)

In this example, we use the wordwrap() function to ensure that the lines of the message won’t exceed 70 characters.

2) Using the PHP mail() function to send a mail with extra headers example

The following example uses the mail() function to send a mail with additional headers like From, Reply-To, and X-Mailer:

<?php

$to      = '[email protected]';
$subject = 'This is a test email';
$message = 'Hi there';

$headers[] = 'From: [email protected]';
$headers[] = 'Reply-To: [email protected]';
$headers[] = 'X-Mailer: PHP/' . phpversion();


mail($to, $subject, $message, implode('\r\n', $headers));Code language: PHP (php)

If you use PHP 7.2 or later, you can pass the headers as an array like this:

<?php

$to      = '[email protected]';
$subject = 'This is a test email';
$message = 'Hi there';

$headers = [
    'From' => '[email protected]',
    'Reply-To' => '[email protected]',
    'X-Mailer' => 'PHP/' . phpversion()
];


mail($to, $subject, $message,  $headers);Code language: PHP (php)

3) Using the PHP mail() function to send HTML email example

To send HTML mail, you need to set the Content-type for the header like this:

<?php

$to      = '[email protected]';
$subject = 'This is a test email';
$message = '<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email</title>
</head>

<body>

    <h1>This is HTML mail</h1>

</body>

</html>';


$headers = [
    'MIME-Version' => '1.0',
    'Content-type' => 'text/html; charset=utf8',
    'From' => '[email protected]',
    'Reply-To' => '[email protected]',
    'X-Mailer' => 'PHP/' . phpversion()
];


if (mail($to, $subject, $message,  $headers)) {
    echo 'email was sent.';
} else {
    echo 'An error occurred.';
}Code language: PHP (php)

Summary

  • Use the PHP mail() function to send an email message.
Did you find this tutorial useful?