Search Email by Alias in Mutt

Aim: search email items using alias

It is very neccessary to search email by the name, since we usually can not rember the email address, but the names.

However, there is no built-in solutions to do this.

Solution

Since mutt extact the name from a file called alias, and mutt has very handy function of limit.

I build a workflow by reading the input (name), and find the email address by sed etc., then create a dynamic macro to limit the email items using the found email address. As shown below:

  • Add the following line in muttrc:

    macro index,pager f "<shell-escape>~/.config/mutt/bin/searchbyalias.sh\n:source /tmp/from\nQ\n" "Search email by alias"
    
  • Shell script to fetch the email address, and make a dynamic macro

    $cat ~/.config/mutt/bin/searchbyalias.sh
    
    #!/bin/bash
    read alias
    
    if [[ $alias == "t "* ]]; then
    alias=${alias#*t }
    email=`cat ~/.config/mutt/aliases | grep $alias | \
        sed -e "s/^.*<\(.*\)>$/~t '\1' | /g" | \
        xargs | sed -e "s/^/l/g" | sed -e "s/|$//g"`
    elif [[ $alias == "c "* ]]; then
    alias=${alias#*c }
    email=`cat ~/.config/mutt/aliases | grep $alias | \
        sed -e "s/^.*<\(.*\)>$/~c '\1' | /g" | \
        xargs | sed -e "s/^/l/g" | sed -e "s/|$//g"`
    elif [[ $alias == "f "* ]]; then
    alias=${alias#*f }
    email=`cat ~/.config/mutt/aliases | grep $alias | \
        sed -e "s/^.*<\(.*\)>$/~f '\1' | /g" | \
        xargs | sed -e "s/^/l/g" | sed -e "s/|$//g"`
    elif [[ $alias == "fc "* ]]; then
    alias=${alias#*fc }
    email=`cat ~/.config/mutt/aliases | grep $alias | \
        sed -e "s/^.*<\(.*\)>$/~f '\1' | ~c '\1' | /g" | \
        xargs | sed -e "s/^/l/g" | sed -e "s/|$//g"`
    elif [[ $alias == "tc "* ]]; then
    alias=${alias#*tc }
    email=`cat ~/.config/mutt/aliases | grep $alias | \
        sed -e "s/^.*<\(.*\)>$/~t '\1' | ~c '\1' | /g" | \
        xargs | sed -e "s/^/l/g" | sed -e "s/|$//g"`
    else
    email=`cat ~/.config/mutt/aliases | grep $alias | \
        sed -e "s/^.*<\(.*\)>$/~f '\1' | ~c '\1' | ~t '\1' | /g" | \
        xargs | sed -e "s/^/l/g" | sed -e "s/|$//g"`
    fi
    
    if [[ "X"$email == "X" ]]; then
    echo "No alias found"
    else
    if [[ -e /tmp/from ]]; then
        rm /tmp/from
    fi
    echo 'macro index Q "'$email'"' > /tmp/from
    echo >> /tmp/from
    fi
    

How to use

  • In mutt, type f to trigger the macro, wait for typing;
  • Type the alias. In my case, alias is shown as follows. If one has more than one email, I use firstlast[1-9] for different alias name.

    alias firstlast First LAST <xxx@xxx.xxx>
    alias firstlast1 First LAST <yyy@yyy.xxx>
    
  • As shown in the searchbyalias.sh, 6 modes are supported for different cases:

    • Type firstlast, will limit to all emails (xxx@xxx.xxx, yyy@yyy.xxx) in the field of from, to and cc.
    • Type t firstlast, will limit to all emails (xxx@xxx.xxx, yyy@yyy.xxx) in the field of to.
    • Type f firstlast, will limit to all emails (xxx@xxx.xxx, yyy@yyy.xxx) in the field of from.
    • Type c firstlast, will limit to all emails (xxx@xxx.xxx, yyy@yyy.xxx) in the field of cc.
    • Type fc firstlast, will limit to all emails (xxx@xxx.xxx, yyy@yyy.xxx) in the field of from and cc.
    • Type tc firstlast, will limit to all emails (xxx@xxx.xxx, yyy@yyy.xxx) in the field of to and cc.
  • Then the shell script searchbyalias.sh will create a macro the limit to the corresponding email items.

Enjoy!

Avatar
Ming Su
Associate Professor of Environmental Engneering

My research interest is water quality problems in drinking water bodies, with a focus on harmful algal blooms and associated taste & odor problems.

Related

Next
Previous
comments powered by Disqus