Bash/Shell Script to Send Email In Linux – Howto, Example

Sending Emails from Bash/Command Line

This article will show you several ways to send an email from the Linux command line/shell – as well as from Bash/Shell scripts.

Why would you want to send an email from the command line? Probably not to communicate – most people use an email client with a nice user interface to send messages to each other day to day – but you may want to have your computer send an email notification when a task completes or when an event occurs.

Such email alerts are commonly used to alert on low disk space or notify someone if an error occurs, power is lost – you could even write a notification to provide you a daily summary of new user signups to your latest app. Here are several tools that will let you do this – with examples.

A Note on Sending Mail

The examples in this article will all assume you are using an external SMTP server to send mail. This may be a dedicated email sending service like Amazon SES or MailGun, or you may send emails from your online email provider (Gmail, Outlook.com) using the SMTP details they provide.

This article does not cover setting up and sending mail directly from your own SMTP host for one simple reason – your messages are likely to be blocked if they are coming from a residential IP address, and you will have a hard time troubleshooting whether your email alerts are working correctly and being blocked, or just not working.

Choosing the Right Program

Many software packages can send email – a large number. I’m not going to try and cover all of them – you’re here looking for something that just works, so…

MSMTP is an ideal tool for this job, so I’ll keep the focus of this article on how to use that program.

Previously, SSMTP was my preferred choice, but the development of that package has stopped – MSMTP is a good replacement.

Other options include the sendmailmail, and mutt commands. All will work, but MSMTP is easy to use and configure and can maintain an independent configuration that won’t interfere with system mail.

Using MSMTP to Send Email

MSMTP is a program that sends email (and only) sends email – making it perfect for sending emails from Bash scripts.

Once you have installed MSMTP, you can view the user manual by running:

man msmtp

Install MSMTP

On Debian/Ubuntu-based systems, run the following commands to install the required MSMTP package and the ca-certificates package if it isn’t installed already:

sudo apt update
sudo apt install msmtp ca-certificates

The sudo command is used to run commands with root/administrative privileges throughout this article.

Configure MSMTP

MSMTP supports per-user configuration or a global system configuration – we’ll set this up with a global configuration so that all services can use MSMTP to send email.

MSMTP comes with an example configuration file we can use as a template – we’ll need to make a copy of it in the /etc/ folder for use:

sudo cp /usr/share/doc/msmtp/examples/msmtprc-system.example /etc/msmtprc

Edit the configuration file using the nano text editor:

sudo nano /etc/msmtprc

Now, you can fill in the details of the SMTP server you will use to send email. You can define several mail accounts if you wish to send from different servers or addresses.

Below is a sample configuration with two Gmail addresses:

# Set default values for all following accounts.
defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log

# Gmail 1
account        gmail1
host           smtp.gmail.com
port           587
from           username1@gmail.com
user           username1
password       password1

# Gmail 2
account        gmail1
host           smtp.gmail.com
port           587
from           username2@gmail.com
user           username2
password       password2


# Set a default account
account default : gmail1

Save the file – MSMTP is ready to use.

Send an Email

Once MSMTP is set up, you’re ready to go. No additional software is needed to send email from MSMTP – you can simply use the printf command to construct your email and pipe it to the msmtp command:

printf "Subject: Testing\nHello there!." | msmtp -a gmail2 recipient@example.com

“Subject: Testing\nHello there!.” – This is the email which will be sent. A subject is defined using Subject: followed by the subject text. \n defines a New Line, which is then followed by the email body text. Additional \n newlines can be added where required to construct a plain text message with formatting.

The -a option is used to set which email account the email will be sent from – if it is not set, the default will be used.

Send an Email with an Attachment using Mutt

Sending an email with an attachment is a bit trickier – additional software is needed to make it easier to do. mutt is an email client which adds this functionality with minimal fuss for use in scripts.

Install it by running:

sudo apt install mutt

mutt may install the Postfix mail transfer agent as a dependency – it will ask for a mail configuration. You can safely choose No configuration – we do not need postfix to be configured. As mentioned earlier in this article, we do not want to send mail directly from this system as it’s likely to not reach its destination due to spam filtering.

Like MSMTP, mutt has per user and global configurations. We’ll use a global configuration so that all users/services can send mail with attachments. Create and edit the file using the nano text editor by running:

nano /etc/muttrc

Enter the below configuration (with values replaced with those from your own mail configuration):

set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="Your Name"
set from=username1@gmail.com
set envelope_from=yes

mutt is now configured and can be used to send an email with an attachment:

mutt -a attachment.txt -s "My Subject" -- recipient@example.com < "Email message body"

Per-User vs. Global Configuration

Above, global configurations have been defined. If you want the configuration to only apply to certain users – so that each user can send only from their own user account, for example – place the configuration in the following files in the users home directory instead:

~/.msmtprc
~/.muttrc
SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *