-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibtmail.py
240 lines (198 loc) · 7.95 KB
/
libtmail.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import email
import mimetypes
import os
import smtplib
import jinja2
import nominal
COMMASPACE = ", "
class ConnectionStatus(nominal.Nominal):
DISCONNECTED = 0
CONNECTED = 1
STATUSES = [DISCONNECTED, CONNECTED]
def __init__(self, value):
if not value in self.STATUSES:
raise ValueError("Invalid connection status %s" % value)
nominal.Nominal.__init__(self, value)
#-- class ConnectionStatus --#
class DebugStatus(nominal.Nominal):
OFF = 0
ON = 1
STATUSES = [OFF, ON]
def __init__(self, value):
if not value in self.STATUSES:
raise ValueError("Invalid debug status %s" % value)
nominal.Nominal.__init__(self, value)
#-- class DebugStatus --#
class EmailAccount(object):
"""Represents an email account on a remote SMTP server. Can be put in
debug mode to show emails being sent.
"""
def __init__(self, host, username, password):
self._host = host
self._username = username
self._password = password
self._connection = None
self._connection_status = ConnectionStatus(ConnectionStatus.DISCONNECTED)
self._debug_status = DebugStatus(DebugStatus.OFF)
def next_email(self, email):
if not isinstance(email, Tmail):
raise TypeError("%s is not a Tmail" % email)
from_addrs = email.from_addrs
to_addrs = email.to_addrs + email.cc_addrs + email.bcc_addrs
if self._debug_status.value == DebugStatus.ON:
print "To send to: %s" % to_addrs
print email
elif self._connection_status.value == ConnectionStatus.CONNECTED:
print "Sending to " + to_addrs
self._connection.sendmail(from_addrs, to_addrs, str(email))
next_email = property(fset=next_email)
@property
def connection_status(self):
return self._connection_status
@connection_status.setter
def connection_status(self, status):
if not isinstance(status, ConnectionStatus):
raise TypeError("%s is not a ConnectionStatus" % status)
if status != self._connection_status:
if status.value == ConnectionStatus.CONNECTED:
self._connect()
elif status.value == ConnectionStatus.DISCONNECTED:
self._disconnect()
self._connection_status = status
@property
def debug_status(self):
return self._debug_status
@debug_status.setter
def debug_status(self, status):
if not isinstance(status, DebugStatus):
raise TypeError("%s is not a DebugStatus" % status)
if status != self._debug_status:
self._debug_status = status
def _connect(self):
self._connection = smtplib.SMTP(self._host)
self._connection.starttls()
self._connection.ehlo()
self._connection.login(self._username, self._password)
def _disconnect(self):
self._connection.quit()
#-- class EmailAccount --#
class Templatizer(object):
"""Represents a templating engine. Currently implemented with Jinja2.
Caches templates loaded from the filesystem.
"""
def __init__(self):
self._options = None
self._templates = {}
self._environment = jinja2.Environment(trim_blocks=True,
autoescape=False)
@property
def result(self):
return self._current_template.render(self._options)
def options(self, options):
self._options = options
options = property(fset=options)
def template(self, template):
if os.path.isfile(template):
if not template in self._templates:
self._load_template(template)
self._current_template = self._templates[template]
else:
self._current_template = self._environment.from_string(template)
template = property(fset=template)
def _load_template(self, template_path):
template_dir, template_name = os.path.split(os.path.abspath(template_path))
self._environment.loader = jinja2.FileSystemLoader(template_dir)
self._templates[template_path] = self._environment.get_template(template_name)
#-- class Templatizer --#
class Tmail(object):
"""Represents a templatized email. Supports templates in both the
subject and the body. Defaults to a multipart/mixed MIME type so that
attachments can be added.
"""
def __init__(self, templatizer=None, options={}):
self._message = email.MIMEMultipart.MIMEMultipart()
self._message["Date"] = email.Utils.formatdate(localtime=True)
self._templatizer = templatizer
if templatizer is None:
self._templatizer = Templatizer()
self._templatizer.options = options
@property
def templatizer(self):
return self._templatizer
@property
def from_addrs(self):
return self._message["From"].split(COMMASPACE)
@from_addrs.setter
def from_addrs(self, from_addrs):
if not isinstance(from_addrs, list):
raise TypeError("%s is not a list" % from_addrs)
self._message["From"] = COMMASPACE.join(from_addrs)
self._message["Reply-To"] = from_addrs[0]
@property
def to_addrs(self):
if self._message["To"] is None:
return []
return self._message["To"].split(COMMASPACE)
@to_addrs.setter
def to_addrs(self, to_addrs):
if not isinstance(to_addrs, list):
raise TypeError("%s is not a list" % to_addrs)
self._message["To"] = COMMASPACE.join(to_addrs)
@property
def cc_addrs(self):
if self._message["Cc"] is None:
return []
return self._message["Cc"].split(COMMASPACE)
@cc_addrs.setter
def cc_addrs(self, cc_addrs):
if not isinstance(cc_addrs, list):
raise TypeError("%s is not a list" % cc_addrs)
self._message["Cc"] = COMMASPACE.join(cc_addrs)
@property
def bcc_addrs(self):
if self._message["Bcc"] is None:
return []
return self._message["Bcc"].split(COMMASPACE)
@bcc_addrs.setter
def bcc_addrs(self, bcc_addrs):
if not isinstance(bcc_addrs, list):
raise TypeError("%s is not a list" % bcc_addrs)
self._message["Bcc"] = COMMASPACE.join(bcc_addrs)
@property
def subject(self):
return self._message["Subject"]
@subject.setter
def subject(self, subject_template):
self._templatizer.template = subject_template
self._message["Subject"] = self._templatizer.result
def body(self, body_template):
if os.path.isfile(body_template):
ctype, encoding = mimetypes.guess_type(body_template)
if ctype is None or encoding is not None:
ctype = "text/plain"
maintype, subtype = ctype.split('/', 1)
if maintype != "text":
raise TypeError("%s is not a valid body template", body_template)
self._templatizer.template = body_template
body = email.MIMEText.MIMEText(self._templatizer.result, subtype)
self._message.attach(body)
body = property(fset=body)
def next_attachment(self, attachment_path):
if not os.path.isfile(attachment_path):
raise TypeError("%s is not a file" % attachment_path)
ctype, encoding = mimetypes.guess_type(attachment_path)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split('/', 1)
with open(attachment_path, "rb") as f:
attachment = email.MIMEBase.MIMEBase(maintype, subtype)
attachment.set_payload(f.read())
if not maintype in ["audio", "image", "text"]:
email.encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment",
filename=os.path.split(attachment_path)[1])
self._message.attach(attachment)
next_attachment = property(fset=next_attachment)
def __str__(self):
return self._message.as_string()
#-- class Tmail --#