Encrypting and forwarding local email to an external email address

Last week I set out to find a reliable and permanent solution to my problems with unread system mail and undelivered cron error messages, and I can now see that sending, delivering, and receiving email is far more complicated than I had thought. I have had to learn more about SMTP, SSL/TLS, and email delivery than I ever wanted to know, and I still only know the bare basics.

When I started I had two requirements:

All local email sent to the root user (and preferably everyone else) must be forwarded to an external SMTP server and email account

All email that leave my network must be encrypted

After experimenting with number of different applications (GNU Anubis, Nullmailer, ESMTP, MSMTP, etc) I finally found something that worked; Postfix with GPG-Mailgate. If all you need is the ability to send email to an external account then there are other applications you could use instead (I had some success with MSMTP and Nullmailer). The advantage of using Postfix is its flexibility and maturity. All other applications I tried had some small thing they could not do reliably or would fail in some edge cases, such as only forwarding some of the emails sent to root, but not quite all of them.

This guide will show each step needed to set this up, and a few mistakes to look out for. The first part will cover Postfix and how to configure it to forward all emails sent to a local user to an external email account. The second part will show how to set up GPG-Mailgate so that all emails that are sent to certain accounts are encrypted with GPG before they leave the server. If all you need is mail forwarding then you can stop after the first part, and if you already have Postfix configured you can jump straight to the second part.

A few assumptions will be made in this guide.

You are using the root account. If you are not then you will have to prepend ‘sudo’ to some commands.

You are using Debian. Everything should still work if you are using a different Linux distribution, but you may have to make some minor changes.

Names and servers

I will be using the following user names, email accounts, and servers in this guide.

Email accounts

[email protected] : An external email account that we want to forward all local emails to

Domains

yourdomain.com : Your local domain

emailprovider.com : The domain of your email provider

anotherdomain.com : The domain of the email provider for the account you want to forward all local email to

Servers

smtp.emailprovider.com : The SMTP server of your email provider

server.yourdomain.com : The computer you want to forward local emails from

Part 1 – Forwarding local email to an external SMTP server

The first step is to install and configure Postfix to forward all emails sent to the root user (or any user you want) to an external SMTP server and email account. This is not difficult, but it does require a few steps.

First, if you do not already have Postfix install it and some dependencies.

apt-get install postfix libsasl2-modules

Fill in any domain names the installer asks for and choose the “satellite” option.

If you are not already using Postfix then you are probably running Sendmail. Before starting Postfix you will have to turn it off. To avoid future error messages and warnings I also recommend you completely uninstall it.

If you want to forward emails for other users than root you simply add them to the list as well. When you are done run:

newaliases

That’s it. If everything is configured properly all emails sent to the root user will now be forwarded to [email protected]

echo "Testing Postfix forwarding" | mail -s "Test email" root

If that was all you needed then you can stop here, but you are now sending unencrypted emails through the internet containing potentially sensitive information. If you think that sounds dangerous then continue to the second part were we will set up some automated GPG encryption.

A warning about restrictive SMTP servers and the From header

It is possible that these settings won’t work with your particular email provider. I’m using a Gmail account to forward email. It isn’t my primary email provider, but there are practical reasons for using it in this case. Unlike some other email providers Gmail does not care about what you specify in the ‘From’ header. It will overwrite whatever is in it with the Gmail user you used. This simplifies things greatly when forwarding local emails since you would otherwise have to change the contents of the header before sending it out. Postfix has the ability to replace addresses in outbound email (just google smtp_generic_maps or sender_canonical_maps and you should find instructions for how to configure Postfix), however, depending on how an application fills out the headers in emails they send out this may not work reliably in all situations. I tried to set this up with a more restrictive Zoho.com email account, but I could not get it to forward cron error messages.

If you need this for your email provider, and if you manage to set it up successfully, please leave a comment explaining how you did it. For everyone else; just use a Gmail account. It’s easier.

Part 2 – Encrypting outgoing email with GPG

It is possible that some of the applications sending email to your local accounts might send data that you don’t want any stranger on the internet to see. In my paranoid mind that just isn’t acceptable, and so we will be adding a layer of encryption to Postfix using GPG-Mailgate. GPG-Mailgate is a content filter script for Postfix that will encrypt a received email if there is a public GPG key available for its recipient, and if the email is not already encrypted. It is relatively easy to install and configure, but be warned that if you do not configure it correctly it will probably fail silently and send out empty emails.

Replace the addresses and keys on the last two lines with the email address you want to forward emails to and the identifier of the public key you imported earlier.

The final line is a bit of a hack. GPG-Mailgate uses the ‘To’ header to find the GPG key to use when encrypting email. If it can’t find a matching entry in the configuration file it will not encrypt it. This creates a minor complication since Postfix doesn’t rewrite the ‘To’ header when forwarding local email. Any email sent to the root user will therefore have a ‘To’ address of ‘[email protected]’; which GPG-Mailgate won’t recognise. To fix this we add the local email address to the configuration file as well.

These commands should both send an encrypted email to [email protected] If it doesn’t work check ‘/var/log/syslog’ for errors.

If GPG-Mailgate did not encrypt your emails then it is likely that it did not find a matching public key. Make sure that the keys and addresses in ‘/etc/gpg-mailgate.conf’ are correct.

If you get empty messages then it is likely that GPG returned an error to GPG-Mailgate. This will cause GPG-Mailgate to fail silently. Make sure that the gpgmap user has the permissions needed to use the GPG keys in ‘/var/gpg/.gnupg’ and that gpgmap is used by Postfix when running GPG-Mailgate.

If you get an error about a missing GnuPG module then you either set the wrong permissions for the GnuPG folder and its contents, or you placed it in the wrong python folder.

If everything is working than this should be it.

You can now send email through an external SMTP server from the command line

All emails sent to the root user are forwarded to an external email address

All emails sent to [email protected] are automatically encrypted before they leave the server