I needed to be able to send mail from the command line on one of my servers. A quick way to do this without setting up a mail server is using Mutt to send mail using Gmail. In this post I will give a short write-up how to install and configure Mutt on Ubuntu. As I have two factory authentication setup on Google an app specific password is required instead of the regular Google account password.
You will need to execute the following steps:
Okay now we know what to do lets get going.
If you do not have two factor authentication (2FA) set on your Google account you can skip this step. That said, if you don’t have 2FA set then I would strongly encourage you to do so, this is an important extra layer of security to ensure your account cannot be hacked.
First step is setting up an application specific password for Mutt in Google.
Execute the following steps:
Tip: I use KeyPass to safely store sensitive information like passwords.
Okay, now we are ready to install Mutt. Which is very easy and straight forward to do.
Execute the following steps:
sudo apt-get install mutt
mkdir -p ~/.mutt/cache
touch ~/.muttrc
Ready to configure Mutt? Lets go.
vim ~/.muttrc
set realname = "[first and last name]" set from = "[gmail username]@gmail.com" set use_from = yes set envelope_from = yes # Ensure TLS is enforced set ssl_starttls = yes set ssl_force_tls = yes # Protocol: smtp for TLS (587), smtps for SSL (465) set smtp_url /cdn-cgi/l/email-protection" data-cfemail="02656f636b6e2c616d6f42716f76722c656f636b6e2c616d6f">[email protected]:587/" set smtp_pass = "[gmail password or app specific password if using 2FA]" set imap_user = "[gmail username]@gmail.com" set imap_pass = "[gmail password or app specific password if using 2FA]" set imap_keepalive = 900 set folder = "imaps://imap.gmail.com:993" set spoolfile = "+INBOX" set record = "+[Gmail]/Sent Mail" set postponed = "+[Gmail]/Drafts" set header_cache = "~/.mutt/cache/headers" set message_cachedir = "~/.mutt/cache/bodies" set certificate_file = "~/.mutt/certificates" set move = no # Needed to get and display mail (imap-fetch-mail) set editor = "vim" set charset = "utf-8" set record = ""
Now you are ready to send, receive and read email using Mutt.
Easy right? Lets test if it works. Just execute the following command:
mutt
The first time you might be prompted to accept the SSL certificate; press “a” to always accept the certificate from Gmail. Now your Gmail inbox is displayed. Pretty cool right?
Lets test sending an email from the command line.
echo "This is a test message." > ~/.mutt/tst-msg.txt mutt -s "Test Mutt" [email address] < ~/.mutt/tst-msg.txt
Now with attachment!
mutt -s "Test Mutt" [email address] < ~/.mutt/tst-msg.txt -a ~/.mutt/tst-msg.txt
Alternatively use “ echo ” to create the body of the email.
echo "This is a test message." | mutt -s "Test Mutt" [email address] -a ~/.mutt/tst-msg.txt
This blog post was written when I was installing Mutt on Ubuntu 16.10.
When installing Mutt on Ubuntu 17.10 I got the warning “GPGME: CMS protocol not available” when sending mail from the command line. It works further as expected.
To suppress the warning you can pass the following command line parameter.
-e "set crypt_use_gpgme=no"
Or add the parameter to the Mutt config file.
Pretty cool easy setup to enable sending emails from the command prompt. I hope you found this post helpful. Any comments or suggestions?