ssmtp and gmail

Sometimes we want to enable our servers/desktops to be able to send email without setting up a full featured mail server or configuring postfix to route through Gmail.

sSmtp is an extremely simple, resource conserving, SMTP server that will allow your desktop or server to send email. In this article we are going to use sSMTP to send outgoing email through Gmail.

Install sSMTP

Debian/Ubuntu users can Install with this command or click here to open up apt:

sudo apt-get install ssmtp

We need to then need to edit, ‘/etc/ssmtp/ssmtp.conf’:

root=username@gmail.com
mailhub=smtp.gmail.com:587
rewriteDomain=
hostname=username@gmail.com
UseSTARTTLS=YES
AuthUser=username
AuthPass=password
FromLineOverride=YES

Then add each account that you want to be able to send mail from by editing, ‘/etc/ssmtp/revaliases‘:

root:username@gmail.com:smtp.gmail.com:587
localusername:username@gmail.com:smtp.gmail.com:587

Now try sending an email

You can send an email through your favorite email client, like ‘mutt‘, or type:

sudo ssmtp someemail@email.com

You will then type your message, hit enter and ‘ctrl+d

Now that you have a simple outgoing email server setup, you can do all sorts of neat things:

  • Configure cron jobs to send log reports to your email address
  • Alert you of all kinds of system changes
  • Send email alerts when your computer reaches a certain temperature
  • Send email through PHP, Python, Ruby, and Perl

Subscribe via Email

Subscribe to Nixtutor via Email

Enter your email address:

Was this information useful?

17 Responses

  1. [...] Setting up an email server on Nix machine is invaluable to keep an eye on multiple machines. Lets face it, often times Nix machines run so well that we sometimes forget to login and check on them every once and awhile. Why not make them check on their selves by scripting them to email you when they need some maintenance love? [...]

  2. Blake

    7-1-2009

    Just wanted to mention that if your using sSMTP from the CLI, that the format should look like this;

    sudo ssmtp someemail@email.com

    To: someemail@email.com
    From: youremail@email.com
    Subject: enter_subject_here

    enter_body_here (then press enter when done to get a newline)
    Then press ctrl-D

  3. Mark Sanborn

    7-1-2009

    Thanks Blake!

  4. [...] We can change this default behavior by changing the MAILTO variable. Note: This will not work if you have not setup an email server. [...]

  5. [...] talked about some of the benefits of setting up an email server in Linux and how you can use python to send email. Now we are going to look at how you can send email from [...]

  6. linux admin

    9-18-2009

    nice tutorial im going to try this on my linux server

  7. linux admin

    9-18-2009

    my file /etc/ssmtp/ssmtp.conf doesn’t contain any of the following

    UseSTARTTLS=YES
    AuthUser=username
    AuthPass=password

    do i need to add those ?

  8. linux admin

    9-18-2009

    i added them and it worked however isn’t it insecure to store the password as plain text ?

  9. Mark Sanborn

    9-18-2009

    @Linuxadmin

    Yes it kinda is… You can chmod 600 it. If you find a better way definitely let me know.

  10. [...] You must have some sort of mailer daemon installed. See How to setup Gmail with sSMTP. You can try it out by editing the appropriate parts of the script and then [...]

  11. Barrydocks

    10-18-2009

    Excellent howto, I have been struggling to set up postfix or exim to act as a simple e-mail reporting system for my server. This worked first time. I assume I just forward all the kernel messages from root to a user and then to an external account?

    Is there anyway of changing the sender name, I simply get messages from “root”?

    Thanks

  12. Badcam

    11-29-2009

    @Barrydocks

    Just follow what Blake does and include a
    From: youremail@email.com when sending emails.

    You can also do:

    From: AnythingYouLike

    I’d like to know if there’s a way to have this set in the ssmtp.conf file.

    Cheers.

  13. romulus

    11-29-2009

    Barrydocks, or anyone else who has that problem – you can add To, From and Subject fields (others I don’t know about). See above, the comment by Blake from 7/1/2009.

  14. G O Free =:)

    1-29-2010

    Debian installed ‘ssmtp’ under ‘/usr/sbin’

    I had to change my personal shell ‘.profile”.

    Now, it works great.

    Thanks a lot!

    G O Free =:)

  15. G O Free =:)

    1-29-2010

    Oh yeah…

    As above…

    Don’t use ‘sudo’ unless you want your emails to appear from ‘root’.

    Add ‘smtp’ directory to the PATH of your normal USER, and run as normal user.

    (Edit ‘/etc/ssmtp/revaliases’ with your normal user name as “localusername”, as in tutorial.)

    Thanks!

  16. [...] from your domain registrars control panel. Also if the amount of mail is less you can use sSMTP to send mail via GMail, which will give guaranteed delivery to inbox. If sending from your server, [...]