I love commandline applications. A while ago I swapped my mail client for NeoMutt, which is an open source commandline mail client. Setting up NeoMutt by hand is tough, so I used a tool called mutt-wizard. This tool allows you to set up multiple accounts for a single NeoMutt instance.

While NeoMutt works very well, there is one thing that bothers me. mailto URLs, that you encounter in your web browser, for example. Without any prior configuration, these URLs do not redirect you to NeoMutt to start writing a mail. While there are solutions that allow you to set up NeoMutt for these mailto links, they do not work for NeoMutt instances with multiple accounts. Therefore, I created a script to fix this issue.

The script

NOTE: this script probably works right of the bat if you use mutt-wizard. If you don’t, you have to update the script a bit.

NOTE: this script uses rofi, an alternative to dmenu. dmenu can be used instead by updating the script.

This script opens up a prompt to select an email account, and opens up NeoMutt with the correct email address.

1
2
3
4
5
6
7
#!/bin/sh
MUTT_CONFIG="$HOME/.config/mutt"
EMAILS=$(cd "$MUTT_CONFIG/accounts" && find . | tail -n +2 | sed "s/\.\///g; s/\.muttrc$//g")
CHOICE=$(echo "$EMAILS" | rofi -dmenu -i -l "$(echo "$EMAILS" | wc -l)" -p "Select address to send from")
[ -z "$CHOICE" ] && exit
ACCOUNT_CONFIG_FILE=$(find "$MUTT_CONFIG/accounts" -name "*$CHOICE*")
exec "$TERMINAL" -e neomutt "$@" -F "$MUTT_CONFIG/muttrc" -F "$ACCOUNT_CONFIG_FILE"

Firstly, this script establishes the NeoMutt configuration directory. Then, it looks for all the accounts in the accounts folder and trims the results, to only retain the email addresses. Then, it pipes the email addresses into rofi to make a selection. Finally, it opens a terminal with the selected account to start writing the email.

Now, this script on its own does not do anything, as we need to supply it with the target email address. This is done by the mailto link. To take advantage of this, we write a desktop entry and put it in ~/.local/share/applications/mail.desktop

[Desktop Entry]
Type=Application
Name=Mail
Exec=mailto %u

NOTE: for this desktop file, the script from before should be called mailto and must be put in the PATH!

There are three things left to do:

  1. Install the desktop file, for which I would like to link to the beautiful Arch Wiki.
  2. Set the default handler for mailto links to our new desktop file. This can be done by editing ~/.local/share/applications/mimeapps.list and adding the following:
[Default Applications]
x-scheme-handler/mailto=mail.desktop;

This registers mail to the mailto handler. To confirm successful registration, run xdg-mime query default 'x-scheme-handler/mailto'. This should output mail.desktop.

  1. Set up the Mail “application” as the default in your program.

Hope this helped!

Yours,

Pim