forked from waveform80/picamera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maildebs.py
executable file
·82 lines (71 loc) · 2.93 KB
/
maildebs.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
import io
import os
import re
import sys
import subprocess
import smtplib
import configparser
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from setup import __project__, __version__
HERE = os.path.dirname(__file__)
def create_message(sender, recipients, subject, body, attachments):
root_container = MIMEMultipart(_subtype='related')
root_container['From'] = sender
root_container['To'] = recipients
root_container['Subject'] = subject
root_container.preamble = 'This is a multi-part message in MIME format.'
root_container.attach(MIMEText(body, _subtype='plain'))
for attachment in attachments:
with io.open(attachment, 'rb') as f:
attachment_container = MIMEApplication(f.read())
filename = os.path.split(attachment)[1]
attachment_container.add_header('Content-Id', '<%s>' % filename)
attachment_container.add_header('Content-Disposition', 'attachment', filename=filename)
root_container.attach(attachment_container)
return root_container
def send_email(message, host='localhost', port=25):
server = smtplib.SMTP(host, port)
try:
server.ehlo()
server.sendmail(
message['From'],
message['To'],
message.as_string())
finally:
server.quit()
def main():
config = configparser.ConfigParser()
config.read(os.path.expanduser('~/.maildebs.conf'))
project = __project__
version = __version__
recipient = config['message']['recipient']
sender = config['message'].get('sender', '%s <%s>' % (
subprocess.check_output(['git', 'config', '--global', 'user.name']).decode('utf-8').strip(),
subprocess.check_output(['git', 'config', '--global', 'user.email']).decode('utf-8').strip(),
))
sender_match = re.match(r'(?P<name>[^<]+) <(?P<email>[^>]+)>', sender)
recipient_match = re.match(r'(?P<name>[^<]+) <(?P<email>[^>]+)>', recipient)
subst = {
'project': project,
'version': version,
'recipient_name': recipient_match.group('name').split(),
'recipient_email': recipient_match.group('email'),
'recipient_forename': recipient_match.group('name').split()[0],
'recipient_surname': recipient_match.group('name').split()[1],
'sender_name': sender_match.group('name').split(),
'sender_email': sender_match.group('email'),
'sender_forename': sender_match.group('name').split()[0],
'sender_surname': sender_match.group('name').split()[1],
}
subject = config['message']['subject'].format(**subst)
body = config['message']['body'].format(**subst)
attachments = sys.argv[1:]
send_email(
create_message(sender, recipient, subject, body, attachments),
config['smtp'].get('host', 'localhost'),
config['smtp'].get('port', 25))
if __name__ == '__main__':
main()