add mail-script

This commit is contained in:
Christoph J. Scherr 2024-04-22 12:21:33 +02:00
parent e18752f758
commit 68a2136e1f
1 changed files with 30 additions and 0 deletions

30
mail-script Executable file
View File

@ -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())