3 minute tip: Configure a Linux server to send email

Written by

in

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.)

Comments

34 responses to “3 minute tip: Configure a Linux server to send email”

  1. Hansel Avatar
    Hansel

    good article!

  2. judy Avatar
    judy

    Really good article, very easy to follow. The only hang up I had is that google prompted me to make a change to https://www.google.com/settings/security/lesssecureapps

  3. Hariharan Iyer Avatar
    Hariharan Iyer

    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?

  4. Hariharan Iyer Avatar
    Hariharan Iyer

    Whoops, sorry Rian! Found you on Twitter – https://twitter.com/rianjs :)

  5. […] 설명을 잘 해놓은 블로그가 있어, 간단하게 따라할 수 있었다. Thank you – 3 minute tip: Configure a Linux server to send email […]

  6. Somdeep Avatar
    Somdeep

    Im getting this error

    dhruv@dhruv-MacBookAir:~$ echo “This is a test” | mail -s “Test” somdeepdey10@gmail.com
    mail: cannot send message: Process exited with a non-zero status

  7. robin Avatar
    robin

    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

  8. Piyush Avatar
    Piyush

    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!

  9. Pierre Tremblay Avatar
    Pierre Tremblay

    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

  10. krishan Avatar

    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.

  11. Shashank Avatar
    Shashank

    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.

  12. Master of Some Avatar
    Master of Some

    Thank you so much! a very concise article!

  13. imansyah Avatar

    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.

  14. Buzzby Avatar
    Buzzby

    Excellent post! Was up and running in minutes.

  15. omid Avatar
    omid

    thumbs up

  16. matt Avatar
    matt

    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!

  17. Duculescu MIhai Avatar
    Duculescu MIhai

    Yes that works very well, very nice job. Thank you!

  18. Matt Avatar
    Matt

    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

  19. DagF Avatar
    DagF

    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?

  20. Rian Avatar

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

  21. Dibya Avatar
    Dibya

    Agreed with Rian. Recent Gmail and Yahoo-DMARC policies have disallowed using their domain names as sender for sending any type of bulk emails e.g. @gmail.com as sender is not allowed. Read the complete blog here: http://www.pepipost.com/2016/06/02/gmail-dmarc-policy-update/

  22. John Avatar
    John

    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

  23. sudhir Avatar
    sudhir

    Gr8t bro very useful thanx, worked for me without any own efforts.

  24. tony Avatar

    good article … helped me a lot

  25. Nestor Arias Avatar
    Nestor Arias

    Very useful and clear!

  26. vinci Avatar

    Is there another way to do this without writing the actual password in ssmtp.conf? Would there be a security problem nonetheless to store in in plain text?
    Thanks

  27. Sri Kasyap Kappagantula Avatar
    Sri Kasyap Kappagantula

    Good article.
    It was very useful for our project

  28. Jonas Avatar
    Jonas

    Thank you very much,

    It was the only guide that worked for me

  29. Jamal Avatar
    Jamal

    Thanks, this worked for me. I already had ssmtp working, so I just had to install mail-utils, without any configuration. Now my mail command works. By the way – you can just install ssmtp and configure it like he says if you want to send email, you don’t need the mailutils part, but it’s good for programs that use the mail command or expect it to be there.

  30. Steven Meltz Avatar
    Steven Meltz

    Thank you! This is the only easy to follow article I found that explained how to just send mail in less than 1 page!

  31. Mark Avatar
    Mark

    Well done for turning what was looking like a very painful postfix-or-exim-manual reading session into a 5 minute fix. Top boy!

  32. Danny Regalia Avatar
    Danny Regalia

    Great program! My SMTP port (25) is blocked by my ISP. So now from my website, a person comes in, fills out a contract form, and then it comes to me. Of course I have to use usermin to get it, but it’s better than nothing. Yes, I have tried to use thunderbird but doesn’t work. I can have the test mail go to my aol and gmail account, but it’s not setup yet. Overall, keep up the good work.

  33. ThumbOne Avatar
    ThumbOne

    Love the terse intro. Tried it with yahoo mail. Alas I get:

    mail: cannot send message: Process exited with a non-zero status

    So what’s missing here is quick intro into diagnosing that, how the mailutils and ssmtp hang together if they have a debug option to trace the effort to send mail.

  34. […] 3 minute tip: Configure a Linux server to send email from rianjs on a bloig entitled “Notes to my future self!” […]

Leave a Reply

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