I recently reinstalled my Ubuntu server (16.04 LTS) and wanted to set up the customary unix mail handing stuff so that my cron, temperature monitoring, and hard drive monitor scripts could automatically email me of anything went wrong. In the past I’ve used mandrill and sparkpost for this, but free services like these keep getting more and more difficult and persnickety to use, most-recently demanding things like opt-out/unsubscribe links in the emails and adding a privacy policy to the website (despite there not actually _being_ any website). So I wanted to see if it was easy to send email via Gmail’s SMTP interface.
I followed two nearly-identical guides:
- http://www.havetheknowhow.com/Configure-the-server/Install-ssmtp.html
- https://help.ubuntu.com/community/EmailAlerts
Basically, you need to install the ssmtp package, then edit the ssmtp.conf configuration file:
sudo apt-get install ssmtp
sudo vim /etc/ssmtp/ssmtp.conf
# Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. #root=postmaster root=MyEmailAddress@gmail.com # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com #mailhub=mail mailhub=smtp.gmail.com:587 AuthUser=MyEmailAddress@gmail.com AuthPass=MyPassword UseTLS=YES UseSTARTTLS=YES # Where will the mail seem to come from? #rewriteDomain= rewriteDomain=gmail.com # The full hostname #hostname=MyMediaServer.home hostname=MyEmailAddress@gmail.com # Are users allowed to set their own From: address? # YES - Allow the user to specify their own From: address # NO - Use the system generated From: address FromLineOverride=YES
If you have two-factor (2FA) authentication set up for your Google account, you need to create an "app password" for this "device". Visit this page to create an app password that doesn't use 2FA auth: https://myaccount.google.com/apppasswords
However, there is a problem with those two guides, specifically in the hostname= line. If you put your actual email address as the hostname entry, then google will complain when you try to connect. You can use this test command to see what is going on:
$ echo "Hello 1" | sudo ssmtp -vvv someone@example.org [< -] 220 smtp.gmail.com ESMTP j76sm1853421ioi.14 - gsmtp [->] EHLO MyEmailAddress@gmail.com [< -] 501 5.5.4 https://support.google.com/mail/?p=helo j76sm1853421ioi.14 - gsmtp ssmtp: Cannot open smtp.gmail.com:587
What you need to do is to set hostname=localhost in the ssmtp.conf file. Then it should work fine.
After install, cannot cd ssmtp???
ray@USN:/etc$ cd ssmtp
-bash: cd: ssmtp: Permission denied
ray@USN:/etc$ sudo cd ssmtp
sudo: cd: command not found
WTF???
Ray
Hi Ray, that’s pretty weird! Maybe try a ls -l /etc/ssmtp to see what the permissions are for that directory. Could also try sudo vim /etc/ssmtp/ssmtp.conf directly instead of doing a cd first? Did the install of ssmtp complete successfully?