author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 27 Jul 2009 18:41:29 +0200 | |
changeset 2520 | 8c5cf48ae9ea |
parent 2476 | 1294a6bdf3bf |
child 2879 | ae26a80c0635 |
permissions | -rw-r--r-- |
0 | 1 |
"""Common utilies to format / semd emails. |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from email.MIMEMultipart import MIMEMultipart |
|
11 |
from email.MIMEText import MIMEText |
|
12 |
from email.MIMEImage import MIMEImage |
|
13 |
from email.Header import Header |
|
14 |
||
15 |
||
16 |
def header(ustring): |
|
17 |
return Header(ustring.encode('UTF-8'), 'UTF-8') |
|
18 |
||
19 |
def addrheader(uaddr, uname=None): |
|
20 |
# even if an email address should be ascii, encode it using utf8 since |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
21 |
# automatic tests may generate non ascii email address |
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
22 |
addr = uaddr.encode('UTF-8') |
0 | 23 |
if uname: |
24 |
return '%s <%s>' % (header(uname).encode(), addr) |
|
25 |
return addr |
|
26 |
||
27 |
||
28 |
def format_mail(uinfo, to_addrs, content, subject="", |
|
29 |
cc_addrs=(), msgid=None, references=(), config=None): |
|
30 |
"""Sends an Email to 'e_addr' with content 'content', and subject 'subject' |
|
31 |
||
32 |
to_addrs and cc_addrs are expected to be a list of email address without |
|
33 |
name |
|
34 |
""" |
|
35 |
assert type(content) is unicode, repr(content) |
|
36 |
msg = MIMEText(content.encode('UTF-8'), 'plain', 'UTF-8') |
|
37 |
# safety: keep only the first newline |
|
38 |
subject = subject.splitlines()[0] |
|
39 |
msg['Subject'] = header(subject) |
|
40 |
if uinfo.get('email'): |
|
41 |
email = uinfo['email'] |
|
42 |
elif config and config['sender-addr']: |
|
43 |
email = unicode(config['sender-addr']) |
|
44 |
else: |
|
45 |
email = u'' |
|
46 |
if uinfo.get('name'): |
|
47 |
name = uinfo['name'] |
|
48 |
elif config and config['sender-addr']: |
|
49 |
name = unicode(config['sender-name']) |
|
50 |
else: |
|
51 |
name = u'' |
|
52 |
msg['From'] = addrheader(email, name) |
|
53 |
if config and config['sender-addr'] and config['sender-addr'] != email: |
|
54 |
appaddr = addrheader(config['sender-addr'], config['sender-name']) |
|
55 |
msg['Reply-to'] = '%s, %s' % (msg['From'], appaddr) |
|
56 |
elif email: |
|
57 |
msg['Reply-to'] = msg['From'] |
|
58 |
if config is not None: |
|
59 |
msg['X-CW'] = config.appid |
|
425
cc9e8986d55e
sort email addresses and remove doubled ones before sending the email
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
60 |
unique_addrs = lambda addrs: sorted(set(addr for addr in addrs if addr is not None)) |
cc9e8986d55e
sort email addresses and remove doubled ones before sending the email
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
61 |
msg['To'] = ', '.join(addrheader(addr) for addr in unique_addrs(to_addrs)) |
0 | 62 |
if cc_addrs: |
425
cc9e8986d55e
sort email addresses and remove doubled ones before sending the email
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
63 |
msg['Cc'] = ', '.join(addrheader(addr) for addr in unique_addrs(cc_addrs)) |
0 | 64 |
if msgid: |
65 |
msg['Message-id'] = msgid |
|
66 |
if references: |
|
67 |
msg['References'] = ', '.join(references) |
|
68 |
return msg |
|
69 |
||
70 |
||
71 |
class HtmlEmail(MIMEMultipart): |
|
72 |
||
73 |
def __init__(self, subject, textcontent, htmlcontent, |
|
74 |
sendermail=None, sendername=None, recipients=None, ccrecipients=None): |
|
75 |
MIMEMultipart.__init__(self, 'related') |
|
76 |
self['Subject'] = header(subject) |
|
77 |
self.preamble = 'This is a multi-part message in MIME format.' |
|
78 |
# Attach alternative text message |
|
79 |
alternative = MIMEMultipart('alternative') |
|
80 |
self.attach(alternative) |
|
81 |
msgtext = MIMEText(textcontent.encode('UTF-8'), 'plain', 'UTF-8') |
|
82 |
alternative.attach(msgtext) |
|
83 |
# Attach html message |
|
84 |
msghtml = MIMEText(htmlcontent.encode('UTF-8'), 'html', 'UTF-8') |
|
85 |
alternative.attach(msghtml) |
|
86 |
if sendermail or sendername: |
|
87 |
self['From'] = addrheader(sendermail, sendername) |
|
88 |
if recipients: |
|
89 |
self['To'] = ', '.join(addrheader(addr) for addr in recipients if addr is not None) |
|
90 |
if ccrecipients: |
|
91 |
self['Cc'] = ', '.join(addrheader(addr) for addr in ccrecipients if addr is not None) |
|
92 |
||
93 |
def attach_image(self, data, htmlId): |
|
94 |
image = MIMEImage(data) |
|
95 |
image.add_header('Content-ID', '<%s>' % htmlId) |
|
96 |
self.attach(image) |