Package: mailscripts Version: 0.24-1 Severity: wishlist Tags: patch Attached is a patch (via git format-patch) for a script to re-inject an existing message via sendmail. The script extracts the sender and all recipients from the message and constructs the appropriate sendmail command to re-send the message. This is very useful for messages that were fcc'd but for some reason failed to make it out on an initial pass (e.g. MTA misconfiguration). A man page is also included.
Thanks for the useful package! jamie. -- System Information: Debian Release: bookworm/sid APT prefers testing APT policy: (600, 'testing'), (500, 'unstable-debug'), (200, 'unstable'), (101, 'experimental') Architecture: amd64 (x86_64) Foreign Architectures: i386 Kernel: Linux 5.16.0-5-amd64 (SMP w/8 CPU threads; PREEMPT) Kernel taint flags: TAINT_OOT_MODULE, TAINT_UNSIGNED_MODULE Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8), LANGUAGE not set Shell: /bin/sh linked to /bin/dash Init: systemd (via /run/systemd/system) LSM: AppArmor: enabled Versions of packages mailscripts depends on: ii libconfig-tiny-perl 2.28-1 ii libfile-which-perl 1.23-1 ii libipc-system-simple-perl 1.30-1 ii liblist-moreutils-perl 0.430-2 ii libmail-box-perl 3.009-1 ii perl 5.34.0-3 ii python3 3.9.8-1 Versions of packages mailscripts recommends: ii devscripts 2.22.1 ii git 1:2.35.1-1 ii libgit-wrapper-perl 0.048-1 ii notmuch 0.35-2 ii python3-argcomplete 1.12.3-0.1 ii python3-gssapi 1.6.12-2 ii python3-pgpy 0.5.4-4 Versions of packages mailscripts suggests: ii gnutls-bin 3.7.3-4+b1 ii gpg 2.2.27-3+b1 ii gpg-agent 2.2.27-3+b1 ii gpgsm 2.2.27-3+b1 pn libdbd-sqlite3-perl <none> ii libemail-date-format-perl 1.005-1.1 ii libio-socket-ssl-perl 2.074-2 ii libmailtools-perl 2.21-1 ii libmime-lite-perl 3.033-1 ii libtry-tiny-perl 0.31-1 pn libxml-feed-perl <none> ii openssl 1.1.1n-1 -- no debconf information
>From 69693dcd497a2fae7f6eb7bdeb0545120b2cb0a6 Mon Sep 17 00:00:00 2001 From: Jameson Graef Rollins <jroll...@finestructure.net> Date: Tue, 12 Apr 2022 13:03:53 -0700 Subject: [PATCH] new script to reinject message via sendmail --- sendmail-reinject | 70 +++++++++++++++++++++++++++++++++++++++++ sendmail-reinject.1.pod | 41 ++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100755 sendmail-reinject create mode 100644 sendmail-reinject.1.pod diff --git a/sendmail-reinject b/sendmail-reinject new file mode 100755 index 0000000..dfc18d5 --- /dev/null +++ b/sendmail-reinject @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +import sys +import argparse +import subprocess + +import email +from email.policy import default +from email.utils import parseaddr, getaddresses + + +def sendmail(recipients, message, sender): + """send message via sendmail""" + cmd = [ + 'sendmail', + '-f', sender, + ] + recipients + print(' '.join(cmd), file=sys.stderr) + subprocess.run( + cmd, + input=message.as_bytes(), + check=True, + ) + + +def main(): + parser = argparse.ArgumentParser( + description="Reinject an email message via sendmail.", + ) + pgroup = parser.add_mutually_exclusive_group(required=True) + pgroup.add_argument( + 'message', nargs='?', type=argparse.FileType('rb'), + help="email message path or '-' for stdin", + ) + pgroup.add_argument( + '-i', '--id', + help="message ID for notmuch extraction", + ) + + args = parser.parse_args() + + if args.id: + import notmuch2 as notmuch + db = notmuch.Database() + query = f'id:{args.id}' + assert db.count_messages(query) == 1, "Message ID does not match exactly one message??" + for msg in db.messages(query): + path = msg.path + break + f = open(path, 'rb') + else: + f = args.message + + # parse the email message + msg = email.message_from_binary_file(f, policy=default) + + sender = parseaddr(msg['from'])[1] + + # extract all recipients + tos = msg.get_all('to', []) + ccs = msg.get_all('cc', []) + resent_tos = msg.get_all('resent-to', []) + resent_ccs = msg.get_all('resent-cc', []) + recipients = [r[1] for r in getaddresses(tos + ccs + resent_tos + resent_ccs)] + + sendmail(recipients, msg, sender) + + +if __name__ == '__main__': + main() diff --git a/sendmail-reinject.1.pod b/sendmail-reinject.1.pod new file mode 100644 index 0000000..ed2ac22 --- /dev/null +++ b/sendmail-reinject.1.pod @@ -0,0 +1,41 @@ +=encoding utf8 + +=head1 NAME + +sendmail-reinject - reinject an e-mail via sendmail + +=head1 SYNOPSIS + +B<sendmail-reinject> B<message.eml> + +B<sendmail-reinject> B<-> <B<message.eml> + +B<sendmail-reinject> B<--id> B<messageID> + +=head1 DESCRIPTION + +B<sendmail-reinject> reinjects a message to your MTA via sendmail. +The message is read in (via path, stdin, or from notmuch via message +ID), the sender and recipients are extracted, and the appropriate +senmdail command is contructed to resent the message. + + +=head1 OPTIONS + +=over 4 + +=item B<--id>,B<-i> B<msgID> + +Message ID of message to reinject as know to a local notmuch database. +Assumes the python3-notmuch package is available. + +=item B<--help>, B<-h> + +Show usage instructions. + +=back + + +=head1 AUTHOR + +B<sendmail-reinject> and this manpage were written by Jameson Graef Rollins. -- 2.35.1