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.
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
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
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.
# 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.
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.
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
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 email@example.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 firstname.lastname@example.org
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 email@example.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 firstname.lastname@example.org
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
Subject: Email Test using MSMTP from File
This is an email test from file.
To send this email you can use command :
$ cat email.txt | msmtp -a default email@example.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.
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 :
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.
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...
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...