-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcourriel.compose
executable file
·92 lines (76 loc) · 3.3 KB
/
courriel.compose
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/env python3
import os
import sys
# For guessing MIME type based on file name extension
import mimetypes
from argparse import ArgumentParser
from email import encoders
from email.utils import parseaddr
from email.utils import formataddr
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
parser = ArgumentParser(description='Creates a MIME message. Body is read from stdin')
parser.add_argument('-f', '--from', required=True, dest='sender',
help='The value of the From: header (required)')
parser.add_argument('-t', '--to', required=True, nargs='+', dest='recipients',
help='A To: header value (at least one required)')
parser.add_argument('--cc', nargs='*', dest='cc',
help='Carbon copy header (optional)')
parser.add_argument('--bcc', nargs='*', dest='bcc',
help='Blind carbon copy header (optional)')
parser.add_argument('-s', '--subject', nargs='?', default='', dest='subject',
help='Subject header (optional)')
parser.add_argument('-a', '--attachment', nargs='*', dest='attachments',
help='Files to send as attachment (optional)')
parser.add_argument('--html', action='store_true',
help='Sets the mime-type of body part to html')
args = parser.parse_args()
# Create the enclosing (outer) message
ADDSEP = ', '
addfmt = lambda x : formataddr( parseaddr( x ) )
outer = MIMEMultipart()
outer['From'] = addfmt( args.sender )
outer['To'] = ADDSEP.join(list(map( addfmt , args.recipients) ) )
outer['Cc'] = ADDSEP.join(list(map( addfmt , args.cc) ) )
outer['Bcc'] = ADDSEP.join(list(map( addfmt , args.bcc) ) )
outer['Subject'] = args.subject
body = sys.stdin.read()
if body :
outer.attach( MIMEText( body , 'html' if args.html else 'plain' ) )
for path in args.attachments:
if not os.path.isfile(path) : continue
filename=os.path.basename(path)
# Guess the content type based on the file's extension. Encoding
# will be ignored, although we should check for simple things like
# gzip'd or compressed files.
ctype, encoding = mimetypes.guess_type(path)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed), so
# use a generic bag-of-bits type.
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
if maintype == 'text':
with open(path) as fp:
# Note: we should handle calculating the charset
msg = MIMEText(fp.read(), _subtype=subtype)
elif maintype == 'image':
with open(path, 'rb') as fp:
msg = MIMEImage(fp.read(), _subtype=subtype)
elif maintype == 'audio':
with open(path, 'rb') as fp:
msg = MIMEAudio(fp.read(), _subtype=subtype)
else:
with open(path, 'rb') as fp:
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
# Encode the payload using Base64
encoders.encode_base64(msg)
# Set the filename parameter
msg.add_header('Content-Disposition', 'attachment', filename=filename)
outer.attach(msg)
# Now send or store the message
composed = outer.as_string()
print(composed)