Terminal Email Client with Raspberry PI and Mutt
Last Updated on 9th December 2022 by peppe8o
We are all getting used to beautiful email clients. Smartphones, computers, and tablets are plenty of open source email management software to make life really easy and communicate with high-value tools.
But sometimes, when some nerds (like me) love working with a terminal, an email client inside your terminal can save looking continuously on another screen to check messages arrived.
One of the most known solutions to this need is Mutt. This is a small but very powerful text-based mail client for Unix operating systems.
From Wikipedia Mutt page:
“Though written from scratch, Mutt’s initial interface was based largely on the ELM mail client. To a large extent, Mutt is still very ELM-like in the presentation of information in menus (and in fact, ELM users will find it quite painless to switch as the default key bindings are identical). As development progressed, features found in other popular clients such as PINE and MUSH have been added, the result being a hybrid, or “mutt.” At present, it most closely resembles the SLRN news client. Mutt was originally written by Michael Elkins“
It is highly customizable and you can configure your own macros to manage email triggers. Supports both POP3 and IMAP protocols.
In this tutorial, I’m going to show you how to install and use Mutt in your Raspberry PI OS Lite (without Desktop Environment). I’ll use a cheap Raspberry PI Zero W for this guide, but it applies also to newer Raspberry Pi boards.
What You Need
As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:
- Raspberry PI Zero W (including proper power supply or using a smartphone micro usb charger with at least 3A) or newer Raspberry PI Board
- high speed micro SD card (at least 16 GB, at least class 10)
A high-speed micro SD Card is not strictly required for this software, but it is useful if you will make other projects on the same PI requiring high resources load.
Check hardware prices with the following links:
Step-by-Step Procedure
Install Operating System
We’ll start installing Raspberry PI OS Lite (headless and high performance OS). This guide applies also to Raspberry PI OS Desktop installations, using its internal terminal.
Make your OS updated. From the terminal, issue the following command:
sudo apt update -y && sudo apt upgrade -y
Install and Setup Mutt
To install Mutt, we can use the convenient aptitude package manager already available in all Rasberry PI OS distributions:
sudo apt install mutt
Next commands will create folders used to improve mutt performance with caching. The “-p” option in mkdir will create subdirectories (parents) with a single command:
mkdir -p ~/.mutt/cache/headers mkdir ~/.mutt/cache/bodies
Create a new Mutt configuration file:
nano ~/.mutt/muttrc
For a very simple and basic IMAP configuration, please use the following template. It is based on Gmail settings, so you should check IMPS and SMTP configurations for your email provider. Also, the mail tree could change based on your provider folders organization (some email providers may use, for example, “INBOX.SENT” instead of “[Gmail]/Sent Mail”.
IMPORTANT: For Gmail users, because of Gmail advanced authentication, you have to generate an app-specific password from your google account in order to make mutt work, then use the password generated instead of your Google one. If you don’t use an app-specific password with Gmail, you will get a “Login failed” error from Mutt.
Change also “email@gmail.com”, “youSecretPassword”, “email” and “User Alias” with your ones.
#================ IMAP ==================== set imap_user = email@gmail.com set imap_pass = youSecretPassword set spoolfile = imaps://imap.gmail.com/INBOX set folder = imaps://imap.gmail.com/ set record="imaps://imap.gmail.com/[Gmail]/Sent Mail" set postponed="imaps://imap.gmail.com/[Gmail]/Drafts" set mbox="imaps://imap.gmail.com/[Gmail]/All Mail" set header_cache = "~/.mutt/cache/headers" set message_cachedir = "~/.mutt/cache/bodies" set certificate_file = "~/.mutt/certificates" #================ SMTP ==================== set smtp_url = "smtp://email@smtp.gmail.com:587/" set smtp_pass = $imap_pass set ssl_force_tls = yes #================ Composition ==================== set editor = "nano" set edit_headers = yes set charset = UTF-8 # value of $LANG; also fallback for send_charset # Sender, email address, and sign-off line must match unset use_domain set realname = "User Alias" set from = "email@gmail.com" set use_from = yes set timeout = 30
Once finished, save and exit. You can now test your SMTP configuration by sending a test email directly from terminal with following command (set your test recipient address):
echo "Email test message body" | mutt -s "Subject here" recipient@example.com
You can also send email with attachment in a similar way, by appending at the end the “-a” option and file path (absolute or relative):
echo "Please find file attached" | mutt -s "Attachment test" recipient@example.com -a "/home/pi/testfile.jpg"
You can now verify also IMAP configuration by accessing your mailbox with mutt client. From terminal:
mutt
This will result in your mailbox email list:
Please find below some useful commands.
In list view:
- ENTER -> focus on selected email
- up/down ARROWS -> scroll emails
- q -> quite
- m -> compose new email (CTRL+C to interrupt composition)
- d -> mark message to be deleted (on mutt exit a delete confirmation will be requested)
- u (on a message before a marked to delete one and then “u” again) -> unmark message from deletion
In email view:
- ENTER -> scroll message down
- DEL -> scroll message up
- i -> exit from message and go back to list view
- up, down, left and right ARROWS -> navigate between different messages
Moreover, with “?” key you can get complete help showing how to use Mutt.
More advanced features can be found in Mutt Gitlab page.
Enjoy! … but …
Before the greetings, did you found the Easter Egg in this tutorial? No?!?! See carefully screenshot! Yes? Write down in comments and challenge your friends to find it!
very good article but what would you do when testing mutt to send an email you got the error message:
Error sending message, child exited 1 ()
?
Hi André. This error warning is specific to Mutt and this line is simple warning that something went wrong, but doesn’t refer the reason. You should check Mutt logs, an help can be Mutt official debug tips page