PHP mail() Function

PHP mail() sends an email directly from your PHP script. You can use it for contact forms, welcome emails, password reset links and similar things.

The mail() function does not send the email by itself. It hands over the message to the mail system of your server (like sendmail), and that system delivers it.


Syntax

mail(
    string $to,
    string $subject,
    string $message,
    array|string $additional_headers = [],
    string $additional_params = ""
): bool

Parameters

ParameterDescription
toRequired. Email address of the receiver. Many receivers can be separated by comma
subjectRequired. Subject of the email. It must not contain new line characters
messageRequired. Body of the email. Each line should not be longer than 70 characters, use wordwrap() for long text
additional_headersOptional. Extra headers like From, Reply-To, Cc and Content-Type. Array format is supported from PHP 7.2
additional_paramsOptional. Extra command line options for the mail program

Return value: true if the mail was accepted for delivery, otherwise false.

Note: A true result does not mean the email reached the inbox. It only means PHP gave the mail to the server successfully.

Examples

Example #1: Send a simple email

<?php
$to = "student@example.com";
$subject = "Welcome to Tutorials Class";
$message = "Hello, thanks for joining us.";

if (mail($to, $subject, $message)) {
    echo "Email accepted for delivery.";
} else {
    echo "Email sending failed.";
}
?>

Output:

Output when mail server is configured

Email accepted for delivery.

On a local computer (like XAMPP) without a mail server you will usually see “Email sending failed”.


Example #2: Add From and Reply-To headers

Headers can be passed as an array. This format works from PHP 7.2 and it is easy to read.

<?php
$to = "student@example.com";
$subject = "Welcome to Tutorials Class";
$message = "Hello, thanks for joining us.";

$headers = [
    "From" => "hello@yourdomain.com",
    "Reply-To" => "support@yourdomain.com",
    "X-Mailer" => "PHP/" . phpversion()
];

if (mail($to, $subject, $message, $headers)) {
    echo "Email accepted for delivery.";
}
?>

Output:

Output when mail server is configured

Email accepted for delivery.


Example #3: Send an HTML email

To send HTML, set the Content-Type header to text/html.

<?php
$to = "student@example.com";
$subject = "Welcome to Tutorials Class";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "From: hello@yourdomain.com\r\n";

$message = "<h1>Welcome</h1><p>Thanks for joining <b>Tutorials Class</b>.</p>";

if (mail($to, $subject, $message, $headers)) {
    echo "HTML email accepted for delivery.";
} else {
    echo "Email sending failed.";
}
?>

Output:

Output when mail server is configured

HTML email accepted for delivery.


Example #4: Validate email before sending

Never put user input directly in mail headers. A bad user can add new lines and send spam from your site (this is called header injection). Validate the email first with filter_var().

<?php
$email = filter_var($_POST["email"] ?? "", FILTER_VALIDATE_EMAIL);

if (!$email) {
    echo "Invalid email address.";
    exit;
}

mail("admin@yourdomain.com", "New enquiry", "Reply to: " . $email, ["Reply-To" => $email]);
?>

Result: If the email is empty or wrong, the script prints “Invalid email address.” and stops. See exit() for more.


Why mail() does not work?

  • No mail server. Local servers and many cloud servers do not have sendmail configured.
  • Hosting restrictions. Some hosts block or limit the mail() function.
  • Goes to spam. Emails without proper From address, SPF or DKIM records are often marked as spam.
  • Wrong headers. Use \r\n between header lines and no extra spaces.

For real projects, most developers prefer a library like PHPMailer with SMTP, because it is more reliable. You can also read our PHP Send Mail chapter.


Summary

  • mail() sends an email through the mail system of the server.
  • It returns true when the mail is accepted, not when it is delivered.
  • Use headers for From, Reply-To and HTML content.
  • Always validate user input before using it in headers.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.