Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Web and Security

PHP mail() function

Reference for PHP mail(): parameters, headers, header-injection safety, why deliverability is poor, and when to use SMTP libraries such as Symfony Mailer or PHPMailer.

mail() hands a message to the mail program configured on the server (usually sendmail or a compatible wrapper, set by sendmail_path in php.ini). It returns true if the message was accepted for delivery, which says nothing about whether it ever reaches an inbox.

It is simple, but it has no SMTP authentication, no queue you control and no error details, and unauthenticated mail is often filtered as spam. For anything beyond a quick notification, use an SMTP library or an email API.

PHP
$ok = mail('[email protected]', 'Hello', "Line one\r\nLine two", ['From' => '[email protected]']);

Try it

This runner has no mail program, so calling mail() would only print a warning. The live examples build and validate messages, which is the part you control; sending is read-only.

PHP Building a safe message

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP What happens without a mail program

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Read-only: sending for real.

PHP
$ok = mail(
    to: '[email protected]',
    subject: 'Order shipped',
    message: "Your order is on its way.\r\n",
    additional_headers: [
        'From' => '[email protected]',
        'Content-Type' => 'text/plain; charset=UTF-8',
    ],
);

Syntax

PHP
mail(
    string $to,
    string $subject,
    string $message,
    array|string $additional_headers = [],
    string $additional_params = ""
): bool
Name Description
$to One address or a comma-separated list.
$subject The subject. Must not contain line breaks.
$message The body. Lines should be separated with \r\n, and lines longer than 70 characters are best wrapped with wordwrap().
$additional_headers From, Reply-To, Content-Type, etc. As an array (PHP 7.2+) use name => value.
$additional_params Extra command-line options for the mail program, such as -f to set the envelope sender.

Return value: true if the message was handed over, false otherwise.

Usage notes

  • Prevent header injection. Never put raw user input in $to, $subject or headers. A newline lets an attacker add Bcc: lines and use your server to send spam. Validate addresses with filter_var() and strip CR/LF.
  • $additional_params is dangerous with user input, because it becomes shell arguments. Use escapeshellarg() or fixed values.
  • Deliverability. Mail from a web server without SPF, DKIM and DMARC records usually lands in spam. A dedicated sending service or an authenticated SMTP account fixes that.
  • Windows uses the SMTP and smtp_port ini settings, not sendmail.
  • Prefer libraries: Symfony Mailer or PHPMailer (installed with Composer) give SMTP authentication, HTML and plain alternatives, attachments and proper error exceptions.
  • Send in the background. Slow mail servers should not delay a page; put the job on a queue.
  • HTML mail needs Content-Type: text/html; charset=UTF-8 and a plain-text alternative for best results; build it with a library rather than by hand.
  • Non-ASCII subjects need encoding with mb_encode_mimeheader() as shown above.