Use GMail as an SMTP relay using SSMTP

On some of your home workstations, and especially on a laptop, setting up a full-blown SMTP server such as Postfix, Sendmail, or Exim might be overkill.  Follow the jump to learn how to setup the lightweight ssmtp to relay all outbound mail through your GMail account by using Gmail as a smarthost.

SSMTP is meant to be a no-frills, secure, and lightweight replacement for a full-blown MTA. Personally, I feel it's best use is on a laptop where you're moving around between networks a lot, and need to send outbound emails from cron or other shell scripts.

By setting up SSMTP, it doesn't matter where you are, sending mail will be sent out over encrypted SMTP to Google's gmail servers. After handing it off, Google's servers do all the routing for you.

Setting up SSMTP is quick and easy - let's get to it. On Ubuntu, run:

sudo apt-get install ssmtp mailx

Now, we just need to configure SSMTP. Open up /etc/ssmtp/ssmtp.conf in your favorite editor, and add or update the following lines:

#The following line redirects mail to root to your gmail account
root=myemail@gmail.com
mailhub=smtp.gmail.com:587
UseSTARTTLS=yes
UseTLS=yes
AuthUser=myemail@gmail.com
AuthPass=mypassword

That's it! Now, let's try testing it:

echo "This is a test message." | mailx -s 'Test Message' myemail@gmail.com

You should now be all setup and ready to go!

No votes yet

Comments

Bug

Hi,
I'm experiencing problems sending mail that way :
send-mail: Cannot open smtp.gmail.com:587

I did exactly the same as you with my gmail account.
Strange ?

Well, it's trying to work,

Well, it's trying to work, but either GMail is down (not likely), or your ISP is blocking access to that port. You should be able to telnet to smtp.gmail.com on port 587 and get a response. Until you do, SSMTP will not work.

HTH,

Justin

Worked like a charm : )

I've been slaving trying to find a way to do this using Sendmail, mailx, postfix but to no avail. Your post worked perfectly! Thanks you very much Justin.
BTW, is this process secure? (not a big deal for me in my case)

Recieving an email from gmail.com would probably be a whole other ball-game would it not?

Thanks again!

Hey Zack - glad to have

Hey Zack - glad to have helped.

Regarding secure, the TLS stuff in the config file enables SSL encryption, so yes, that is secure.

Receiving email is a different ball game. It's been awhile, but I'm sure you can configure fetchmail to receive email from gmail.

Justin

Very useful

Was up and running in < 60 secs, thanks!

Error 500

What would be the cause of this?

ssmtp: RCPT TO: (550 Hostname/Domain name not valid or not available.)

me too

I am having the same problem. Let me know if you find the solution for this.

TIA

solved

In my case, the problem was that my user hasn't access for reading the file /etc/ssmtp/ssmtp.conf. So the solution was only to add my user to the group mail, that have access for reading in this file.

HTH

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>
  • Lines and paragraphs break automatically.

More information about formatting options