Skip to content

Instantly share code, notes, and snippets.

@brauner
Last active April 25, 2023 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brauner/f15663d562fabeb83ca4af8ac1caed7b to your computer and use it in GitHub Desktop.
Save brauner/f15663d562fabeb83ca4af8ac1caed7b to your computer and use it in GitHub Desktop.
> cat ~/.config/neomutt/generate-msgid
#!/bin/bash
# This generates lore-friendly message-id headers that are safe, unique, and
# provide better UX for someone using lore to retrieve messages.
#
# Instructions for using with mutt/neomutt:
#
# Save this as ~/bin/my-gen-msgid, then add ~/.mutt-fix-msgid with the following,
# fixing your path to the file:
#
# my_hdr Message-ID: <`/home/user/bin/my-gen-msgid`>
#
# then edit ~/.muttrc to add:
#
# send-hook . "source ~/.mutt-fix-msgid"
#
# I like my msgid to start with the date
msgid="$(date +%Y%m%d)-"
if [[ -x /bin/diceware ]]; then
# I like memorable nonsense, so I can visually tell one message from another,
# by looking at the lore URL, so use diceware for that
msgid="${msgid}$(diceware --no-caps -d- -n2)-$(openssl rand -hex 6)"
else
# Just use openssl with some extra randomness
msgid="${msgid}$(openssl rand -hex 12)"
fi
# Don't leak my hostname, just use something that makes sense to me
echo -n "${msgid}@brauner"
> cat .git/hooks/sendemail-validate
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: GPL-2.0-or-later
import email
import email.generator
import os
import subprocess
import sys
__author__ = 'Christian Brauner <brauner@kernel.org>'
new_msgid = subprocess.check_output(["/home/brauner/.config/neomutt/generate-msgid"], text=True)
f = open(sys.argv[1], "r+")
msg = email.message_from_file(f)
del msg["Message-Id"]
msg.__setitem__("Message-Id", new_msgid)
f.seek(0)
f.truncate()
gen = email.generator.Generator(f)
flattened = gen.flatten(msg, unixfrom=True)
f.close()
subprocess.run(['patatt', 'sign', '--hook', sys.argv[1]], check=True)
@marckleinebudde
Copy link

I think you want to put the message id into "<>", e.g

msg.__setitem__("Message-Id", f"<{new_msgid}>")

@brauner
Copy link
Author

brauner commented Apr 25, 2023

@marckleinebudde
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment