Home > bash, ubuntu > Send email from command line

Send email from command line

Problem
I wanted to send an email notification from command line. I launched a program on my desktop machine at my workplace that would require some days to finish. I want to know when it’s finished and I want to get the result too.

Solution
The email will be sent via gmail, it’s the easiest solution. Here, Sid explains nicely how to configure ssmtp on a desktop. Here I repeat the steps:

Step one:
Install ssmtp: “sudo apt-get install ssmtp“.

Step two:
Edit the file /etc/ssmtp/ssmtp.conf and add the following content:

root=username@gmail.com
mailhub=smtp.gmail.com:465
rewriteDomain=gmail.com
AuthUser=username
AuthPass=password
FromLineOverride=YES
UseTLS=YES

For this purpose, I created a dedicated Gmail account.

Step three:
Test if it works: “ssmtp recipient@gmail.com < filename.txt“.

You can also send emails with the command “mail“:

mail -s "Subject of the mail" recipient@gmail.com < result.txt

Use Case
In the intro I mentioned that I want to get the result of a slow program. Here is how I did it: the program writes the result in a file called “result.txt“. When it’s finished, a shell script is called that sends the content of result.txt to me:

time ./pe_211.d && ./send-mail.sh

Where send-mail.sh looks like this:

#!/usr/bin/bash
mail -s "PE 211" me@gmail.com < result.txt

Related links

  1. July 10, 2012 at 10:39

    Nice post.
    You can also do this with your own python script using smtplib.

    • July 10, 2012 at 11:37

      True. Once I wrote a post about such a Python script here.

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 71 other followers

%d bloggers like this: