nblock's ~

Using mutt alias files for email address completion

I use mutt as mail client at home and at work and I'm quite happy with it. One thing that bugs me though is the built-in alias support. Its email address completion is rather limited. Luckily, mutt supports QueryCommand that can be used to connect an arbitrary data source for external email address completion. I use a tiny wrapper for grep to search multiple alias files at once:

#!/bin/sh
# Search all alias files and sort by name.

echo "Search results for »$1«"
grep --ignore-case --no-filename "$1" $HOME/.mutt/aliases/*.aliases | sort --key=2,2

The query_command option in mutt must be set accordingly to use the script:

set query_command="~/.mutt/scripts/alias-query '%s'"

Mutt and the above script expect an alias file to be in the mutt query format. This format is rather simple and looks as follows (described here):

<email address> <tab> <long name> <tab> <other info> <newline>

Most of my alias files are autogenerated using various scripts. One of those scripts connects to an LDAP server, finds the name and the email address of all company employees and converts them to the mutt query format described above (tested on Exchange):

#!/bin/sh
# Collect data from an LDAP server and convert to mutt query format.
# NOTE: This script interactively asks for the LDAP password.

set -e
set -u

# Configuration
LDAP_HOST="192.168.1.1"
LDAP_USER="DOMAIN\\USERNAME"
LDAP_BASE="OU=SBSUsers,OU=Users,OU=MyBusiness,DC=DOMAIN,DC=TLD"
MUTT_INFO="TheOtherInfoField"

ldapsearch -LLL -h "$LDAP_HOST" -D "$LDAP_USER" -W \
  -x -b "$LDAP_BASE" "(mail=*)" cn mail | \
  sed -n "/^cn:/ {N; s/^cn: \(.*\)\nmail: \(.*\)$/\2\t\1\t$MUTT_INFO/p}"

Have fun.


permalink | tweet this

tagged ldap and mutt