Mail-to-HTTP

In addition to any other e-mail address, incoming mail can also be forwarded as an HTTP request to a URL, which gives the possibility to programmatically process the mail.

For data security reasons, only URLs with the same domain as the referring email address can be used. Subdomains are also allowed.
For example, the referring address added to the name@example.com mailbox is https://example.com/mail_to_http.php.

If the URL/file receiving the request is protected by Apache Basic auth (HTTP Authentication), its username and password must be appended to the URL in the form:
https://user:pwd@example.com/mail_to_http.php

Mail-to-http forwarding URL can be added in the “Forwarding” section of email account management.

When a new email arrives, the server makes an HTTP POST request to the URL.
The $_POST array variables are:

id (string): message id
from (string): email sender's e-mail address
to (string): email recipient's e-mail address
headers (array):
  from (JSON object): email sender's name and e-mail address
  to (JSON array): email recipient's name and e-mail address
  date (string): sending time (RFC 2822)
  message_id (string): email unique Message-ID
  subject (string): email subject
mail_to_http (array):
  from (string): email sender (SMTP protocol MAIL FROM:)
  rcpt (string): email recipient (SMTP protocol RCPT TO:)
  real_length (int): full length of the raw email (body)
  body (string): raw email (RFC822)
Additional information

['mail_to_http']['body'] is primarily intended for emails that do not have large attachments, because if an email is larger than 500 KB, the rest will be cut off.

Email larger than 500 KB can be retrieved as .eml files from the $_FILES array.
The fields in the $_FILES array are typical of POST uploads:
PHP documentation: POST method uploads

$_FILES array of variables are:

message (array):
  name (string): .eml file name
  type (string): email format type
  tmp_name (string): absolute path to temporary file
  error (string): error code
  size (string): file size in bytes

If the script or web server receiving the POST request returns the error code 5xx as the HTTP status code, the email server will keep the email in the queue and try to resend the email to the same URL after a while.

Updated on 29. Apr 2024

Was this article helpful?

Related Articles