How to Setup Email Client in Linux Terminal to Send Emails

As a Linux administrator, sending emails using terminal could be handy when it comes to automated alerts and when you are looking to automate tasks. Even if you are not yet on that level where you automate tasks, still learning to send emails via terminal could help you send bulk emails or send useful notifications. 

There are plenty of ways to send emails in Linux but I am going to discuss only the one which works for me the best and which I have been using very often. 

How to send emails using msmtp

Step 1: First off, update the repository and install msmtp:

$ sudo apt update
$ sudo apt install msmtp
Setup Email Client In Linux 1

Step 2: Go to your Gmail and enable 2-factor authentication if it is not already enabled:

Setup Email Client In Linux 2

Step 3: Setup App Password, for that matter search your Google account:

Setup Email Client In Linux 3

Now set your App Name:

Setup Email Client In Linux 4

You will now get your App Password:

Setup Email Client In Linux 5

Copy this password, you are going to need to use it shortly.

Step 4: Open ~/.msmtprc by using nano:

$ sudo nano ~/.msmtprc

Once, its opened add the following:

defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log

account default
host smtp.gmail.com
port 587
from [email protected]
user [email protected]
password your_app_password
Setup Email Client In Linux 6


CTRL+O to save it and CTRL+X to exit out of the nano editor.

Step 5: Give user related permissions:

$ sudo chmod 600 ~/.msmtprc

Setup Email Client In Linux 7

If you don’t know what 600 means then I recommend you read this linux file permission article.

Step 6: Now go ahead and send a test email:

$ echo "Subject: Sending Test Email" | msmtp --debug --from=default -t sent_email_address 
Setup Email Client In Linux 8

Step 7: Verify if you have received the email on the sent address:

Setup Email Client In Linux 9

Conclusion

We have learned how to set up a simple email client in Linux which will help you send automated alerts and notifications directly to your email inbox. Any change in your server and you are informed right away. Just one clarification is that I used ~/.msmtprc for the current user, if you want to use it for the entire system then create /etc/msmtprc.