2.8.17. PHP mail function


This function, with incorrect settings and use, may incorrectly deliver messages to the target mailbox — sent messages may end up in spam or rejected by the recipient's mail server. It is recommended to use correct settings or send letters to SMTP.

Function mail allows sending emails without SMTP authorization in the mailbox. It is useful for quickly setting up the sending of letters, but at the same time, without certain settings, it can send letters that with high chances will be placed in spam or rejected. The reason for this is the absence of a correct or an indication of the wrong title. FROM upon dispatch, as well as the inability to use DKIM.

Pointing heading FROM, it is important to consider which mailbox will be used. Recommended:

  • Or do not specify it in the function itself, but set the correct mailbox in site settings.
  • Or don't install it in site settings, and indicate in the function with an additional header.

For example, when sending emails with a header that will use the google.com domain, the email will be rejected due to the presence of an SPF record for the google.com domain, so only your domain or an existing mailbox should be specified as the sender.

The mail function has certain parameters for specifying the data of the recipient, sender, letter, and more. An example of using the function:

mail("recipient", "letter subject", "letter body", "additional headers", "Extra options");

All specified function parameters must be specified in double " or single ' quotation marks. An example of using the function is provided in test script... The parameters must match:

  • recipient — the parameter must match standard, cannot contain more than 60 recipients, taking into account those indicated in the header to, Cc, Bcc, and can be specified as:
    • admin@example.com — one recipient.
    • admin@example.com, user@example.com — multiple recipients, separated by commas.
    • Name <admin@example.com> — the name of the recipient and mailbox. Optionally, you can also specify multiple recipients, separated by commas.
  • letter subject — the subject, which will be indicated in the letter, must correspond standard.
  • body of letter — the text that will be indicated in the letter itself. To indicate the correct body, you need to consider:
    • The body of the letter must be split into lines if it exceeds 70 characters. The string cannot be more than 70 characters. Splitting text into lines is convenient to implement with the function wordwrap... For example, if the variable $message contains the text of the letter, then you can use it like this:
      wordwrap($message, 70, "\r\n")
    • Splitting lines should be done using CRLF (\r\n).
  • additional headers (optional) — CRLF—delimited string (\r\n) headers or an array of strings with specific headers. Frequently cited headings:
    • From — header, which indicates the sender of the letter. Will generate an error when using a specific mailbox in site settings.
    • Reply-To — the address to which replies to the letter will be addressed. It is recommended to use this title instead of From.
    • Content-Type — A MIME header that tells the mailer about the type of data stored in the message. In this header, you can set the used encoding:
      'Content-type: text/html; charset="utf-8"'
    • Cc and Bcc — sending a copy of the message. Cc — copies the message to the addressee, working by analogy with the header To (recipient), but many mail services can use it to separate recipients, so as not to display them a list of who the letter was addressed to. This header for each recipient substitutes the recipient in the header To, thus each recipient will think that the letter was addressed specifically to him. Bcc — copies the message to the addressee and performs the same function as Cc except that the title is To (recipient) will not be changed and the user to whom the message is addressed will not be displayed in his mailbox in the list of message recipients. This header is dangerous to use and can lead to delivery problems or understanding the reasons for the delivery of the letter to the recipient indicated in it. Important! You cannot specify more than 60 recipients, taking into account those specified in the header To, Cc, Bcc.
  • Extra options (optional parameter) — parameters and keys to use sendmail... Frequently used keys:
    • -Fuser — a key with the name of the sender (not separated by a space).
    • -fuser@example.com — a key with the sender's mailbox (not separated by a space). The parameter will generate an error when using a specific mailbox in site settings.