1

What options do I have for adding recipients to email when using mailx (or variations thereof)? The ones I know about are:

  • Manually type email address character by character from memory
  • Enter an alias that I have stored in .mailrc earlier

Are there other options like an address book created from email addresses I have sent email to earlier, that can be navigated with the cursor keys or narrowed down by auto-complete?

2 Answers 2

1

Forthrin, I could not find an elegant solution to this problem. But I did a little thinking, and a possible workaround would be outlined like this:

Make a directory called 'recipients', create dummy files named after the email addresses you desire to send to, use auto-complete inside this directory.

mkdir recipients
cd recipients
touch [email protected]
mail -s "stuff" '[email protected]' < logfile.txt

Single quotes here to help avoid escaping the '\' that would appear.

I know it's not pretty, but you could doll this up by pulling from a text file of email addresses and cobbling an auto-complete together that way. I'm unsure how to go about that, though. Someone smarter than me could chime in.

Further, you can scour your previous history and maillog to look at the mails you've already sent and grab those addresses, then toss them into the recipients folder. Now you have a folder of recipients with which tab completion will work (satisfying your auto-complete request).

Not the best solution, but I don't see any feature in mailx for address books.

1
  • Thanks for sharing an amusing solution! I'll keep the question open for further (creative) suggestions.
    – forthrin
    Commented May 18, 2018 at 16:29
0

A solution would be to use a wrapper to save addresses and bash completion to retrieve them:

address_book=${HOME}/.address
mailx() {
    #loop through all args 
    for i; do
        # does this look like an email address
        if grep -xqE '[[:alnum:]_.+\-]+@[[:alnum:]\-]+.[[:alnum:].\-]+' <<< "${i}" && ! grep -xqF "${i}" "${address_book}"; then
            echo "${i}" >> "${address_book}"
        fi
    done
    /usr/bin/mailx "${@}"
}
_mailx_completion() {
    if [[ ! -f ${address_book} ]] ; then
        return
    fi
    # Only add each email once
    emails=($(grep -xvf<(echo "${COMP_WORDS[*]}"|tr ' ' '\n') "${address_book}"))
    if [[ ${#emails[@]} -eq 0 ]] ; then
        return
    fi
    # list emails
    COMPREPLY=( $(compgen -W "${emails[*]}" "${COMP_WORDS[${COMP_CWORD}]}") )
}                                                      
complete -F _mailx_completion mailx

Add to your ‘.bashrc’

Explanation:

We use a bash function to intercept the call to mailx. This function searches through the command line arguments looking for email addresses. After finding one it will then check our "address book" to see if it isn't there already - if it isn't it'll then append it to the list. Finally we call mailx with the original arguments.

The second part is the bash autocomplete function - this is called whenever you press the Tab key. Bash will read email addresses from ~/.address whenever you press tab after typing mailx. The extra logic is to remove email addresses from the suggestion list after being added to the command line.

To enable all that, either add the above to your .bashrc file.

3
  • Very interesting! Could you explain step-by-step how this works? What can I expect to happen if I run this?
    – forthrin
    Commented Jan 5, 2019 at 11:32
  • @forthrin - added comments and explanation
    – user14755
    Commented Jan 6, 2019 at 21:39
  • @forthrin - fixed some bugs. Let me know if there is any part you don’t understand
    – user14755
    Commented Jan 15, 2019 at 21:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .