author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 21 Oct 2015 16:32:11 +0200 | |
changeset 10863 | 8e1f6de61300 |
parent 10702 | f94c812c3669 |
child 11012 | 739943455c9d |
permissions | -rw-r--r-- |
8599
4fabc81cd924
docstring typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8056
diff
changeset
|
1 |
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
8599
4fabc81cd924
docstring typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8056
diff
changeset
|
18 |
"""Common utilies to format / send emails.""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
22 |
from base64 import b64encode, b64decode |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
23 |
from time import time |
7879
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7815
diff
changeset
|
24 |
from email.mime.multipart import MIMEMultipart |
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7815
diff
changeset
|
25 |
from email.mime.text import MIMEText |
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7815
diff
changeset
|
26 |
from email.mime.image import MIMEImage |
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7815
diff
changeset
|
27 |
from email.header import Header |
10326
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9362
diff
changeset
|
28 |
from email.utils import formatdate |
8056
8909800a8c51
[cleanup] drop some appengine support junk
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
29 |
from socket import gethostname |
0 | 30 |
|
10701
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
31 |
from six import PY2, PY3, text_type |
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
32 |
|
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
33 |
|
0 | 34 |
def header(ustring): |
10701
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
35 |
if PY3: |
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
36 |
return Header(ustring, 'utf-8') |
0 | 37 |
return Header(ustring.encode('UTF-8'), 'UTF-8') |
38 |
||
39 |
def addrheader(uaddr, uname=None): |
|
40 |
# 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
|
41 |
# automatic tests may generate non ascii email address |
10701
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
42 |
if PY2: |
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
43 |
addr = uaddr.encode('UTF-8') |
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
44 |
else: |
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
45 |
addr = uaddr |
0 | 46 |
if uname: |
10701
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
47 |
val = '%s <%s>' % (header(uname).encode(), addr) |
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
48 |
else: |
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
49 |
val = addr |
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
50 |
assert isinstance(val, str) # bytes in py2, ascii-encoded unicode in py3 |
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
51 |
return val |
0 | 52 |
|
53 |
||
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
54 |
def construct_message_id(appid, eid, withtimestamp=True): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
55 |
if withtimestamp: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
56 |
addrpart = 'eid=%s×tamp=%.10f' % (eid, time()) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
57 |
else: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
58 |
addrpart = 'eid=%s' % eid |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
59 |
# we don't want any equal sign nor trailing newlines |
10701
ca3efd72e854
[mail] port to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10326
diff
changeset
|
60 |
leftpart = b64encode(addrpart.encode('ascii'), b'.-').decode('ascii').rstrip().rstrip('=') |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
61 |
return '<%s@%s.%s>' % (leftpart, appid, gethostname()) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
62 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
63 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
64 |
def parse_message_id(msgid, appid): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
65 |
if msgid[0] == '<': |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
66 |
msgid = msgid[1:] |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
67 |
if msgid[-1] == '>': |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
68 |
msgid = msgid[:-1] |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
69 |
try: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
70 |
values, qualif = msgid.split('@') |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
71 |
padding = len(values) % 4 |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
72 |
values = b64decode(str(values + '='*padding), '.-') |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
73 |
values = dict(v.split('=') for v in values.split('&')) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
74 |
fromappid, host = qualif.split('.', 1) |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5736
diff
changeset
|
75 |
except Exception: |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
76 |
return None |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
77 |
if appid != fromappid or host != gethostname(): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
78 |
return None |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
79 |
return values |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
80 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
81 |
|
0 | 82 |
def format_mail(uinfo, to_addrs, content, subject="", |
83 |
cc_addrs=(), msgid=None, references=(), config=None): |
|
84 |
"""Sends an Email to 'e_addr' with content 'content', and subject 'subject' |
|
85 |
||
86 |
to_addrs and cc_addrs are expected to be a list of email address without |
|
87 |
name |
|
88 |
""" |
|
10702
f94c812c3669
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10701
diff
changeset
|
89 |
assert isinstance(content, text_type), repr(content) |
0 | 90 |
msg = MIMEText(content.encode('UTF-8'), 'plain', 'UTF-8') |
91 |
# safety: keep only the first newline |
|
5579
297b2002a083
fix 978144: exception with view send-email if empty subject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
92 |
try: |
297b2002a083
fix 978144: exception with view send-email if empty subject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
93 |
subject = subject.splitlines()[0] |
297b2002a083
fix 978144: exception with view send-email if empty subject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
94 |
msg['Subject'] = header(subject) |
297b2002a083
fix 978144: exception with view send-email if empty subject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
95 |
except IndexError: |
297b2002a083
fix 978144: exception with view send-email if empty subject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
96 |
pass # no subject |
0 | 97 |
if uinfo.get('email'): |
98 |
email = uinfo['email'] |
|
99 |
elif config and config['sender-addr']: |
|
10702
f94c812c3669
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10701
diff
changeset
|
100 |
email = text_type(config['sender-addr']) |
0 | 101 |
else: |
102 |
email = u'' |
|
103 |
if uinfo.get('name'): |
|
104 |
name = uinfo['name'] |
|
9362
2f0129d17fa9
[mail] format_mail copy/paste fix: we want to test sender-name, not sender-addr
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8931
diff
changeset
|
105 |
elif config and config['sender-name']: |
10702
f94c812c3669
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10701
diff
changeset
|
106 |
name = text_type(config['sender-name']) |
0 | 107 |
else: |
108 |
name = u'' |
|
109 |
msg['From'] = addrheader(email, name) |
|
110 |
if config and config['sender-addr'] and config['sender-addr'] != email: |
|
111 |
appaddr = addrheader(config['sender-addr'], config['sender-name']) |
|
112 |
msg['Reply-to'] = '%s, %s' % (msg['From'], appaddr) |
|
113 |
elif email: |
|
114 |
msg['Reply-to'] = msg['From'] |
|
115 |
if config is not None: |
|
116 |
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
|
117 |
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
|
118 |
msg['To'] = ', '.join(addrheader(addr) for addr in unique_addrs(to_addrs)) |
0 | 119 |
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
|
120 |
msg['Cc'] = ', '.join(addrheader(addr) for addr in unique_addrs(cc_addrs)) |
0 | 121 |
if msgid: |
122 |
msg['Message-id'] = msgid |
|
123 |
if references: |
|
124 |
msg['References'] = ', '.join(references) |
|
10326
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9362
diff
changeset
|
125 |
msg['Date'] = formatdate() |
0 | 126 |
return msg |
127 |
||
128 |
||
129 |
class HtmlEmail(MIMEMultipart): |
|
130 |
||
131 |
def __init__(self, subject, textcontent, htmlcontent, |
|
132 |
sendermail=None, sendername=None, recipients=None, ccrecipients=None): |
|
133 |
MIMEMultipart.__init__(self, 'related') |
|
134 |
self['Subject'] = header(subject) |
|
135 |
self.preamble = 'This is a multi-part message in MIME format.' |
|
136 |
# Attach alternative text message |
|
137 |
alternative = MIMEMultipart('alternative') |
|
138 |
self.attach(alternative) |
|
139 |
msgtext = MIMEText(textcontent.encode('UTF-8'), 'plain', 'UTF-8') |
|
140 |
alternative.attach(msgtext) |
|
141 |
# Attach html message |
|
142 |
msghtml = MIMEText(htmlcontent.encode('UTF-8'), 'html', 'UTF-8') |
|
143 |
alternative.attach(msghtml) |
|
144 |
if sendermail or sendername: |
|
145 |
self['From'] = addrheader(sendermail, sendername) |
|
146 |
if recipients: |
|
147 |
self['To'] = ', '.join(addrheader(addr) for addr in recipients if addr is not None) |
|
148 |
if ccrecipients: |
|
149 |
self['Cc'] = ', '.join(addrheader(addr) for addr in ccrecipients if addr is not None) |
|
150 |
||
151 |
def attach_image(self, data, htmlId): |
|
152 |
image = MIMEImage(data) |
|
153 |
image.add_header('Content-ID', '<%s>' % htmlId) |
|
154 |
self.attach(image) |