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
Step 2: Go to your Gmail and enable 2-factor authentication if it is not already enabled:
Step 3: Setup App Password, for that matter search your Google account:
Now set your App Name:
You will now get your App Password:
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
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
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Â
Step 7: Verify if you have received the email on the sent address:
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.
Check our LinkedIn company page