Rewriting an Email’s Sender Address w/Postfix

At Virtual Instruments we rely upon emails from cron to get output from periodic tasks within our infrastructure.  These emails will come from the user account and the machine from whence they ran.  For example, I can expect emails from “” or “”.

Our IT staff recently made a security change as to who could send emails to company distribution lists, blocking emails that do not come from “<xyz>” email addresses.  This broke our aforementioned cron email notification setup.

Apparently new versions of cron support a “MAILFROM” environment variable, but unfortunately Ubuntu 12.04 LTS does not support this.

Not to fear!  Our production VMs are all running the Postfix mail daemon, managed by Puppet.  With a little bit of Postfix sender address re-writing magic, we can make all of the emails sent by cron appear to be sent by someone else.

The file “/etc/postfix/generic” is a table of patterns to match against the sender of each email.  If there is a match in the left hand column of this table, postfix will replace the sender with the corresponding address in the right hand side of the row. In our case, I wanted all emails being sent from the current host to go out with a different sender. You could easily change the pattern to only match for specific users but since our machines are all service machines I didn’t see the point in limiting this to specific users.

I made the following changes to our Postfix configuration with Puppet:

    smtp_generic_maps = hash:/etc/postfix/generic

    @<%= @hostname %>     <my_sender_account>

Then you just need Puppet to exec the following “postmap” command and restart the Postfix service like so:

    file {'/etc/postfix/generic':
      owner   => 'root',
      group   => 'root',
      mode    => '0644',
      content => template('postfix/generic.erb'),
      require => Package['postfix'],
      notify  => [
        Exec['update postfix postmap generic.db'],

    exec {'update postfix postmap generic.db':
      command     => '/usr/sbin/postmap /etc/postfix/generic',
      user        => 'root',
      refreshonly => true,
      notify      => Service['postfix'],

Voila! Emails from cron no longer come from “”, but instead as whatever sender account you defined in “/etc/postfix/generic”.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s