How to send emails from a bash script using ssmtp?

I was recently working on a script to monitor some the TCP connections on one of my DigitalOcean servers and I wanted to receive an email every time the connections count was greater than 200. As I wanted to use SMTP I decided to go with ssmtp.

Here is a mini-tutorial on how to install and configure ssmtp and send emails directly from your command line or bash scripts.

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
June 12, 2020
Accepted Answer


SSMTP is a tool that delivers emails from a computer or a server to a configured mail host.

SSMTP is not an email server itself and does not receive emails or manage a queue.

One of its primary uses is for forwarding automated email (like system alerts) off your machine and to an external email address.


You would need the following things in order to be able to complete this tutorial successfully:

Installing SSMTP

In order to install SSMTP you’ll need to first update your apt cache with:

  1. sudo apt update

Then run the following command to install SSMTP:

  1. sudo apt install ssmtp

Another thing that you would need to install is mailutils, to do that run the following command:

  1. sudo apt install mailutils

Configuring SSMTP

Now that you have ssmtp installed, in order to configure it to use your SMTP server when sending emails you need to edit the SSMTP configuration file.

Using your favourite text editor opent the /etc/ssmtp/ssmtp.conf file:

  1. sudo nano /etc/ssmtp/ssmtp.conf

You need to incldue the your SMTP configuration:


Save the file and exit.

Sending emails with SSMTP

Once your configuration is done, in order to send an email just run the following command:

  1. echo "Here add your email body" | mail -s "Here specify your email subject"

You can run this directly in your terminal or include it in your bash scripts.

Sending A File with SSMTP (optional)

If you need to send files as attachments, you can use mpack.

To install mpack run the following command:

  1. sudo apt install mpack

Next, in order to send an email with a file attached, run the following command

  1. mpack -s "Your Subject here"

The above command would send an email to with the attached.


SSMTP is a great and reliable way to implement SMTP email functionality directly in bash scripts.

For more information about SSMTP I would recommend checking the official documentation here.

Hope that this helps! Regards, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel