12 |
12 |
13 __docformat__ = 'restructuredtext' |
13 __docformat__ = 'restructuredtext' |
14 |
14 |
15 |
15 |
16 # import standard library |
16 # import standard library |
17 import chardet |
|
18 import codecs |
17 import codecs |
19 from datetime import datetime |
|
20 from email.mime.multipart import MIMEMultipart |
|
21 from email.mime.text import MIMEText |
|
22 from html import entities |
18 from html import entities |
23 |
19 |
24 # import interfaces |
20 # import interfaces |
25 |
21 |
26 # import packages |
22 # import packages |
27 from pyams_utils.html import html_to_text |
23 from pyams_utils.html import html_to_text |
28 from pyams_utils.timezone import gmtime |
24 from pyramid_mailer.message import Message |
29 |
25 |
30 |
26 |
31 def html_replace(exc): |
27 def html_replace(exc): |
32 if isinstance(exc, (UnicodeEncodeError, UnicodeTranslateError)): |
28 if isinstance(exc, (UnicodeEncodeError, UnicodeTranslateError)): |
33 s = ['&%s;' % entities.codepoint2name[ord(c)] for c in exc.objet[exc.start:exc.end]] |
29 s = ['&%s;' % entities.codepoint2name[ord(c)] for c in exc.objet[exc.start:exc.end]] |
39 |
35 |
40 def html_encode(unicode_data, encoding='utf-8'): |
36 def html_encode(unicode_data, encoding='utf-8'): |
41 return unicode_data.encode(encoding, 'html_replace') |
37 return unicode_data.encode(encoding, 'html_replace') |
42 |
38 |
43 |
39 |
44 def HTMLMessage(subject, fromaddr, toaddr, html, text=None): |
40 def HTMLMessage(subject, fromaddr, toaddr, html, text=None, encoding='utf-8'): |
45 """Create a MIME message that will render as HTML or text |
41 """Create a MIME message that will render as HTML or text""" |
46 |
42 html = html_encode(html, encoding).decode(encoding) |
47 Copied from 'Python Cookbook', chapter 13.5""" |
|
48 html = html_encode(html) |
|
49 if text is None: |
43 if text is None: |
50 # produce textual rendering of the HTML string when None is provided |
44 # produce textual rendering of the HTML string when None is provided |
51 text = html_to_text(html) |
45 text = html_to_text(html) |
52 msg = MIMEMultipart() |
|
53 msg['Subject'] = subject |
|
54 msg['Date'] = gmtime(datetime.utcnow()).strftime('%a, %d %b %Y %H:%M:%S %z (%Z)') |
|
55 msg['From'] = fromaddr |
|
56 if isinstance(toaddr, str): |
46 if isinstance(toaddr, str): |
57 toaddr = (toaddr, ) |
47 toaddr = (toaddr, ) |
58 msg['To'] = ', '.join(toaddr) |
48 return Message(subject=subject, |
59 parts = MIMEMultipart('alternative') |
49 sender=fromaddr, |
60 plain_part = MIMEText(text, 'plain') |
50 recipients=toaddr, |
61 plain_part.set_charset('utf-8') |
51 html=html, |
62 html_part = MIMEText(html, 'html') |
52 body=text) |
63 html_part.set_charset('utf-8') |
|
64 parts.attach(plain_part) |
|
65 parts.attach(html_part) |
|
66 msg.attach(parts) |
|
67 return msg |
|
68 |
53 |
69 |
54 |
70 def TextMessage(subject, fromaddr, toaddr, text): |
55 def TextMessage(subject, fromaddr, toaddr, text): |
71 """Create a text message""" |
56 """Create a text message""" |
72 msg = MIMEMultipart() |
|
73 msg['Subject'] = subject |
|
74 msg['Date'] = gmtime(datetime.utcnow()).strftime('%a, %d %b %Y %H:%M:%S %z (%Z)') |
|
75 msg['From'] = fromaddr |
|
76 if isinstance(toaddr, str): |
57 if isinstance(toaddr, str): |
77 toaddr = (toaddr, ) |
58 toaddr = (toaddr, ) |
78 msg['To'] = ', '.join(toaddr) |
59 return Message(subject=subject, |
79 if isinstance(text, str): |
60 sender=fromaddr, |
80 text = text.encode() |
61 recipients=toaddr, |
81 charset = chardet.detect(text).get('encoding', 'utf-8') |
62 body=text) |
82 plain_part = MIMEText(text, 'plain', charset) |
|
83 msg.attach(plain_part) |
|
84 return msg |
|