Sending mail from the command line! Arguably the best one-liner ever

August 5, 2022

There are many ways to send mail from the command line. There are many applications to accomplish that, there are many one-liners available. What makes my contribution unique is the integration with a file picker for attachments, and an address picker, to select an email address in the very same command line.

An address picker? Are you for real, dude?

For real real! Not for play play.

This was tested to work on Linux. These are the dependencies: nnn, dialog, mutt, abook, awk and fzf. So first, do proceed to install those.

nnn is a file manager that works like a charm as a file picker. dialog is not really necessary, it is my choice to get input from the user. Note that dialog can be used itself as a file picker, only that the names get very easily off screen. mutt is the best email client ever. abook is a simple ncurses agenda to store addresses, phone numbers, email addresses, and other personal info, of people. awk is a utility to process text. And fzf is a fuzzy finder that we will be using to select the desired adress when presented with multiple options.

Include this ugly chunk in your .bashrc and source it:

# press F12 to select a file:
selectafile()
{
 FILE=$(nnn -p -)
# an alternative using dialog instead:
# FILE=$(dialog --stdout --title "Please choose a file" --fselect $PWD/ 14 48)
 READLINE_LINE=$READLINE_LINE$FILE
 READLINE_POINT=${#READLINE_LINE}
}

bind -x '"\e[24~":selectafile'


# press F10 to select email address
selectemail()
{
USERINPUT=$(dialog --title "Select an email address from abook" \
--backtitle "Tool provided by gasconheart" \
--inputbox "Type a name or a text string " 8 60 \
3>&1 1>&2 2>&3 3>&- )
SELECTEDONE=$(abook --mutt-query $USERINPUT | awk '{ print $1 }' | awk 'NF' | fzf --prompt "Select one email address: ")
echo
READLINE_LINE=$READLINE_LINE$SELECTEDONE
READLINE_POINT=${#READLINE_LINE}
}

bind -x '"\e[21~":selectemail'

It you use a different shell, do your homework. I use BASH. Anyway. Do source the aforementioned chunk somehow.

This will bind the F12 key to a file selector so you can attach said file, and the F10 key to an email address selector.

Envision a BASH line like this one:

echo "Hello, this is the body of an email." | mutt -s "hello there" gasconheart@sdf.org -a importantdocument.pdf

The chunk you source saves you from typing the email of the sendee, and the file name to be attached. Right after you type the subject, press F10. You will have to type the name, surname, part of one of those, or a string of the email address you may remember. Then the function searches the abook database, and fzf presents you with a list of matches, for you to select one with the arrow keys and enter.

Then, after you type -a, instead of typing the path and name of the file you want to attach, just press F12. nnn will fire up for you to select a file with its full path. Easy peasy lemon squeazy.

Passing the output of dialog to a variable was an ordeal.


Flag Counter

Comment Form is loading comments…

Back · Email