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.
$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.
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.
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.
$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
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,$subjector headers. A newline lets an attacker addBcc:lines and use your server to send spam. Validate addresses withfilter_var()and strip CR/LF. $additional_paramsis dangerous with user input, because it becomes shell arguments. Useescapeshellarg()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
SMTPandsmtp_portini 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-8and 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.