Mutt - Extract content from a batch of emails

2020-05-13 @Technology
By Vitaly Parnas

On a weekly basis I’m emailed the meeting minutes of my Toastmasters club. These emails I opt to quickly delete, being very large in size, and containing a series of uncompressed images.

Of interest in each such email are just a couple of links to meeting recordings. The respective two lines are

Speaker view: <URL>
Gallery view: <URL>

These lines I wish to extract and save to my notes before the deletion.

In addition, I tend to let these emails accumulate.

Rather than manually open each such email, find and copy the links, then delete, there is a way to automate the entire procedure:

  1. Tag all the respective emails: T + <tag pattern>.

    Replace the pattern with something identifying the emails of interest (ex ~f '<from address>', ~s '<subject substring>'). In my case, ~s 'Meeting Minutes' does the trick.

  2. Press ; to apply the following command to all tagged messages.

  3. Pipe all tagged emails to an egrep command that filters out just the lines of interest. Then pipe the results of that into a VIM buffer for further processing of your choice.

    Press | to initiate the pipe command, then enter the following:

    egrep '^(From:|Subject:|Speaker|Gallery)' | vim -
    

    The above extracts the from and subject header lines as well as the two lines containing the URLs. (The subject identifies the meeting date corresponding to the URLs, which well categorizes the links).

    The resulting (four) lines per every tagged email then appear in a VIM buffer. Exit the buffer afterwards to return to mutt.

    Alternatively, we could append the egrep output to a designated file by replacing

    | vim -

    with something like

    >> ~/notes/meeting_recordings.txt.

  4. Following the above procedure, the emails still tagged, we can delete them all via ;D. (D is my mapping to delete an email. I think a lowercase d is the default.)

To top it all, we can write a macro to perform the entire procedure via one key stroke. Place the following Alt-B macro (or your keystroke of preference) in your muttrc. Here I’ve omitted the delete, letting you do that manually.

macro index <Esc>B "<tag-pattern>~s 'Meeting Minutes'<enter>\
<tag-prefix><pipe-message>\
egrep '^(From:|Subject:|Speaker|Gallery)' | vim -<enter>"

Questions, comments? Connect.