-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.py
93 lines (69 loc) · 2.47 KB
/
mail.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
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
import os
import sys
import syslog
import base64
import quopri
import re
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Email, To, Content
def decode_mail_address(encoded_mail_address):
try:
encoded_word_regex = r'=\?{1}(.+)\?{1}([B|Q])\?{1}(.+)\?{1}= (.*){1}'
charset, encoding, encoded_text, mail = re.match(
encoded_word_regex, encoded_mail_address).groups()
if encoding is 'B':
byte_string = base64.b64decode(encoded_text)
elif encoding is 'Q':
byte_string = quopri.decodestring(encoded_text)
return byte_string.decode(charset) + ' ' + mail
except:
return encoded_mail_address
def decode_subject(encoded_subject):
try:
encoded_word_regex = r'=\?{1}(.+)\?{1}([B|Q])\?{1}(.+)\?{1}='
charset, encoding, encoded_text = re.match(
encoded_word_regex, encoded_subject).groups()
if encoding is 'B':
byte_string = base64.b64decode(encoded_text)
elif encoding is 'Q':
byte_string = quopri.decodestring(encoded_text)
return byte_string.decode(charset)
except:
return encoded_subject
syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL)
syslog.syslog('FORUM Sending mail %s' % sys.argv)
data = sys.stdin.readlines()
if len(data) == 0:
syslog.syslog(syslog.LOG_ERR, 'Data is empty')
sys.exit(0)
is_body = False
params = {}
for line in data:
if len(line.strip()) == 0:
is_body = True
continue
if is_body:
params['body'] = params.setdefault('body', '') + line
else:
try:
key, value = line.split(':', 1)
params[key] = value.strip()
except:
syslog.syslog(syslog.LOG_ERR, 'Invalid data: %s' % line)
pass
try:
params['From'] = decode_mail_address(params['From'])
params['To'] = decode_mail_address(params['To'])
params['Subject'] = decode_subject(params['Subject'])
syslog.syslog('Params: %s' % params)
mail = Mail(from_email=params['From'],
to_emails=params['To'],
subject=params['Subject'],
plain_text_content=params['body'])
sg = SendGridAPIClient(api_key=SENDGRID_API_KEY)
response = sg.send(mail)
syslog.syslog('Response: Code %s' % response.status_code)
except Exception as e:
syslog.syslog(syslog.LOG_ERR, 'ERROR: %s' % e)
syslog.syslog('FORUM Sending mail finished')