Articles in this section

How to rewrite headers in outgoing mail messages on a Plesk server

kb: how-to Plesk for Linux ABT: Group B

Applicable to:

  • Plesk for Linux

Question

How to rewrite headers of outgoing mail messages on a Plesk server?
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:

      CONFIG_TEXT: /^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:

      CONFIG_TEXT: /^Received:\s+.*/ IGNORE

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

    • To change Message-ID:

      CONFIG_TEXT: /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:

CONFIG_TEXT: 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 .

Was this article helpful?

Comments

1 comment
Date Votes
  • Hi Mikhail Shport,

     

    When placing the call via:

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

     

    What I get is:

    warning: pcre:/etc/postfix/header_checks is unavailable. unsupported dictionary type: pcre

     

    Can you please see, what a fix is required?

     

    Could it be you missed the mapping of it?

     

    cd /etc/postfix

    postmap header_checks

    postconf -e 'smtp_header_checks = regexp:/etc/postfix/header_checks'

    service postfix reload

     

    Install PCRE for postfix?

     

    sudo apt install postfix-pcre

     

    And yet... it doesn't work. Is it called "on time" before the email is finalized?

     

    0

Please sign in to leave a comment.