SMTP & mailutils: How to send your mails via the Linux terminal.

florenceokoli

Florence Okoli

Posted on April 28, 2024

SMTP & mailutils: How to send your mails via the Linux terminal.

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

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

Next, go to the security settings and turn on your 2-step verification.
Two-step verification

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

Generated App Password

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

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

ubuntu
Click on Launch instance.

Next, click on instance ID. See the image below

instance id
Then connect to the terminal

Connect to your terminal

5. Configuration of SMTP

i. On your terminal, run sudo apt update

Sudo apt update
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
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

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

Conf file command

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

SSMTP Conf File

SSMTP Conf File
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
Once this is done, check your Gmail to see the mail.

Gmail inbox

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

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

Successful instance termination

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.

💖 💪 🙅 🚩
florenceokoli
Florence Okoli

Posted on April 28, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related