copyright 1998-2017 by Mark Verboom
ipv6 ready RSS Feed Schakel naar Nederlands

Go back to What's new

Profiles for mutt

To blog index

Wednesday, 18 May, 2011

Profiles for mutt

I've been using mutt for years and recently started using muttprofile which lets you choose certain configuration settings when you have multiple email accounts.

For me a couple of things are unhandy about muttprofile. I don't like the fact it gives out error codes and I would like an option to select a profile from a list. So I decided to build something similar in bash.

The idea is to make a folder in your homedirectory which contains small sniplets of mutt configurations. Every file contains settings specific for sending mail for that account. The script can easily be changed from default locations, but in this example I will keep the defaults. Create a folder .mutt

mkdir ~/.mutt

You can now create a profile. I'll do an example for a private email account. Edit the file (vi ~/.mutt/profile.private) and put in the following content:

# NAME: private
# DESC: private email profile

# Signature file
set signature='~/.signature'

# Sing everything with this account.
set pgp_autosign=yes

# Customized headers
unmy_hdr * # remove all extra headers first.

my_hdr From: My private email adress <private@hotmail.com>
my_hdr Reply-To: My private email <private@hotmail.com>
set sendmail="/usr/bin/msmtp -f private@hotmail.com"

# Include the profile name in the status line
set status_format="priv(%r): %f [Msgs:%?M?%M/?%m%?n? New:%n?%?o? Old:%o?%?d? Del:%d?%?F? Flag:%F?%?t? Tag:%t?%?p? Post:%p?%?b? Inc:%b? %?l? %l?]---(%s/%S)-default-%>-(%P)---"

The configuration sets the headers, configures the smtp delivery I use (msmtp) and updates the statusbar. Of course you can add all kinds of other configuration items. The NAME and DESC items in the header are the same as for muttprofile. The NAME is ignored, but the DESC can be used to give a better description of the profile.

When this profile is made active, a symlink is created to the profile. As the symlink always has the same name you can refer to it in the main mutt configuration. For example, if you use the c key to compose a new message in mutt, you can add the following to your main mutt configuration:

macro index c "!mpn:source ~/.mutt/profile.activenm"

Here mp is the name of the shellscript shown below (somewhere in your path). It is executed first, letting you select the profile you want to use. It will link the chosen profile to the ~/.mutt/profile.active. The settings in that file will be sourced and the m command is run to compose a new mail.

The mp script looks like this:

#!/bin/bash
#
# Select a mail profile for mutt. Simplified shellscript version
#
# 20110518 MV: Initial version

BASEDIR=~/.mutt

MUTTBASE=profile
MUTTPROFILE=$BASEDIR/profile.active
CHOICE=/tmp/mp.$$

declare menulist

for option in $( find $BASEDIR/$MUTTBASE.* -type f | sed "s/.*\.//" )
do
menulist[${#menulist[@]}]="$option"
desc="$( grep "DESC:" $BASEDIR/$MUTTBASE.$option )"
menulist[${#menulist[@]}]="${desc/*DESC: /}"
done
default=$( find $BASEDIR/$MUTTBASE.* -type l -printf "%l" | sed "s/.*\.//" )

IFS=$'\n'
whiptail --default-item $default --nocancel --menu "Profile selection" 20 60 10 $( printf "${menulist[*]}") 2> $CHOICE
ret=$( cat $CHOICE )
rm -f $CHOICE
if test -f $BASEDIR/$MUTTBASE.$ret
then
ln -sf $BASEDIR/$MUTTBASE.$ret $MUTTPROFILE
fi

I've used whiptail instead of dialog as it has the option to select a default menu item. That way pressing enter will keep you going on the same profile.

Of course you can add loads of profiles and make the menu look different, its quite a simpel shellscript.