Sunday, February 27, 2011

Exim4 to Gmail with ubuntu

It's handy if your server can mail you alerts (think smartmon and the likes), and as gmail completely allows this and most people have a gmail account anyway, let's do a quick setup of the exim4 email server.

Just create a new email address at gmail, so we dont run into trouble with exim4 accidently sending so much messages your gmail account becomes blocked for suspected spamming and such.



In my current Ubuntu 10.10 there's a dependency problem with Exim4, so till that's fixed, first you have to install the normal packages, then the meta package :
   apt-get install exim4-base exim4-config
   apt-get install exim4-daemon-light

for testing to send an email, let's also install mailutils
   apt-get install mailutils

Then we configure Exim4:
   dpkg-reconfigure exim4-config

Here are the answers to the questions it asks (these differ a bit with version differences)

General type of mail configuration: mail sent by smarthost; received via SMTP or fetchmail
System mail name: leave as is,  "zz9" in my case
IP-addresses to listen on for incoming SMTP connections: leave as is, "127.0.0.1 ; ::1" in my case
Other destinations for which mail is accepted: leave as is, "zz9" in my case
Machines to relay mail for: leave as is blank
IP address or host name of the outgoing smarthost: smtp.gmail.com::587
Hide local mail name in outgoing mail? No
Keep number of DNS-queries minimal (dial-on-demand)? No
Delivery method for local mail: mbox format in /var/mail/
Split configuration into small files: No


Now open the file /etc/exim4/exim4.conf.template (possibly copy first so you have a backup)
Find the line .ifdef DCconfig_smarthost DCconfig_satellite and add the following in that section
   send_via_gmail:
       driver = manualroute
       domains = ! +local_domains
       transport = gmail_smtp
       route_list = * smtp.gmail.com
If you have any other smarthost defined with “domains = ! +local_domains” remove that smarthost.
Find the “begin authenticators”. In that section add the following
   gmail_login:
       driver = plaintext
       public_name = LOGIN
       client_send = : yourname@gmail.com : YourGmailPassword
Make sure you have no other authenticators with the same public_name (LOGIN). Comment them out if needed.
Find the comment  “transport/30_exim4-config_remote_smtp_smarthost”. In that section add
   gmail_smtp:
       driver = smtp
       port = 587
       hosts_require_auth = $host_address
       hosts_require_tls = $host_address

Now to effectuate:
  update-exim4.conf
  /etc/init.d/exim4 reload

In case you want all your root messages to go to that or another account, edit /etc/aliases, and add/replace:
root: accountname@gmail.com
adding that for each account you want to redirect to that email address... so if my user is jeroen just add 
jeroen: accountname@gmail.com
or just say jeroen: root


now let's send a message:
  echo blabla | mail -s "test" accountname@gmail.com


A few seconds later you should have mail in your inbox. Enjoy your new mailserver :)

No comments:

Post a Comment