SMTP & mailutils: How to send your mails via the Linux terminal.
Tags: cloudcomputing, linux, devops, smtp
Published: 2024-04-28 22:08:10

 Table of Contents
 1. Introduction
 2. Prerequisite
 3. Generate Google App password
 4. Launch your EC2 Instance
 5. Configuration of SSMTP
 6. Write and send your mail
 7. Conclusion

## 1. Introduction

Imagine being able to compose, attach files and send emails directly from your CLI using a few concise Linux commands. Well, the Linux terminal offers a streamlined and efficient alternative for sending emails using mailutils and the Simple Mail Transfer Protocol (SMTP). 

What is SMTP?
SMTP stands for Simple Mail Transfer Protocol. It is a protocol used by email servers to send and receive email messages over the Internet. For instance, the email you send is transferred over the internet from one server to the other using SMTP. 

What is `mailutils`?
The `mailutils` is a set of versatile utilities that manages emails directly from the CLI or Linux terminal using SMTP and other protocols. Some of the utilities include: 
`mail`: This utility allows you to read and send emails interactively from the command line. 
`movemail`: A utility that moves mail from one location to another.
`frm`: A utility to list the subject lines of email in a mailbox.
`readmsg`: A utility to read and print messages.

This article will provide a guide on how to send mail seamlessly via the Linux terminal using SMTP and mailutils.

Now, let's get started.

## 2. Prerequisite
For this article, you will need:
i. A Google account for your app password generation
ii. A Linux terminal. I used the AWS console. You can sign up for a free 1yr tier account [here](https://aws.amazon.com/)

## 3. Generate Google App Password
On your Google account, locate the settings icon and click on manage your Google account. Take a look at the image attached below:

![Google Account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvc0a9kw4mq9r2ph2fon.png)

Next, go to the security settings and turn on your 2-step verification.
![Two-step verification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18rq4589exi7xlzcx4w1.png)


Scroll down the 2-step verification page and click on the App passwords. Type a name for your app and a password will be generated automatically. Make sure to copy and save this password as we will need it later.
See the images attached below:

![App Password](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/634dt1yvopzzktuu9r03.png)

![Generated App Password](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k3te2w9sskf9elvw3rh6.png)

## 4. Launch EC2 Instance
Once you log in on your AWS console, in the search bar, type EC2 and click on EC2. Click on instance and choose a name for your instance. See image below

![EC2 Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ysbwf1obaacgv5ykofq.png)

Select your AMI. For this article, we will use the Ubuntu AMI. So, go ahead and select your Ubuntu AMI. 

![ubuntu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjsh4pg5hd1wyomp86si.png)
Click on Launch instance.

Next, click on instance ID. See the image below

![instance id](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d669vhfakjwfh5xlm7i2.png)
Then connect to the terminal

![Connect to your terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ohnwh708p6hk79trh2l.png)

## 5. Configuration of SMTP
i. On your terminal, run `sudo apt update`

![Sudo apt update](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/778855697yv2yo0bw53c.png)
This command is run before installing new software with apt install to ensure you get the latest versions of packages and all their dependencies.

ii. Install mailutils 
  `sudo apt update mailutils`

![mailutils](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpp5jf36utwny6kqxkeb.png)
Please note that while the installation is running, a postfix configuration will pop up, select the internet site option using your arrow keys and then click ok to finish up with the installation.

iii. Install SSMTP

Why install SSMTP?

SSMTP is a simple mail transfer agent (MTA) used to send emails from a Linux system. In other words, it forwards emails to another SMTP server for delivery.

To install SSMTP
`sudo apt install ssmtp`


![SSMTP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1kf6sfeb4t4ub4dawpse.png)

iv. Configuration of SSMTP file
To configure the ssmtp file, `sudo nano /etc/ssmtp/ssmtp.com`

![Conf file command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tir9gamdn87iacs2bjks.png)

With the arrow keys, edit your ssmtp configuration file to look exactly like the images attached below

![SSMTP Conf File](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5tov9l98nhai1xnxo4g.png)

![SSMTP Conf File](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6yczf03ityb3ebyv4yhu.png)
In this configuration file,
Your root should be your email address. 
Your AuthUser should be your email address. 
Your AuthPass should be the 16-character string generated from the app password in your Google account. When typing it in, remember not to include the spaces.
The rewriteDomain should be gmail.com, but if you are not using Gmail's SMTP server to send your email, replace gmail.com with your domain.
Lastly, uncomment the FromLineOverride as shown in the image above. 
Save your file.

## 6. Write and send your mail

To send your mail, run the command below
`echo "I'm a Cloud Engineer" | mail -s "Hello there!" your email address`
![Send email](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtyyalnh31b9zjmyu64l.png) 
Once this is done, check your Gmail to see the mail.

![Gmail inbox](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rijxw1j1tuff1pnxg1x.png)

This is how to use SMTP and mailutils to send emails from the Ubuntu terminal.
As soon as you are done following this guide, please terminate your EC2 instances to avoid incurring costs from AWS. Let the images below guide you on how to terminate your instance.

Go back to your console where you have your instance ID. Expand the Instance state box and click on terminate instance

![Terminate Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3bnelp90dblri0fe33wh.png)

Once this is done, you will find a pop-up like the image image below

![Successful instance termination](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzr9bjhrs9nzg8hz5eek.png)

## 6. Conclusion

Congratulations on successfully setting up your Linux terminal to send emails using SMTP and mailutils! You've learned how to configure your environment step by step, from generating a Google App password to launching an EC2 instance and configuring SSMTP.

Sending emails from the command line can be a powerful and efficient way to handle communications, especially in automated scripts or remote environments. By following this guide, you've gained valuable insights into how SMTP works and how to leverage it within your Linux terminal.

Your feedback is important to me! If you encounter any issues or have suggestions for improvement, please don't hesitate to share your thoughts. Your insights can help refine this guide and make it even more helpful for others.