How to Configure Postfix to Customize Sender Name and Email Subject for Server Alerts


2 views

When setting up server alerts via Postfix, many administrators want to customize both the sender identity and email subject for better identification. The default configuration often shows unhelpful information like:

From: root@hostname (root)
Subject: Alert from hostname

We want to transform this into more professional-looking emails with:

From: zeus@example.com (Zeus)
Subject: [Zeus.domain] - Disk Space Alert

The first part is handled through smtp_generic_maps in /etc/postfix/main.cf:

smtp_generic_maps = hash:/etc/postfix/generic

Then create/edit /etc/postfix/generic:

root@zeus zeus@example.com
@zeus.localdomain @example.com

Run postmap /etc/postfix/generic and reload Postfix.

To change the displayed name from "root" to "Zeus", modify /etc/postfix/main.cf:

smtp_header_checks = regexp:/etc/postfix/header_checks

Create /etc/postfix/header_checks:

/^From:.*\$(.*)\$/ REPLACE From: $1 <${sender}>
/^From:.*/ REPLACE From: Zeus <${sender}>

Run postmap /etc/postfix/header_checks and reload Postfix.

For automatic subject prefixing, add to /etc/postfix/main.cf:

smtp_header_checks = regexp:/etc/postfix/header_checks

Then append to your existing header_checks file:

/^Subject:(.*)/ REPLACE Subject: [Zeus.domain] - $1

Send a test email from the server:

echo "Test message" | mail -s "Test Subject" your@email.com

You should receive an email with headers like:

From: Zeus <zeus@example.com>
Subject: [Zeus.domain] - Test Subject

For multiple servers, use a script to generate dynamic header checks. Create /usr/local/bin/update_header_checks:

#!/bin/bash
HOSTNAME=$(hostname)
DOMAIN="example.com"

cat > /etc/postfix/header_checks << EOF
/^From:.*/ REPLACE From: $HOSTNAME <${sender}>
/^Subject:(.*)/ REPLACE Subject: [$HOSTNAME.$DOMAIN] - $1
EOF

postmap /etc/postfix/header_checks
systemctl reload postfix

Make it executable and run it whenever hostnames change.


When setting up server alerts through Postfix, many administrators encounter an issue where outgoing emails show unwanted default values in the From header. The default configuration often displays:

From: root@hostname (root)

When what we really want is something more professional like:

From: zeus@example.com (Zeus Server)
Subject: [Zeus] Disk Usage Alert

To achieve this, we need to modify several Postfix configuration files. Here's the complete solution:

# /etc/postfix/main.cf
myhostname = zeus.example.com
smtp_generic_maps = hash:/etc/postfix/generic
header_checks = regexp:/etc/postfix/header_checks

Create or edit /etc/postfix/generic:

# /etc/postfix/generic
root@zeus   zeus@example.com
@localhost  @example.com

Then compile the map:

postmap /etc/postfix/generic

For the sender name modification, create /etc/postfix/header_checks:

# /etc/postfix/header_checks
/^From:.*$root$/    REPLACE From: zeus@example.com (Zeus Server)
/^Subject:/          PREPEND [Zeus] 

Then compile the header checks:

postmap /etc/postfix/header_checks

After making these changes, restart Postfix and test with:

systemctl restart postfix
echo "Test message" | mail -s "Test Subject" your@email.com

For more granular control over different services, consider using separate header checks:

# For cron jobs
/^From:.*$root$/    REPLACE From: cron@example.com (Cron Daemon)

# For backup scripts
/^From:.*$root$/    REPLACE From: backup@example.com (Backup System)
  • Check mail logs: tail -f /var/log/mail.log
  • Verify header processing: postconf -n
  • Test header checks: postmap -q - regexp:/etc/postfix/header_checks < test_email.eml