mail()
mail() — Send mail.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
mail(string $to, string $subject, string $message, array|string $additional_headers = [], string $additional_params = ""): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $to | string | Receiver, or receivers of the mail. |
| $subject | string | Subject of the email to be sent. |
| $message | string | Message to be sent. |
| $additional_headers (optional) | mixed | String or array to be inserted at the end of the email header. |
| $additional_params (optional) | mixed | The additional_params parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option. |
Return value
Returns true if the mail was successfully accepted for delivery, false otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
Example
A runnable snippet:
// The message $message = "Line 1\r\nLine 2\r\nLine 3"; // In case any of our lines are larger than 70 characters, we should use wordwrap() $message = wordwrap($message, 70, "\r\n"); // Send mail('caffeinated@example.com', 'My Subject', $message);
Changelog
| Version | Change |
|---|---|
| 8.5.0 | When using the sendmail transport, the actual sendmail error is now reported, and unexpected termination of the sendmail process is detected. In such cases, an E_WARNING is emitted and false is returned. Previously, these errors were silently ignored. |
| 8.0.0 | All headers, including those in the additional_headers parameter, are now separated by CRLF (\r\n), as required by RFC 5322, rather than a possible mixture of CRLF and bare LF (\n). |
| 7.2.0 | The additional_headers parameter now also accepts an array. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page mail() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.