-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
hotdog-generateEmailFrom:to:subject:body:attachment:.py
executable file
·58 lines (46 loc) · 1.52 KB
/
hotdog-generateEmailFrom:to:subject:body:attachment:.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import os
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
def get_attachment(path):
str = path.lower()
name = os.path.basename(path)
data = open(path, 'rb').read()
part = None
if str.endswith('.jpg') or str.endswith('.jpeg'):
part = MIMEImage(data, 'jpeg')
elif str.endswith('.png'):
part = MIMEImage(data, 'png')
elif str.endswith('.gif'):
part = MIMEImage(data, 'gif')
elif str.endswith('.txt'):
part = MIMEText(data, 'plain')
# elif str.endswith('.html'):
# part = MIMEText(data, 'html')
elif str.endswith('.pdf'):
part = MIMEApplication(data, 'pdf')
else:
part = MIMEApplication(data, 'octet-stream')
part.add_header('Content-Disposition', 'attachment', filename=name)
return part
if __name__ == "__main__":
if len(sys.argv) < 5:
sys.stderr.write("specify from, to, subject, body, attachment\n")
sys.exit(1)
fromaddress = sys.argv[1]
toaddress = sys.argv[2]
subject = sys.argv[3]
body = sys.argv[4]
message = MIMEMultipart()
message['From'] = fromaddress
message['To'] = toaddress
message['Subject'] = subject
text = MIMEText(body)
message.attach(text)
for attachmentfile in sys.argv[5:]:
attachment = get_attachment(attachmentfile)
message.attach(attachment)
print(message.as_string())