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:
- Create a application specific password for Mutt on Google
- Install Mutt
- Configure Mutt
- Test Mutt
Okay now we know what to do lets get going.
1. Create a application specific password for Mutt on Google
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:
- Log into Gmail
- Click on your photo in the top right corner
- Click on the button “My Account“
- In the left menu bar click on “Sign into Google“
- Under “Password & sign-in method” (first block on the right side) click on “App passwords“
- You are required to login again
- From the drop down list “Select App” select the option “Mail“
- From the drop down list “Select Device” select the option “Other (Custom name)“
- Enter a name for the server, for example “my-vps“
- Click the button “Generate“
- The application specific password is generated, write it down in a safe place as it will only be displayed once
Tip: I use KeyPass to safely store sensitive information like passwords.
2. Install Mutt
Okay, now we are ready to install Mutt. Which is very easy and straight forward to do.
Execute the following steps:
- Install Mutt:
sudo apt-get install mutt
- Create the following working folder:
mkdir -p ~/.mutt/cache
- Create the config file:
3. Configure Mutt
Ready to configure Mutt? Lets go.
- Open the configuration file:
I use VIM, feel free to use your favorite editor
- Add the following contents to the configuration file and change the values between brackets (“”):
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 = "smtp://[gmail username]@[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.
3. Test Mutt
Easy right? Lets test if it works. Just execute the following command:
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
Warning “GPGME: CMS protocol not available”
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?
4 thoughts on “Setting up Mutt to send mail using Gmail with 2FA set”
Pingback: Initial server setup Ubuntu 17.10 | Adventures of a space monkey
For macOS also it needs adding
`set smtp_authenticators = ‘gssapi:login’`
Otherwise it gives the following error when sending: “No authenticators available”
Thx for sharing
You’re a life saver!! Just had a Raspberry Pi server with exim4 stop sending e-mails for no apparent reason. Read about mutt, configured it with your help, and then I only had to change “mail” to “mutt” in my script to be up and running again!