So I want my servers to email me when there’s problems or when I just need to feel like I matter and someone cares about emailing me.

Unfortunately, my university (MTU) blocks outgoing smtp on the default port (25) hence I need to use an external service like gmail to send email.

How do we do this?

1) setup a gmail account (or use a current one, whereever you want your emails to come from)

2) Tell exim4 (or sendmail, exim is already installed though and theres this nifty guide) to use gmail as a “smarthost”

3) use the “mail” command (or others) to send email to virtually anyone through the gmail account.

 

Here’s a little step by step on how to do this.

(In the following, “HOSTNAME” = whatever is in your /etc/hostname and “USER” = whatever user you want the email to appear from, usually root)

 

1) First lets reconfigure exim4 to our liking using dpkg

# dpkg-reconfigure exim4-config

Here’s the answers to the questions it asks:

  • Choose “mail sent by smarthost; received via SMTP or fetchmail”
  • Set to “localhost” for “System mail name:”.
  • Set to “127.0.0.1? for “IP-addresses to listen on for incoming SMTP connections” to disable external connection.
  • Leave as empty for “Other destinations for which mail is accepted:”.
  • Leave as empty for “Machines to relay mail for:”.
  • Set to “smtp.gmail.com::587? for “IP address or host name of the outgoing smarthost:”.
  • Choose “NO” for “Hide local mail name in outgoing mail?”.
  • Choose “NO” for “Keep number of DNS-queries minimal (Dial-on-Demand)?”.
  • Leave as empty for “”.
  • Choose “mbox format in /var/mail/” for “Delivery method for local mail”. (optional)
  • Choose “YES” for “Split configuration into small files?”. (optional)

2) Now lets configure the account details by opening /etc/exim4/passwd.client and adding the following:

*.google.com:[email protected]:password

where accountname is your gmail username and password is obviously the password.

3) Let’s setup the email addresses (/etc/email-addresses) to use for outgoing mail:

USER: [email protected] USER@localhost: [email protected] USER@HOSTNAME: [email protected] [email protected]: [email protected]

4) Finally, lets finish up and tell exim4 what is going on:

# update-exim4.conf # invoke-rc.d exim4 restart # exim4 -qff

Ding. Setup is Done. You should now be able to do something like this:

mail -s “SUBJECT OF EMAIL” [email protected] < email.message

OR

cat email.message | mail -s “SUBJECT OF EMAIL” [email protected]

where email.message is a text file with the body of the email you are sending

 

To send a message written in html:

cat email.message | mail -a “MIME-Version: 1.0” -a “Content-Type: text/html” -s “SUBJECT OF EMAIL” [email protected]

Obviously make sure your message uses html formatting (like your writing a web site)…

Most of the instructions here are from the great guide at the debian wiki: http://wiki.debian.org/GmailAndExim4

UPDATE – Just found another interesting program called ssmtp which supports gmail and other services…check it out here: http://www.howtogeek.com/51819/how-to-setup-email-alerts-on-linux-using-gmail/

Mario Loria is a builder of diverse infrastructure with modern workloads on both bare-metal and cloud platforms. He's traversed roles in system administration, network engineering, and DevOps. You can learn more about him here.