Goal, disclaimer and credits
The high level goal of this howto
Be able to read mail from cron-jobs and other system services with your normal e-mail account.
The detailed goals of this howto
- To be a better alternative then ssmtp, nullmailer and similar MTA:s. In my experience all of them have problems which does not exist in this set up.
- Set up the Mail Transfer Agent (MTA) Postfix to re-route all local mail to a single external address. The local user who generally recieves the most mail is root. After following this howto the local users will not recieve a single mail, they will all be re-routed to the external address of your choice.
- Remove the default MTA Exim and all its configuration files. However, if you skip the purge command lines your configuration will not be removed.
- Go against the Debian way and use Postfix as your MTA instead of Exim. The only reason Postfix is used is that I found it a bit easier when researching MTA:s. If you have one MTA working already I would suggest that you stick with it. This howto is primarely aimed at those who have not touched their MTA since installing Debian and who have never read any local mail.
- Use a single Google account to send mail using Google's SMTP servers. You can use your regular account or create a dedicated one.
- All local addresses will be valid, not only those of existing accounts in /etc/passwd or aliases in /etc/aliases. The advantage to this is that all mail will always reach you as if their senders were configured correctly. The downside to this is if you stop using this MTA solution all misconfigured senders will have their mails bounced. However in that scenario postmaster (which you should check, or as by default forward to root which in turn is forwarded to you local account) will be informed so you will be able to correct them. In practise I think the downside is neglectable for 99% of the users.
- This set up will be able to send mail to arbitrary external addresses, but as it sends through GMail and does not concern itself with responses we will not know if the mail arrived or not. It makes the set up unsuitable for forums and other systems where arbitrary external addresses are sent mail and it's necessary to check if the sent mail arrived or not. However, it works well for recieving cron mails and other system mails at an external address which you actually check
- I take no responsiblity for the correctness of this howto or any damages to your system or family it might cause. I have done what I can to see that it works and that it is secure. I am using this set up myself.
- Give me any feedback you have and I will try to correct any errors in this post or make other improvements.
- I found out about virtual_maps through an answer on askubuntu.com.
- A blog post on rtcamp.com thought me how to send mail through GMail.
- The Postfix documentation in general and especially the postconf part as it thought me how to configure /etc/postfix/main.cf.
Perform all commands below as root unless otherwise is specified. If you use sudo issue sudo -i to become root and on other systems you simply issue su:
-
Install Postfix by issuing apt-get install postfix, this will also uninstall Exim but keep its configuration files, in case you change your mind. You will be presented with some package configuration dialogs for Postfix:
- General type of mail configuration: Internet Site
- System mail name: Just press Ok and accept the default
- Stop Postfix (which has automatically been started) using service postfix stop as it's not ready yet
- Remove the Exim configuration files using apt-get purge exim4 exim4-base exim4-config exim4-daemon-light if you had never touched them since installing Debian and want to clean up a bit. If you are the least bit unsure, skip this step!
- Since you won't be fetching mail using procmail any more it's safe to remove it apt-get remove procmail. By default it doesn't have any configuration, which is why remove is used instead of purge. (If it turns out that you are using it without knowing it, simply re-install it using apt-get install procmail. But you are most likely not using it.)
-
Replace the contents of /etc/postfix/main.cf by first clearing it using cat /dev/null >/etc/postfix/main.cf and then filling it with the following contents using nano by issuing nano /etc/postfix/main.cf
Code: Select all
## ## Only listen for mail from the local machine and only trust the local ## machine as an SMTP client ## inet_interfaces = loopback-only mynetworks = loopback-only ## ## Redirect local mail according to /etc/aliases (ie. postmaster => root) ## alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases ## ## Redirect all local mail to ## virtual_maps = regexp:/etc/postfix/virtual-regexp ## ## Send through GMail ## relayhost = [smtp.gmail.com]:587 ## ## SMTP client authenticates using SASL using a user:password pair ## stored in a separate file. Do not allow anonymous authentication ## and use TLS if it is available ## smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_CAfile = /etc/postfix/cacert.pem smtp_use_tls = yes
- Create a SASL password mapping file which only root can access using (umask 166 && nano /etc/postfix/sasl_passwd) (the paranthesis are important) and fill it with a single line where you replace sendingusername and thepassword accordingly. If you use Google Apps for Domains you simply replace gmail.com with your domain.
Code: Select all
[smtp.gmail.com]:587 sendingusername@gmail.com:thepassword
-
Create /etc/postfix/virtual-regexp by issuing nano /etc/postfix/virtual-regexp and fill it with a single line where you replace alllocalmailgoeshere@anydomain.com with the mail address where you want to recieve all local mail.
Code: Select all
/.+@.+\.localdomain/ alllocalmailgoeshere@anydomain.com
- Create the Postfix lookup tables (the files which Postfix actually reads) for the above two files by issuing postmap /etc/postfix/virtual-regexp and postmap /etc/postfix/sasl_passwd.
- Copy the certificate used for authenticating with GMail's SMTP servers into the Postfix directory using cp -v /etc/ssl/certs/Thawte_Premium_Server_CA.pem /etc/postfix/cacert.pem.
First make sure that you have all the necessary files with correct permissions. Your output from ls -l /etc/postfix/ | egrep 'main|cacert|sasl|virtual' should look exactly like this (except that the text file sizes may vary 1 byte depending on if you have a trailing newline or not):
Code: Select all
-rw-r--r-- 1 root root 1155 Sep 21 03:18 cacert.pem
-rw-r--r-- 1 root root 829 Sep 21 02:55 main.cf
drwxr-xr-x 2 root root 4096 Mar 6 2013 sasl
-rw------- 1 root root 51 Sep 21 03:07 sasl_passwd
-rw------- 1 root root 12288 Sep 21 03:25 sasl_passwd.db
-rw-r--r-- 1 root root 44 Sep 21 04:10 virtual-regexp
-rw-r--r-- 1 root root 12288 Sep 21 04:10 virtual-regexp.db
Create a test function called testsend which we will use to send a few mails by copy pasting this into your command line
Code: Select all
function testsend { u=$1;echo -e "Hello $u\n\nLucky nr: $RANDOM" | mail -s "Postfix test message to $u at $(date)" $u; }
Test sending mail to a local user
- Send a test mail to root using testsend root
- Check for errors in /var/log/mail.err
- Check for warnings in /var/log/mail.warn
- Check that you actually recieved the mail at the mail account you want all local mails sent to
- Look in /var/log/mail.info and see how it was routed
- Send a test mail to nonexistinguser using testsend nonexistinguser
- Check for errors in /var/log/mail.err
- Check for warnings in /var/log/mail.warn
- Check that you actually recieved the mail at the mail account you want all local mails sent to
- Look in /var/log/mail.info and see how it was routed
- Send a test mail to an external mail address you can check which is not the normal address for local mail, using testsend someone@somewhere.com where you replace someone@somewhere.com accordingly
- Check for errors in /var/log/mail.err
- Check for warnings in /var/log/mail.warn
- Check that you actually recieved the mail at that mail account
- Look in /var/log/mail.info and see how it was routed
- Send a test mail to a non existing mail account using testsend idontexist@example.com
- Google will say that sending went OK and there will be no error in /var/log/mail.err
- Nor any warnings in /var/log/mail.warn
- Look in /var/log/mail.info and see that it was routed as the other external address
- But a reply will be sent from mailer-daemon@googlemail.com to the account you are using to send mail when Google realizes that this recipient does not exist.
It took me quite a while to reach this set up, mostly due to my inexperience with MTA:s. Enjoy! Feel free to ask questions!