/
Email.py
145 lines (111 loc) · 4.58 KB
/
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
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
#!/usr/bin/python2.4
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
#
# $Id$
#
# Copyright (C) 1999-2006 Keith Dart <keith@kdart.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
"""
Report objects that sends a text email.
"""
import os
from cStringIO import StringIO
import chardet
from pycopia import reports
NO_MESSAGE = reports.NO_MESSAGE
from pycopia import ezmail
class EmailReport(reports.NullReport):
"""Create an a report that is emailed, rather than written to a file.
EmailReport(
[formatter="text/plain"], # formatter type
[recipients=None], # list of recipients, or None. If none the
# message is mailed to self (From address).
[From=None], # Address for From field. If None the current user is used.
)
"""
def __init__(self, formatter="text/plain", recipients=None, From=None, attach_logfile=False):
self._logfile = None
self._message = ezmail.MultipartMessage()
self._message.From(From)
self._message.To(recipients)
self._attach_logfile = attach_logfile
self._formatter, ext = reports.get_formatter(formatter)
filename = property(lambda s: None)
filenames = property(lambda s: [])
mimetype = property(lambda s: s._formatter.MIMETYPE)
def initialize(self, config=None):
self._fo = StringIO()
self.write(self._formatter.initialize())
def logfile(self, lf):
self._logfile = str(lf)
def write(self, text):
self._fo.write(text)
def writeline(self, text):
self._fo.write(text)
self._fo.write("\n")
def finalize(self):
"""finalizing this Report sends off the email."""
self.write(self._formatter.finalize())
report = ezmail.MIMEText.MIMEText(self._fo.getvalue(),
self._formatter.MIMETYPE.split("/")[1])
report["Content-Disposition"] = "inline"
self._message.attach(report)
if self._attach_logfile and self._logfile:
try:
lfd = open(self._logfile, "rb").read()
except:
pass # non-fatal
else:
logmsg = ezmail.MIMEText.MIMEText(lfd, charset=chardet.detect(lfd))
logmsg["Content-Disposition"] = 'attachment; filename=%s' % (
os.path.basename(self._logfile), )
self._message.attach(logmsg)
ezmail.mail(self._message)
def add_title(self, title):
self._message.add_header("Subject", title)
self.write(self._formatter.title(title))
def add_heading(self, text, level=1):
self.write(self._formatter.heading(text, level))
def add_message(self, msgtype, msg, level=1):
self.write(self._formatter.message(msgtype, msg, level))
def add_summary(self, entries):
lines = map(self._formatter.summaryline, entries)
self.write("\n".join(lines))
self.write("\n")
def passed(self, msg=NO_MESSAGE, level=1):
self.add_message("PASSED", msg, level)
def failed(self, msg=NO_MESSAGE, level=1):
self.add_message("FAILED", msg, level)
def expectedfail(self, msg=NO_MESSAGE, level=1):
self.add_message("EXPECTED_FAIL", msg, level)
def incomplete(self, msg=NO_MESSAGE, level=1):
self.add_message("INCOMPLETE", msg, level)
def abort(self, msg=NO_MESSAGE, level=1):
self.add_message("ABORTED", msg, level)
def info(self, msg, level=1):
self.add_message("INFO", msg, level)
def diagnostic(self, msg, level=1):
self.add_message("DIAGNOSTIC", msg, level)
def add_text(self, text):
self.write(self._formatter.text(text))
def add_url(self, text, url):
self.write(self._formatter.url(text, url))
def newpage(self):
self.write(self._formatter.newpage())
def newsection(self):
self.write(self._formatter.section())
if __name__ == "__main__":
rpt = EmailReport("text/plain", recipients=["kdart@kdart.com"])
rpt.initialize()
rpt.add_title("Email report self test.")
rpt.info("Some non-useful info. 8-)")
rpt.finalize()