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
~ sudo apt-get install ssmtp
~ sudo vim /etc/ssmtp/ssmtp.conf
- Hit “i” to enter Insert mode.
- Uncomment
FromLineOverride=YES
by deleting the#
- Add the following to the file:
AuthUser=<user>@gmail.com
AuthPass=Your-Gmail-Password
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
- Save and close the file:
- Hit Escape
- Type
:wq
- 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:
- 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.) - 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.)
good article!
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
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?
Whoops, sorry Rian! Found you on Twitter – https://twitter.com/rianjs :)
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
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
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!
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
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.
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.
Thank you so much! a very concise article!
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.
Excellent post! Was up and running in minutes.
thumbs up
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!
Yes that works very well, very nice job. Thank you!
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
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?
You didn’t do anything wrong. Google has recently disallowed sending email in this fashion. It doesn’t work for me anymore, either.
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/
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
Gr8t bro very useful thanx, worked for me without any own efforts.
good article … helped me a lot
Very useful and clear!
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
Good article.
It was very useful for our project
Thank you very much,
It was the only guide that worked for me
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.
Thank you! This is the only easy to follow article I found that explained how to just send mail in less than 1 page!
Well done for turning what was looking like a very painful postfix-or-exim-manual reading session into a 5 minute fix. Top boy!
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.
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.