From 68a2136e1f2f5315a84e29615ce171031856fde6 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 22 Apr 2024 12:21:33 +0200 Subject: [PATCH] add mail-script --- mail-script | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 mail-script diff --git a/mail-script b/mail-script new file mode 100755 index 0000000..28f4c95 --- /dev/null +++ b/mail-script @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +from email.message import EmailMessage +import sys + +if sys.argv[1] == "--help" or sys.argv[1] == "-h" or sys.argv[1] == "": + print(""" +This is just a hacked together python script from stackoverflow that can be used to add a proper +MIME header for file attachments to a mail. Pipe the outpit to sendmail -oi -t to send. + +usage: +mail-script "from@email.com" "to@email.com" "subject" /file/to/append < /body/of/mail | sendmail -oi -t +""") + exit(0) + +msg = EmailMessage() +msg['From'] = sys.argv[1] +msg['To'] = sys.argv[2] +msg['Subject'] = sys.argv[3] +# Don't mess with the preamble, the documentation is wrong to suggest you do + +msg.set_content(''.join([line for line in sys.stdin])) + +with open(sys.argv[4], 'r') as attachment: + #msg.add_attachment(attachment.read(), maintype='text', subtype='plain') + asStr = attachment.read() + asBytes = asStr.encode('utf-8') + msg.add_attachment(asBytes, maintype='application', subtype='octet-stream', filename="appendix.txt") + + print(msg.as_string())