# `mail()` and `mb_send_mail()` Hardened Against Header Injections

> Section in “New and Extended Functionality › Cryptography and Security” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/cryptography-and-security/mail-and-mb_send_mail-hardened-against-header-injections/>

Sending emails is a common task and, of course, PHP has had support for this for a long time. Not properly validating values used in custom headers – passed in via the optional parameter `$additional_headers` to the `mail()` and `mb_send_mail()` function – allows an attacker to take control over the sender and recipients fields as well as the very content of the email.

To protect against these injections, PHP 7.2 now parses the `$additional_headers` string and employs security filters and restrictions on the individual header fields. For security reasons – and because it would also be invalid according to the email RFCs – headers such as `To` or `Subject` can no longer be set via the `$additional_headers` parameter. As those fields are available via the first two explicit parameters of `mail()` or `mb_send_mail()`, this change should not pose a problem.

Starting with PHP 7.2, `mail()` and `mb_send_mail()` also accept an array as `$additional_headers` rather than only a string. We recommend to always supply additional headers as an array, one header field per array element.

Of course, the same security restrictions do apply regardless of whether arrays or strings are used.

---

Previous: [crc32c Hashing Algorithm](https://php7explained.com/new-and-extended-functionality/cryptography-and-security/crc32c-hashing-algorithm/index.html.md) | Next: [Secure unserialize()](https://php7explained.com/new-and-extended-functionality/cryptography-and-security/secure-unserialize/index.html.md)
