Sending Email From Your System with sSMTP
Wouldn’t it be useful if your computer could email you? I’d like to be notified by email when my server is in trouble, but I don’t want to run my own mail server. sSMTP is perfect for this; it’s a simple way to send email from your system to an SMTP mail server, like Gmail’s.
Here’s how I set up sSMTP on Ubuntu to send mail through my Gmail account.
Install sSMTP from the package ssmtp (click the link to install), or by running the command below in your terminal:
sudo apt-get install ssmtp
sSMTP can be configured from one text file. Open /etc/ssmtp/ssmtp.conf
in a
text editor:
sudo gedit /etc/ssmtp/ssmtp.conf
The configuration file is very short and well commented by default. Here’s the options I use for sending mail through Gmail:
root=myemailaddress@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=mygmailusername
AuthPass=mypassword
UseSTARTTLS=YES
If you’re using Gmail like I am, change the root
, authuser
, and authpass
options to the email address, username, and password of your Google account. If
you’re using another mail service you’ll need to change mailhub
to the
relevant SMTP server.
Once your configuration work is done, time to try sending some mail! The simplest way to do this is to run sSMTP in a terminal with a recipient email address:
ssmtp recipient_email@example.com
sSMTP will then wait for you to type your message, which needs to be formatted like this:
To: recipient_email@example.com
From: myemailaddress@gmail.com
Subject: test email
hello world!
Note the blank like after the subject, everything after this line is the body of the email. When you’re finished, press Ctrl-D. sSMTP may take a few seconds to send the message before closing.
Now it’s easy to write scripts which can send you mail. Here’s an example terminal command to send an email from the msg.txt text file:
ssmtp myemailaddress@gmail.com < msg.txt
msg.txt is a simple text using the proper formatting for sSMTP:
To: myemailaddress@gmail.com
From: myemailaddress@gmail.com
Subject: alert
The server is down!
Later this week I’ll be setting sSMTP on my web server to notify me when when there’s a problem. Have any other ideas for how this can be used? Leave a comment below.
Archived Comments
NoNeedForAName
The web service software (eg. Apache, Ngix, LigHTTPD, etc.), which *nix veterans know as the server daemon, can choke independently of the the network connection’s status.
So yes, a downed web/database/app server can mail you, depending on specific circumstances.
Max
This is a really neat idea.
I am wondering how I would automatically have the output of my cron jobs emailed to me every morning to see if they executed properly? I know that the cron output is stored in the servers mail archive, because when I log in, the server tells me I have new mail waiting, and using the mail command I can view the output. One way to do it would be to just pipe the script output to sSMTP as part of the cron job, but would there be any way to have cron not output to the server mail application but rather to sSMTP directly?
Also on an unrelated note, it could substitute for the php mail function thus sending your email securely and depositing a copy of the email in your gmail sent box.
jma
Good article, cheers. Can I recommend using the ‘-C’ option to provide an alternative .conf file. Saves storing a password somewhere globally ;)
Anonymous
If you want to monitor a server, sSMTP would be very useful when combined with OSSEC HIDS (http://www.ossec.net/). Certainly much simpler than a full Postfix setup. I may do this on my home servers.
Tony
I’ve been meaning to setup email notification for my software RAID but never found a simple walk-through for it. This sounds like it might help with that goal?
a0peter
It would be great with a howto to automatically send files dropped into a fold in an email? That would make it much easier to backup files.
randomUbuntuUser
Pretty useful
Would be cooler if it was possible to make these mails HTML mails.
Or at least add an image.
Any way to do that ?
tom
Ssmtp works great for me.
I have replaced Exim for it as I doesn’t need a “full” MTA.
Tom
Tom
Andrew:
You’re right, if the server was in enough trouble it may not even have network
connectivity. Maybe a script on another system would be a better idea.
jma:
Sounds like a great idea! I’ll try it out.
Anonymous:
OSSEC looks cool, but I think it would be overkill for me.
a0peter:
Emailing files in a folder would be nifty, I’ll look into it.
randomUbuntuUser:
HTML email is possible. Just add these lines after the subject:
Mime-Version: 1.0
Content-type: text/html; charset=“iso-8859-1”
jure
hmm… why would you do this when you can just install monit from the
repositories and let it monitor apache, your ram, your system and email you when
you reach certain limits? :)
It’s also better if this kind of monitoring is on another computer so you get an
email when the system is down.
jarav
I use Twitter for this. I then get the server status as sms on my cell phone. First, you create a twitter id to which you post using ‘curl’ from the server. You then subscribe( with your twitter id) to that twitter id.
Anonymous
I used this with an Ubuntu server with RAID. By setting MailAddr in mdadm.config, it now sends emails using ssmtp. Installing ssmtp apparently is now the default for sending system emails.
Philip
To read the “to address” from file you can add -t :
ssmtp -t < msg.txt
I didn’t see that you had specified the “to address” on the commandline since it was also in the file.
addhe
send-mail: Cannot open smtp.gmail.com:587
i got this error please help me
Zelalem
Hi I followed your step, but when I press Ctr+D it doesn’t close. I had to close
using Ctr+Z and it said:
[1]+ Stopped sudo ssmtp zelalems@hotmail.com
Initially I used to get the following error when I write the e-mail (especially
when I try to create the empty line after the subject):
ssmtp: 550 preferably your XXX address error. (XXX is where I have an e-mail
address)
But later when I changed it, it seemed to work, but when I press Ctr+D it
doesn’t close. I used the outgoing mail server address of my e-mail account
provider in my configuration. However, in addition to the mail server address,
the following information exisits in the mail server:
Protocol: ESMTP
Submission: port 587 using STARTTLS: port 465 for TLS/SSL - on connect. So I
used the port 465 in addition to the mail server name. What could be wrong?
Zelalem
Hi, sorry for the previous posting. It is working now. The problem is I used a different port. I changed the port and it worked. Thank you for introducing for us the very simple but very useful powerful tool in a very nice way.
Cheers,
reza
i used it in a batch file, how i input ctrl+d ?
henry
you can use the tool xdotool. install it and use it in a bachtfile. Here an
example :#!/bin/bash
var=0
count=0
for i in $(cat /home/hm/Desktop/list1.csv);
do
count=$((count + 1))
/usr/bin/firefox -new-tab “$i”
sleep 1
xdotool key “ctrl+plus” “ctrl+plus” “ctrl+plus” “ctrl+plus”
done
exit
it loads a list of sites in firefox screens. the list is in the list1.csv file.
Mark
Something good to note, my password had an ‘!’ in it, which prevented authentication.
Not sure if anyone know a workaround.
Alan
The use that I am trying to get out of it is to have my FTP server, PureFTPd,
send me an email when a file has been uploaded.
PureFTPd has pure-uploadscript(8), which can pass arguments to a script that can
send an email, notifying of the upload.
At the moment I am just trying to write a script that will send a test email
using ssmtp.
I can get it to work by typing the command into the command line, but have had
no luck in writing a script that will send an email via ssmtp.
Any help would be greatly appreciated.
Chris
By far the simpliest solution I’ve found. Thank you very much!
ubuntu lover
I remade this tutorial and added some optional tricks and gave you the credit. Thanks! http://ubuntu-for-humans.blogspot.com/2010/04/sending-emails-from-ubuntu-without.html
gabriel
this post is very good!!!!!!!
Matt C
Thanks for your tutorial. I found it useful. Is it possible to change the sender email (by masking it or something) to a different address? At the moment, when it sends data to the Gmail SMTP, it comes out as my gmail account’s username (eg. user@gmail.com)
For example:
From address is “bah@bah.com”
Email gets sent.
I recieve the email, but the sender says “bah@gmail.com” instead of
“bah@bah.com”. Is there anyway to override this?
Cheers!
HollowMac
I desperately wanted a simple way to allow mdadm to send me mail : I thought that the complicated tutorials on PostFix ….. And then I came across your article, thank you! :) ;)
dakdo
works perfectly with new mdadm raid on ubuntu 10.10, all I had to do was (after following your instructions) edit mdadm.conf by inputting my gmail at the MAILADDR line. THANKS!
SunCheek
This info is great! Going to use it for sending emails via SMS from my home
DECT phone and local Asterisk.
Thanks!
Sandeep Singh
Great article buddy. Simple and precise. I spend hours on configuring postfix and then ssmtp but was stuck. With your instructions, it was up & running right away!
Thanks!
Bernard
Fantastic Tom !!!
I had been trying to setup Sendmail on Ubuntu 10.04 for a while; this is 100x simpler, and works immediately.
Thank you for sharing your knowledge with us.
Best Regards
Bernard in Luxembourg
Musa Yusof
Thanks.
I manage to send via PHP mail() by modifying php.ini sendmail_path=“/usr/sbin/ssmtp -t” on Ubuntu 11.10.
Best Regards,
Musa in Kuala Lumpur.
venkatesh
how can i attach files by using this ssmtp mta. i am able to send mails by using ssmtp very comforbly, . plese help how can i do email-attachments with ssmtp?
Thank “u”
Mari
Hi I feloowld your step, but when I press Ctr+D it doesn’t close. I had to close using Ctr+Z and it said:[1]+ Stopped sudo ssmtp Initially I used to get the following error when I write the e-mail (especially when I try to create the empty line after the subject): ssmtp: 550 preferably your XXX address error. (XXX is where I have an e-mail address)But later when I changed it, it seemed to work, but when I press Ctr+D it doesn’t close. I used the outgoing mail server address of my e-mail account provider in my configuration. However, in addition to the mail server address, the following information exisits in the mail server:Protocol: ESMTPSubmission: port 587 using STARTTLS: port 465 for TLS/SSL on connect. So I used the port 465 in addition to the mail server name. What could be wrong?
sam
hi ,
Thanks for good article , but i got the below error , could you help me :
ssmtp mymail@gmail.com < email.txt
ssmtp: Cannot open smtp.gmail.com:587
and the email.txt enclude :
To: myemail@gmail.com
From: myemail@gmail.com
Subject: alert
This is a TEST !
Grats
Do you still need help Sam? If so you can email me (along with anyone else) gratsgg@ymail.com
Also something people look for is changing the ‘From:’ header so it appears the email came from lets say.. no-reply@yourwebsite.something well, gmail auto changes the From: header to your email, however the google apps doesn’t, (currently it doesn’t, at least.) I assume it won’t change for business reasons.. anyway you’d be able to use that to simulate so and so from so and so . whatever
rodd
I also got:
ssmtp: Cannot open smtp.gmail.com:587
although i can telnet to the name and port and get a prompt.
John
Heh, your post is over 4 years old now and the most useful help for this issue I found.
My server is very slow and spidering a client’s web site takes over 2hrs. I’ll use this to let me know when it’s done.
Cheers,
~j
Dave Nelson
This worked great on a BeagleBone Black with Debian installed. Thanks!
K K
Very useful. Thank you.
While this does work beautifully, how do I send a BCC:
Thanking you in anticipation, my friend.
Andrew
Hehe nice idea, but if there was a problem with your webserver would it be able to send an email?