forked from brandon-rhodes/fopnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_mime_email.py
executable file
·60 lines (50 loc) · 2.38 KB
/
build_mime_email.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
58
59
60
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter12/build_mime_email.py
import argparse, email.message, email.policy, email.utils, mimetypes, sys
plain = """Hello,
This is a MIME message from Chapter 12.
- Anonymous"""
html = """<p>Hello,</p>
<p>This is a <b>test message</b> from Chapter 12.</p>
<p>- <i>Anonymous</i></p>"""
img = """<p>This is the smallest possible blue GIF:</p>
<img src="cid:{}" height="80" width="80">"""
# Tiny example GIF from http://www.perlmonks.org/?node_id=7974
blue_dot = (b'GIF89a1010\x900000\xff000,000010100\x02\x02\x0410;'
.replace(b'0', b'\x00').replace(b'1', b'\x01'))
def main(args):
message = email.message.EmailMessage(email.policy.SMTP)
message['To'] = 'Test Recipient <[email protected]>'
message['From'] = 'Test Sender <[email protected]>'
message['Subject'] = 'Foundations of Python Network Programming'
message['Date'] = email.utils.formatdate(localtime=True)
message['Message-ID'] = email.utils.make_msgid()
if not args.i:
message.set_content(html, subtype='html')
message.add_alternative(plain)
else:
cid = email.utils.make_msgid() # RFC 2392: must be globally unique!
message.set_content(html + img.format(cid.strip('<>')), subtype='html')
message.add_related(blue_dot, 'image', 'gif', cid=cid,
filename='blue-dot.gif')
message.add_alternative(plain)
for filename in args.filename:
mime_type, encoding = mimetypes.guess_type(filename)
if encoding or (mime_type is None):
mime_type = 'application/octet-stream'
main, sub = mime_type.split('/')
if main == 'text':
with open(filename, encoding='utf-8') as f:
text = f.read()
message.add_attachment(text, sub, filename=filename)
else:
with open(filename, 'rb') as f:
data = f.read()
message.add_attachment(data, main, sub, filename=filename)
sys.stdout.buffer.write(message.as_bytes())
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Build, print a MIME email')
parser.add_argument('-i', action='store_true', help='Include GIF image')
parser.add_argument('filename', nargs='*', help='Attachment filename')
main(parser.parse_args())