3 minute tip: Configure a Linux server to send email

It’s useful to be able to send email from a Linux webserver. I do it to get MediaWiki page change notifications and other automated status updates. I wanted something that supported two-factor authentication, and this does.

This guide is for you, if:

  • You don’t want to run a mail server
  • You want to send email, and you don’t care about receiving it
  • You want people to receive the emails that your server sends

I’ve used this method with Linode, and it works perfectly.

Install mailutils

~ sudo apt-get install mailutils

When the setup wizard launches, choose the unconfigured option. You don’t need to do any special configuration to get this to work.

Install and configure sstmp

  1. ~ sudo apt-get install ssmtp
  2. ~ sudo vim /etc/ssmtp/ssmtp.conf
  3. Hit “i” to enter Insert mode.
  4. Uncomment FromLineOverride=YES by deleting the #
  5. Add the following to the file:

     
    AuthUser=<user>@gmail.com
    AuthPass=Your-Gmail-Password
    mailhub=smtp.gmail.com:587
    UseSTARTTLS=YES

  6. Save and close the file:
    1. Hit Escape
    2. Type :wq
    3. Hit Enter

If you’re using two-factor authentication
Create a new application-specific password to use in the config file above. (If you’re using Gmail, you can manage those passwords here.)

Test it out
~ echo "This is a test" | mail -s "Test" <user>@<email>.com

Using a webmail service other than Gmail
You can follow the same pattern that I used above. You’ll need to:

  1. Subsitute the SMTP address and port for your email service (e.g. Yahoo!) where it says smtp.gmail.com:587. (587 is the port number.)
  2. Set up an application-specific password if your webmail provider allows it, and paste that into the password line, the way I did with Gmail. (Yahoo! appears to have something similar.)

23 thoughts on “3 minute tip: Configure a Linux server to send email

  1. Thank you!!

    Works like a charm. Using it on my Ubuntu desktop to learn and play :)

    I couldn’t find your name on this site. Are you Mike McLaughlin?

  2. i follow all above setps on my ubuntu 12.04 version machine but it is not working

    i write echo “This is a test” | mail -s “Test” email@gmail.com this after installation and add above line in configure file but it is not working it stuck, when i enter its stuck please sugest me how to fix it ASAP

  3. Good thing is that this works with vagrant on mac as well. May be you could include that so that people find the page easily.
    I tested this on vagrant box precise32.
    Thanks!

  4. Hi,

    I followed your instructions but I got this message:
    mail: cannot send message: Process exited with a non-zero status

    There is nothing written in /var/log/mail.err
    I check the login and password with gmail: ok
    tested different setting STARTTLS or TLS with port 587 or 465
    Still don’t work 2 days later.

    Do you have any idea?
    Thanks

  5. i follow all above setps on my ubuntu 12.04 version machine but it is not working

    i write echo “This is a test” | mail -s “Test” email@gmail.com this after installation and add above line in configure file but it is not working.

  6. I wanted to point that ssmtp.conf is a read only file and for making changes to it, one must be logged in as a root user.

  7. Just want to contribute to this.

    For this error : “mail: cannot send message: Process exited with a non-zero status”, you might want to check the error log.

    more /var/log/mail.err

    Since the sample is using free gmail stmp server, so I guess we have the same issue. So apparently gmail has blocked ‘less secure’ app to connect to the smtp server by default. This is including our ‘mail’ command.

    https://support.google.com/accounts/answer/6010255

    If you really want to use gmail smtp server, turn on the ‘Access for less secure apps’ as described in the above link.

  8. Very helpful. I needed this extra step in setting up mail on my digital ocean droplet. I shoot the mail to another droplet (mailinabox). Good looks!

  9. Used an open Relay, so I didn’t need to do steps #4 on. Just changed the Mil server to the correct name and was up and running.

    A million Thank You’s

  10. The gmail i tried to use this with got suspended.
    The only thing i did different from the tutorial was use nano instead of vi.

    Any idea what i did wrong?

  11. You didn’t do anything wrong. Google has recently disallowed sending email in this fashion. It doesn’t work for me anymore, either.

  12. Work’s perfect, to work, enable “Access less secura application” to account google.

    and change the /etc/ssmtp/ssmtp.conf file and You will capable to set any name and email during runtime :

    FromLineOverride=YES

    Rgs

Leave a Reply

Your email address will not be published. Required fields are marked *