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:
  • Then the shell script searchbyalias.sh will create a macro the limit to the corresponding email items.

Enjoy!

苏命
苏命
副研究员

我的研究方向:水源地水质安全保障,主要针对水源地由于藻类爆发引起的水体嗅味问题开展研究。

comments powered by Disqus

相关