Subscribe to Penguin-man the computer guy

Friday, November 14, 2014

Mail script to automate SSMTP

I wanted to use SSMTP to send mail from the terminal. After a little effort, I got it working. I wanted to set up SSMTP as a way of automating alerts from my system and sending them to my emails. In order to do that, I needed a terminal based email that I could call from a script. I'm using a Asus K55 laptop running Ubuntu 14.04 at the moment.

One of the disadvantages of SSMTP is there is no built in signature section or headings. I figured I'd hack that problem with a little bash scripting. That actually worked quite well and I decided to make an alias to run my script as if it were a built in program. So in this lesson, I will show you three things: how to install and configure SSMTP, how to configure and run it, and how to make the same script and alias that can automate email for you.

To get started we run the following to install SSMTP:

       sudo apt-get install ssmtp 

After installing SSMTP, we must configure it. run:
 

       sudo gedit /etc/ssmtp/ssmtp.conf

Once we have /etc/ssmtp/ssmtp.conf open in a text editor (I'm assuming you use gedit, you could also use emacs, nano, or vim, etc) place the following inside the document editing in order to use the settings for your account. I'm assuming you are using a gmail account.

           root=myemailaddress@gmail.com
           mailhub=smtp.gmail.com:587
           AuthUser=mygmailusername
           AuthPass=mypassword 
           UseSTARTTLS=YES
If you want more basic information on SSMTP, this link to another tutorial I used might help. After we are done configuring the email, we can test it. Place the message you want to send in a file, and name it message.txt. Now, make your signature file, and name it signature.txt. To email a file with signature in another file, run:

      cat message.txt signature.txt | ssmtp recipient-email-address@gmail.com


Now that we have tested our email, lets automate it a bit using a BASH script.

Script to automate our email

Before we get to the scripting, lets look at some formatting details of SSMTP assuming that you are using gmail. I'd be curious how much they vary for other email services. So, to get a "Subject" and "To" in out emails, they must be formatted correctly. The format is as follows at the very top of the email.

     To: recipient@gmail.com
     Subject: Some message


Keeping that in mind, lets look at the script I wrote. To download the whole script, go here. Let's discuss how to use it. Download it from the link above, place it in home to begin with, and run the following to elevate the scripts permissions and make the script executable.

      sudo chmod 777 send-mail.sh

After you have done that, create a signature file,  name it signature.txt. Now place the message you want to send in a file, and name it message.txt. Now run the following:

./send-mail.sh "subject" "recipient name" body-text.txt recipient-name@gmail.com                          


You will be prompted twice to accept the settings. Typing Y/y will accept them, and N/n will reject them and not send the email. After the email is sent, the message will be saved as message(date).txt in the current folder.

Make an Alias and Place in /etc/

Now that we have a script to automate email, let's run it like a real program. To do this, we will place the script and files associated with it into /etc/, and will make an alias to run the program in out ~/.bashrc file.

To start, lets make a folder for the script:

     sudo mkdir /etc/send-mail/

Now move your sent-mail.sh file into this folder along with any signatures, etc. you want. Again run the following to elevate the scripts permissions and make the script executable.

      sudo chmod 777 send-mail.sh

Now lets make an alias in our ~/.bashrc file:

           sudo gedit .bashrc

Once in ~/.bashrc, add this line to the end of the file:

     alias send-mail="/etc/send-mail/send-mail.sh"
now, simply type send-mail at your prompt and enjoy automated mail.