#!/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())