Skip to main content

Getting Debian to email with exim4 and Google Mail

There are may reasons why would might like to run a headless Debian server on your home network. I have mine primarily as as file-store, serving video, music and pictures to my Windows MCE PC (you see I'm not one of these Windows hating Linux nerds, use the right tool for the right job I say!).

Being headless and tucked away in a dark corner it's important to be able to manage the system with little fuss, and I use ssh to login and administer the system remotely. The question here though is 'When do I know that I need login and do something?'. I ideally want to know asap if there are some important security updates (apticron), or that the raid (mdadm) has run into some trouble or if some important cron job has spewed some helpful output.

Lucky for me is was fairly straightforward to configure the server to email my google mail (gmail) account whenever it has something important to say, and here's how I did it! I have this working on both Lenny and Squeeze.

First off your server is going to need a gmail account; now it could use your own but I would rather not have my personal account details lingering around on my server (even if they are secured properly). I actually use my own domain with Google Apps, but this should work just as well with any old gmail account.

Now that you have an account you will need a Mail Transport Agent (MTA). You probably already have exim4 installed for this as it's a requirement for many packages. Check this and if not install it, something similar to the following should do the trick;
# apt-get install exim4-daemon-light

Now we'll generate the base of our configuration using dpkg-reconfigure;
# dpkg-reconfigure  exim4-config

This will launch you into a series of question, the answers to which are;
General type of mail configuration: mail sent by smarthost; no local mail
System mail name: [hostname].[domain]
IP-addresses to listen on for incoming SMTP connections:  127.0.0.1
Other destinations for which mail is accepted: [left blank]
Visible domain name for local users: [hostname].[domain]
IP address or host name of the outgoing smarthost: smtp.gmail.com::587
Keep number of DNS-queries minimal (Dial-on-Demand)? No
Split configuration into small files? yes 

This setup ensures that the server will be able to send mail, but will not receive or relay mail. Pay attention to the double :: separating out the host (smtp.gmail.com) and port (587) for the smarthost.

This will have populated our basic configuration which is contained in /etc/exim4/update-exim4.conf.conf which should now resemble;
# Some Debian help-text at the top here, omitted to save space

dc_eximconfig_configtype='satellite'
dc_other_hostnames=''
dc_local_interfaces='127.0.0.1'
dc_readhost='gabbro.mrverrall.co.uk'
dc_relay_domains=''
dc_minimaldns='false'
dc_relay_nets=''
dc_smarthost='smtp.gmail.com::587'
CFILEMODE='644'
dc_use_split_config='true'
dc_hide_mailname='true'
dc_mailname_in_oh='true'
dc_localdelivery='mail_spool'

Next up is the authentication details which are contained in /etc/exim4/passwd.client. Following the example provided by Debian in the file two lines are required. This is because smtp.gmail.com does not always resolve to the same hostname, hence configure the auth details in the following fashion, where your login is the full email address for the account;
# Example:
### target.mail.server.example:login:password

*.google.com:login:password
*.gmail.com:login:password

As this file contains some sensitive account details we should give it appropriate permissions;
$ chmod 0640 /etc/exim4/passwd.client

To ensure we get all the mail we're after, and to save us having to configure every daemon with our email address, the next step is to configure some aliases. using your favorite editor, open; /etc/aliases. You'll see a list of system users which are all mapped to 'root'. Now add an alias for root, pointing to your username and one for your username pointing to your email address. Here is an example;
# /etc/aliases
mailer-daemon: postmaster
postmaster: root
nobody: root
hostmaster: root
usenet: root
news: root
webmaster: root
www: root
ftp: root
abuse: root
noc: root
security: root
root: myusername
myusername: myaccount@gmail.com

Now we'll get all the emails we're after! Now to test that what we have done actually works, the easiest way is to send an email to root from your user account like this;
$ mail root

Set a subject and some body text (end the email with a single . on its own line) and go check your exim logs...
# tail -f /var/log/exim4/mainlog

With any luck you should see something like;
2009-12-30 16:40:15 1NQ1ap-0001rN-Pj <= root@host.domain U=root P=local S=384 2009-12-30 16:40:17 1NQ1ap-0001rN-Pj => myaccount@gmail.com  R=smarthost T=remote_smtp_smarthost H=gmail-smtp-msa.l.google.com [74.125.77.109] X=TLS1.0:RSA_ARCFOUR_MD5:16 DN="C=US,ST=California,L=Mountain View,O=Google Inc,CN=smtp.gmail.com"
2009-12-30 16:40:17 1NQ1ap-0001rN-Pj Completed

If not, one of us has made a mistake and I suggest you go look at the following websites:

Comments

Popular posts from this blog

Moodle on Centos or Red Hat 7 (with SELinux!)

Why the need for another 'Installing Moodle' guide? Two reasons, Systemd and SELinux. The steps are presented as a Bash script, which may be run on a virgin system, installing a complete working Moodle stack in one go, including enforcing SELinux. In addition to the absolute basics it also includes adding ClamAV virus for file uploads and Memcached for sessions and 'MUC'. It does not cover any extras you will need to get your site up to production, e.g. securing your database  or updating your virus definitions automatically. Neither does it do any extra PHP configuration (upload limits, execution time etc.) or any extra complexities that might be desirable. For all of this you should goto  docs.moodle.org .

Spawning many VirtualBox machines from a single VDI

What I'm taking about here is a way to have many VirtualBox machines based upon a single hard drive image. There are many reasons why you might like to do this, but the most compelling is probably saving time by not having to install an OS over and over again, especially useful if you do anything like software testing. Our goal is a single vdi (virtual disk) file which contains a vanilla installation of our favourite OS which we can then use to conjure up a fresh new machine in a jiffy. Assuming you already have VirtualBox installed our first step is (maybe for the last time ever!) to install our OS into a new virtual machine. Now I shan't go through this as it's pretty straight forward and if you're reading this it's the sort of thing you have probably done a hundred times before. One thing of note during the initial setup is the 'Virtual Hard Disk' configuration. Be sure to allocate enough space to allow for all potential applications of the image. It wou...

Row a Concept2 on Zwift with a $10 Raspberry Pi!

Short story, I made a program. Instructions and download here:  https://github.com/mrverrall/go-row Despite there being an appetite for rowing in Zwift the fact is a rowing machine is not a bicycle and a Concept2 rower won't connect directly to Zwift. The Zwift gods tease a rowing release every now and again, but it's been coming 'soon' for years now. Don't hold your breath. But people do row in Zwift, so how do they do it? To get the data from the rowers computer, the PM5, into something Zwift recognises as a bicycle you need a device that translates between the rower and the device running Zwift. There are solutions already available to do this. Some are expensive like the  NPE CABLE (about £90 in the UK) and some are 'free' like the  RowedBiker  app. The downside with RowedBiker is that it needs to run on a extra device separate from the one running Zwift. If you have a compatible device lying around, great, otherwise you'll need to buy one. Meanwh...