Plesk

How to rewrite headers in outgoing mail messages

Question

How to rewrite headers of outgoing mail messages?
For example, change From: MAILER-DAEMON@server.example.com to From: "Mailer-Daemon at server.example.com" <MAILER-DAEMON@server.example.com>.

Answer

This feature is yet to be implemented in Plesk UI.

Workaround

Warning: You must make sure that the postfix-pcre package is installed on your Linux server before following the steps below, because if it is not, the pcre (Perl Compatible Regular Expressions) rules we need to add to the /etc/postfix/header_checks will not be interpreted correctly by the postfix mail server and that will cause mail delivery issues.

As a workaround, use Postfix header_checks:

  1. Connect to the server via SSH

  2. Add a header_checks entry to the /etc/postfix/main.cf configuration file (the file specified will contain regular expressions for the rewriting):

    # postconf -e "header_checks = pcre:/etc/postfix/header_checks"

  3. If the file /etc/postfix/header_checks does not exist, create it:

    # [ ! -f /etc/postfix/header_checks ] && >/etc/postfix/header_checks

  4. Modify the /etc/postfix/header_checks file:

    • For example, to rewrite all headers with From: MAILER-DAEMON@server.example.com to From: "Mailer-Daemon at server.example.com" <MAILER-DAEMON@server.example.com> , add the following line to the end of the file:

      /^From:s+MAILER-DAEMON@server.example.com/ REPLACE From: "Mailer-Daemon at server.example.com" MAILER-DAEMON@server.example.com

    • To hide server IP address from headers:

      /^Received:s+.*/ IGNORE

      Note: For more information on regular expressions, refer to the PCRE documentation by php.net .

    • To change Message-ID:

      /Message-Id:s+<(.*?)@server.example.com>/ REPLACE Message-Id: $1@example.com

  5. Reload Postfix to apply the enabled header_checks:

    # service postfix reload

If a regular expression was applied to the mail message, a similar line will be appended to the /var/log/maillog log file:

server postfix/cleanup[21124]: E5CD0225EF9: replace: header From: MAILER-DAEMON@server.example.com (Mail Delivery System) from local; from=<> to= user@example.com: From: "Mailer-Daemon at server.example.com" MAILER-DAEMON@server.example.com

Note: For more information on header_checks feature in Postfix, refer to the Postfix documentation .