• Get In Touch
October 16, 2015

How to Send Email from the Command Line with MSMTP and Mutt

Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on.
View Plans

Overview

Sometimes you might need or want to send email from the command line. You might generate a report from a database or your application and want to send the generated report via email. Assuming that the report is generated manually via a scipt and invoked using crontab.

In this tutorial we’ll learn how-to install MSMTP and Mutt. We’ll use MSMTP to relay our email to Gmail. If you want to follow this tutorial using Gmail a email account please create a new email address to be used on your server and don’t use your current gmail account, one of the reasons is that the password to the Gmail account will be stored in plain text by default. You can use pgp to store your password securely. Another reason is that the account that you will use to send email from the server might get locked due to the different location between you and the server.

If you find difficulties logging in from your server to Gmail, you might want to follow steps provided in Google Support Site.

We can use MSMTP to send email but unfortunately we cannot include atatchments, so We’ll use Mutt to help us send email with attachments from the command line. We’ll use Ubuntu 14.04 on this tutorial but any Ubuntu release and even Debian release should be able to follow this tutorial.

Updating Server

Before we install MSMTP and Mutt we’ll update our server to the latest update. If you are installing MSMTP and Mutt on an existing server, make sure it’s ok to update your server to the latest release.

$ sudo apt-get update
$ sudo apt-get upgrade

Installing MSMTP

Now it’s time to install MSMTP. You can use command below to install MSMTP.

$ sudo apt-get -y install msmtp

We need to have a file that contains Certificate Authority (CA) certificates so that we can connect using SSL / TLS to the email server. You can check whether your server already has a ca-certificates package installed or not using command below :

$ dpkg -l | grep ca-certificates
ii  ca-certificates                     20141019ubuntu0.14.04.1          all          Common CA certificates

If a ca-certificates package is already installed you will get output similar to above output similar to output above.
If you get blank output it means that ca-certificates package is not installed, and you need to install it. You can install this package by running this command :

$ sudo apt-get -y install ca-certificates

Configuring MSMTP

Create an MSMTP configuration on /etc/msmtprc with the content below. You will have to enter your Gmail username and password on this file.

# 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
account        gmail
host           smtp.gmail.com
port           587
from           @gmail.com
user           @gmail.com
password       

# Gmail
account        anotherprovider
host           smtp.anotherprovider.net
port           587
from           @anotherprovider.net
user           @anotherprovider.net
password       


# Set a default account
account default : gmail

This configuration assumes that you have more than mail configuration. That’s why we use account gmail and also account default : gmail. If you use a different name make sure you set which account that you want to use as default.

Installing Mutt

To install Mutt, you can use command below.

$ sudo apt-get -y install mutt

Mutt installs Postfix as one of its dependencies. You will have to manually select the package configuration. You can select No configuration.

Configuring Mutt

We will create a local configuration for Mutt on ~/.muttrc. This file might not exist yet, you can create this file with the content below

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

You need to change realname and from above. realname is shown as sender on recipient, while from is the email address that you’ll use to send the email.

Sending Email Without Attachment

Now the fun part. To send the email using msmtp is really easy. The easiest syntax that you can use is :

$ echo "Hello this is sending email using msmtp" | msmtp recipent@domain.com

If you want to specify which configuration that you want to use, you can use -a option :

$ echo "Hello this is sending email using msmtp" | msmtp -a gmail recipent@domain.com

The command above will send email without a subject, and with body / content “Hello this is sending email using msmtp”. If you want to send email with subject then you can use this command :

$ echo "Subject: Hello this is subject" | msmtp recipient@domain.com

The command above will send email with a Subject but without body or email content. What about a complete email? you can use command below:

$ printf "To: @domain.comnFrom: @gmail.comnSubject: Email Test Using MSMTPnnHello there. This is email test from MSMTP." | msmtp recipient@domain.com

Of course you don’t have to put your email directly on command line. Another way to create complete email like what we just did earlier is to create a file with the contents below. Give the file name email.txt

To: @gmail.com
From: @gmail.com
Subject: Email Test using MSMTP from File

Hi,
This is an email test from file.

To send this email you can use command :

$ cat email.txt | msmtp -a default recipient@domain.com

Sending Email With an Attachment

So far you’ve learned how to send email using msmtp, now we will use mutt to send email with an attachment. You might be asking why do we need both msmtp and mutt? The answer is that msmtp is an Mail Transfer Agent (MTA) and only knows how to send email but it doesn’t know how to create the attachment. Creating an email as MIME is more of Mail User Agent (MUA) function and that’s why we use Mutt in this tutorial.

We will install logwatch to create report that we will attach on our sample. Actually logwatch has options that can send report directly to email. We only use logwatch report as an example on this tutorial.

Now, let’s install logwatch first :

$ sudo apt-get -y install logwatch

Now we can generate the log using logwatch :

$ sudo logwatch --detail High --filename report.txt

the report.txt file might be owned by root. We’ll change the ownership so our current user is the owner of the report.txt file.

$ sudo chown `whoami`:`whoami` report.txt

Now the report is ready. Let’s send the attachement using Mutt.

$ mutt -a report.txt -s "Logwatch Report" -- recipient@domain.com recipient2@domain.com < /dev/null

The command above will send email with an attachment but with a blank body since we put the content from /dev/null. If you want to send complete email with subject, to, and body, create a file that contains the message that you want to put on the email body. Then you can change /dev/null with the filename. For example, if you put the body of the email in the email message.txt file you can use command below :

$ mutt -a report.txt -s "Logwatch Report" -- recipient@domain.com recipient2@domain.com < emailmessage.txt

Summary

In this tutorial we learned how-to send email from command line using MSMTP and Mutt. We learned how-to send email with and without attachments. You can use methods that we already learned on your scripts that require email sending functionality.

References

  1. MSMTP Manual
  2. Mutt Manual
Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on.
View Plans

Share this Article!

Related Posts

Node.js Authentication – A Complete Guide with Passport and JWT

Node.js Authentication – A Complete Guide with Passport and JWT

Truth be told, it’s difficult for a web application that doesn’t have some kind of identification, even if you don’t see it as a security measure in and of itself. The Internet is a kind of lawless land, and even on free services like Google’s, authentication ensures that abuses will be avoided or at least […]

Node.js and MongoDB: How to Connect MongoDB With Node

Node.js and MongoDB: How to Connect MongoDB With Node

MongoDB is a document-oriented NoSQL database, which was born in 2007 in California as a service to be used within a larger project, but which soon became an independent and open-source product. It stores documents in JSON, a format based on JavaScript and simpler than XML, but still with good expressiveness. It is the dominant […]

Using MySQL with Node.js: A Complete Tutorial

Using MySQL with Node.js: A Complete Tutorial

Although data persistence is almost always a fundamental element of applications, Node.js has no native integration with databases. Everything is delegated to third-party libraries to be included manually, in addition to the standard APIs. Although MongoDB and other non-relational databases are the most common choice with Node because if you need to scale an application, […]

Node.Js Vs Django: Which Is the Best for Your Project

Node.Js Vs Django: Which Is the Best for Your Project

Django and NodeJs are two powerful technologies for web development, both have great functionality, versatile applications, and a great user interface. Both are open source and can be used for free. But which one fits your project best? NodeJs is based on JavaScript, while Django is written in Python. These are two equally popular technologies […]

Nodejs Vs PHP:  Which Works Best?

Nodejs Vs PHP: Which Works Best?

Before getting into the “battle” between Node.js and PHP we need to understand why the issue is still ongoing. It all started with the increased demand for smartphone applications, their success forcing developers to adapt to new back-end technologies that could handle a multitude of simultaneous requests. JavaScript has always been identified as a client-side […]