+
AMDG
Goodman Coat of Arms

Goodman's Oak

Miscellaneous Thoughts, Projects, and Ruminations on Life, the Universe, and Everything

Using Mutt and Abook with Email Groups

or, Taking Advantage of Mutt's Query Command

Donald P. Goodman III 26 Dec 1200 (30 Dec 2016)

The Purpose of This Page

I've mentioned using mutt and abook together before, but there is one level on which they simply don't work: email lists. That is, mutt works just fine with email lists using its alias features, and mutt and abook work together very well when you're looking for individual email addresses to pick out of a list. But if you want to keep all your email information in one place, you're out of luck; you have to keep your lists in mutt and your other information in abook.

Until now!

I advised you to put the following lines in your .muttc:

set query_command= "abook --mutt-query '%s'"
macro index,pager A "abook --add-email-quiet" "add the sender address to abook"

This will let you select emails from your abook addressbook, rather than having to remember your addresses, or their aliases. This is certainly a much easier way of doing things, and helps you string two really useful applications together, each doing their job really well.

However, if you want to be able to send mail to an email list or group, you'll still have to pick each address out individually, or keep the list in your .muttrc manually. And even then, you'll have to keep the email addresses for the members of that list manually, apart from your addressbook. That is, to say the least, suboptimal.

So I wrote a couple of scripts which will handle all this for you. Let's take a look at what we have to do.

The Scripts

The scripts themselves are surprisingly simple. Just put them someplace that executable for you; I personally have a ~/scripts directory where I put personal scripts, so I put them both there.

The first is group_get.sh:

#!/bin/sh
# +AMDG
#
# This file is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any
# later version.  For a copy of the GNU General Public
# License, see <http://www.gnu.org/licenses/>.

if [ $# -ne 1 ]; then
	echo "error:  must give one argument"
	exit
fi

if [ `echo $1 | grep "^g/"` ]; then
	GROUPLIST=`echo $1 | cut -b 3-`
	for i in `echo $GROUPLIST | cut -d "," --output-delimiter=" " -f 1-`; do
		awk -f ~/scripts/group_get.awk -v groupname=$i ~/.abook/addressbook
	done
else
	abook --mutt-query "$1"
fi

In this case, the awk file itself is not executable, so the file merely being in your PATH will not suffice; you need to give the shell the complete path to the script. So in line 20 of this script (the one beginning awk -f...), replace ~/scripts/ with the actual path to the directory containing the awk script.

The next is what does the magic with the groups in your addressbook, group_get.awk:

# +AMDG
#
# This file is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any
# later version.  For a copy of the GNU General Public
# License, see <http://www.gnu.org/licenses/>.

BEGIN {
	FS = "="
	currname = ""
}

/^email=/ {
	split($2,emails,",")
}

/^name=/ {
	currname = $2
}

/^email_lists=/ {
	regex = ".*"groupname".*"
	if ($2 ~ regex) {
		for (addr in emails) {
			print emails[addr]"\t"currname
		}
	}
}

These two surprisingly simple scripts take care of the makework. You still have a little bit of tweaking to do in your abookrc and .muttrc, though.

Modification to the Config Files

Modification to .muttrc

The first thing you need to do is make a slight modification to your .muttrc. You still want abook to be your query command, but you want it to be accessed through the above scripts. So replace your query_command line with the following:

set query_command= "group_get.sh '%s'"

That's all the fixing that your .muttrc will need.

Modification of Abook's Files

Next, though, you'll need to do some work on your abookrc file. My abookrc is pretty heavily customized, so I'll cut out my special stuff and show you what you need to add to yours for these scripts to work. You'll need to define a new field, email_lists:

field email_lists = "Email Lists", string

If you want, you can have this show up in one of your tabs; that way you can enter them in the interactive program. For example,

view OTHER = nick,birthday,url,email_lists

This will produce a screen something like the following:

(I've cut out an email address in this image; you won't see the ugly white block on your screen.) Now, enter the information in the usual way. When you come to the "Email Lists" line, type the names of the email lists that this person belongs to, separated by commas (with no spaces). Your entry might look something like this:

5 - Email Lists		: friends,family,baseball

Note that you must enter the email lists after you have entered at least the name and email address. These scripts presume that these two items are defined prior to the email_lists variable. If you forget about this, you can easily fix it by modifying the entry in your addressbook file, but it seems unlikely you'd ever be defining someone's email groups prior to their name and email address.

Now you're ready to do some actual querying!

Querying Addresses and Groups

We get very used to thinking that abook is primarily good for picking out individual recipients for an email by hitting CTRL-t when mutt prompts us with "To:". And abook is excellent with that. But mutt's query command is much more than that.

For example, go to your message index in any mailbox and hit "Q" (a capital "q"). You'll be prompted to enter a search string. Given the setup above, mutt will use that string to search your abook database and return to you any matching addresses. You can tag whichever addresses from that list you want to use with "t" (a lowercase "T"), then hit ";m" (semicolon lowercase-m) to mail to all the tagged messages.

Now, you've got additional abilities: if you want to email to a list, when you query with "Q", type first "g/", then the names of the groups you want to mail to, separated by commas (no spaces). E.g.:

Q
Query:  g/friends,family

This will produce a list of email addresses which belong to the two groups, "friends" and "family". Select the ones you want with "t", then hit ";m" to write a message to them. Done and done.

Conclusion

Mutt and abook are two powerful tools that can take care of a lot of work for us, particularly when we let them work together. These scripts provide a way to use them together even more effectively.