2.8.18. PHP mail function

Attention!

If you don't use this feature right, you might run into delivery issues — your messages might go to spam or get rejected by the recipient's mail server. To avoid this, check out Proper sending mail from site.
For an example of how to use this function, see Check outgoing mail via PHP mail.

The PHP function mail allows you to send messages from a web server without authorization in an existing mailbox. It is useful for quickly setting up the sending of messages, but without certain settings, sent messages are likely to go to spam or be rejected. This is often due to an incorrect FROM header or its absence, as well as the inability to use DKIM.

When specifying the FROM header, it is important to consider which email address will be used. Recommended:

  • Or do not specify it in the function itself, but select it in the site settings.
  • Either do not select it in the site settings, but specify it in the function as an additional header.

For example, if you specify a mailbox with the google.com domain in the header when sending a message, the message will be rejected due to the SPF record settings for the google.com domain. Therefore, you should only specify your domain or an existing mailbox as the sender.

The PHP mail function has specific parameters for specifying recipient, sender, message, and other data:

mail("recipient", "message subject", "message body", "additional headers", "additional parameters");

Main parameters:

  • recipient — must comply with RFC 2822, cannot contain a total of more than 60 recipients in the To, Cc, Bcc headers:
    • admin@example.com — one recipient.
    • admin@example.com, user@example.com — multiple recipients.
    • Name <admin@example.com> — recipient's name and address; you can specify multiple recipients separated by commas.
  • message subject — must comply with RFC 2047.
  • message body — message content:
    • Must consist of lines no longer than 70 characters. To split the text into lines, you can use the PHP function wordwrap:
      wordwrap($message, 70, "\r\n")
    • Lines must be separated by CRLF (\r\n).
  • additional headers (optional) — a string with headers separated by CRLF (\r\n), or an array of strings with headers:
    • From — message sender. Will cause an error if a mailbox is selected in the site settings.
    • Reply-To — address for receiving replies to the message. Recommended to use instead of From.
    • Content-Type — MIME header with the data type in the message. Can be used to specify the encoding of the message:
      'Content-type: text/html; charset="utf-8"'
    • Cc and Bcc — sending a copy of the message. Cc — copies the message to the recipient, who will see their address in the To header (each recipient will think that the message was addressed specifically to them). Bcc — works similarly, but without changing the To header. ⚠️ In total, there should be no more than 60 recipients in the To, Cc, and Bcc headers.
  • additional parameters (optional) — parameters and keys for using sendmail:
    • -Fname — key with the sender's name (not separated by a space).
    • -fmail@example.com — key with the sender's mailbox (not separated by a space). Will cause an error if the mailbox is selected in the site settings.

Function parameters must be specified in double " or single ' quotation marks.

Content