15 # |
15 # |
16 # You should have received a copy of the GNU Lesser General Public License along |
16 # You should have received a copy of the GNU Lesser General Public License along |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 """Common utilies to format / send emails.""" |
18 """Common utilies to format / send emails.""" |
19 |
19 |
20 |
|
21 |
|
22 from base64 import b64encode, b64decode |
20 from base64 import b64encode, b64decode |
23 from time import time |
21 from time import time |
24 from email.mime.multipart import MIMEMultipart |
22 from email.mime.multipart import MIMEMultipart |
25 from email.mime.text import MIMEText |
23 from email.mime.text import MIMEText |
26 from email.mime.image import MIMEImage |
24 from email.mime.image import MIMEImage |
27 from email.header import Header |
25 from email.header import Header |
28 from email.utils import formatdate |
26 from email.utils import formatdate |
29 from socket import gethostname |
27 from socket import gethostname |
30 |
28 |
31 from six import PY2, PY3, text_type |
|
32 |
|
33 |
29 |
34 def header(ustring): |
30 def header(ustring): |
35 if PY3: |
31 return Header(ustring, 'utf-8') |
36 return Header(ustring, 'utf-8') |
|
37 return Header(ustring.encode('UTF-8'), 'UTF-8') |
|
38 |
32 |
39 def addrheader(uaddr, uname=None): |
33 def addrheader(addr, uname=None): |
40 # even if an email address should be ascii, encode it using utf8 since |
34 # even if an email address should be ascii, encode it using utf8 since |
41 # automatic tests may generate non ascii email address |
35 # automatic tests may generate non ascii email address |
42 if PY2: |
|
43 addr = uaddr.encode('UTF-8') |
|
44 else: |
|
45 addr = uaddr |
|
46 if uname: |
36 if uname: |
47 val = '%s <%s>' % (header(uname).encode(), addr) |
37 val = '%s <%s>' % (header(uname).encode(), addr) |
48 else: |
38 else: |
49 val = addr |
39 val = addr |
50 assert isinstance(val, str) # bytes in py2, ascii-encoded unicode in py3 |
40 assert isinstance(val, str) # bytes in py2, ascii-encoded unicode in py3 |
84 """Sends an Email to 'e_addr' with content 'content', and subject 'subject' |
74 """Sends an Email to 'e_addr' with content 'content', and subject 'subject' |
85 |
75 |
86 to_addrs and cc_addrs are expected to be a list of email address without |
76 to_addrs and cc_addrs are expected to be a list of email address without |
87 name |
77 name |
88 """ |
78 """ |
89 assert isinstance(content, text_type), repr(content) |
79 assert isinstance(content, str), repr(content) |
90 msg = MIMEText(content.encode('UTF-8'), 'plain', 'UTF-8') |
80 msg = MIMEText(content.encode('UTF-8'), 'plain', 'UTF-8') |
91 # safety: keep only the first newline |
81 # safety: keep only the first newline |
92 try: |
82 try: |
93 subject = subject.splitlines()[0] |
83 subject = subject.splitlines()[0] |
94 msg['Subject'] = header(subject) |
84 msg['Subject'] = header(subject) |
95 except IndexError: |
85 except IndexError: |
96 pass # no subject |
86 pass # no subject |
97 if uinfo.get('email'): |
87 if uinfo.get('email'): |
98 email = uinfo['email'] |
88 email = uinfo['email'] |
99 elif config and config['sender-addr']: |
89 elif config and config['sender-addr']: |
100 email = text_type(config['sender-addr']) |
90 email = config['sender-addr'] |
101 else: |
91 else: |
102 email = u'' |
92 email = u'' |
103 if uinfo.get('name'): |
93 if uinfo.get('name'): |
104 name = uinfo['name'] |
94 name = uinfo['name'] |
105 elif config and config['sender-name']: |
95 elif config and config['sender-name']: |
106 name = text_type(config['sender-name']) |
96 name = config['sender-name'] |
107 else: |
97 else: |
108 name = u'' |
98 name = u'' |
109 msg['From'] = addrheader(email, name) |
99 msg['From'] = addrheader(email, name) |
110 if config and config['sender-addr'] and config['sender-addr'] != email: |
100 if config and config['sender-addr'] and config['sender-addr'] != email: |
111 appaddr = addrheader(config['sender-addr'], config['sender-name']) |
101 appaddr = addrheader(config['sender-addr'], config['sender-name']) |