2

In mutt, can macros can obtain some of the properties from the current message, in order to use them as variables? Here are some examples of potential macros, with the properties I'd wish to obtain in italics.

  • Remove gpg encryption for the current message. i.e. decrypt-save to the current mailbox containining the message.
  • Save current message to a file with the subject and date as its name. i.e. | cat > /tmp/filename, where filename is date_subject.
  • File message into mailbox archive-year, where year depends on the year of the message. (There is a way to do this based on the current date, but not the date of the message.)
1

I don't know how to obtain properties from the message, but at least for your first example, there is another solution.

You can use ^ as a shortcut for the current mailbox, so if you want to decrypt-save to the current mailbox it is possible with

macro index <F7> <decrypt-save>^<Enter>y<Enter>
1

Similar to the other answer, I haven't yet worked out how to obtain properties in a general sense, but here is a hacky solution to the second example.

Write a script that accepts an email via standard input, extracts the date and subject, and saves it to /path/to/save/email/date_subject.

#!/bin/env bash

message=$(cat)

mail_date=$(<<<"$message" grep -oPm 1 '^Date: ?\K.*')
formatted_date=$(date -d"$mail_date" +%y%m%d)
# Get the first line of the subject, and change / to ∕ so it's not a subdirectory
subject=$(<<<"$message" grep -oPm 1 '^Subject: ?\K.*' | sed 's,/,∕,g')    
# decode base64 (UTF-8)
if [[ "$subject" =~ ^=\?[Uu][Tt][Ff]-8\?B\?.*?= ]]; then
  nofront="$(echo "${subject#=\????-8\?B\?}")"
  todecode="$(echo "${nofront%\?=}")"
  subject="$(<<<"$todecode" base64 --decode)"
fi

if [[ $formatted_date == '' ]]; then
  echo Error: no date parsed
  exit 1
elif [[ $subject == '' ]]; then
  echo Warning: no subject found
fi

echo "${message}" > "$1/${formatted_date}_${subject}.eml" && echo Email saved to "$1/${formatted_date}_${subject}.eml"

In muttrc, bind S to this function:

macro index,pager S "| /path/to/script /path/to/save/email<enter>"

N.B. this will only use the first line of multi-line subjects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.