How to send emails from Linux Terminal

Endrit Qerreti

Endrit Qerreti

In this tutorial you will learn how to send an email from your terminal in Linux.  

Step 1  - Update your system

Before we start with the steps below, make sure your system is updated, run the command below in order to do so

sudo apt update

Step 2  - Install mailutils Package

Mailutils is a utility for mailhandling in Linux. We'll be using this package in order to be able to handle emails

Run the command below to install mailutils

sudo apt install mailutils

Step 3 - Configure ssmtp.conf

Once you have installed mailutils on your system, now you need to configure the ssmtp configuration file.

All you have to do is open the ssmtp.conf via a text editor such as nano

nano /etc/ssmtp/ssmtp.conf

And add these configs to your file

root=postmaster
mailhub=smtp.gmail.com:587
hostname=oht
AuthUser=Your_Email_Address
AuthPass=Your_Auth_Pass 
FromLineOverride=YES
UseSTARTTLS=YES

The configuration above is suitable for sending emails via your gmail address, if you want to use your server or any other domain replace smtp.gmail.com:587 with your own.

Once done editing ssmtp.conf press CTRL + X to save the changes.

Note "AuthPass" Is not the password of your email address, the password in this case is an App password, which gives access to mail utility in order to send emails using your account.  Check out our tutorial how to generate an app password on Gmail

Step 4 - Send emails

Now that you have configured the ssmtp you are good to go and start sending emails right from your terminal.

For example to send a test email, run the following command

echo "Replace this with your email body" | mail -s "Replace this with your subject" to-this-email-address@gmail.com

Replace the email address shown on the config above with the email address where you want to send the email.

And you should receive the email to your Gmail Inbox.

If you don't receive the email make sure you configured the ssmtp.conf file correctly, you can also check logs to see what's causing the issue for the email to not be delivered.

tail -f /var/log/mail.log

Conclusion

In this tutorial you learned how to send an email to any email address by using the mail -s command in Linux.