source: OpenWorkouts-current/ow/mail.py @ 8c2b094

current
Last change on this file since 8c2b094 was 1829fbf, checked in by Borja Lopez <borja@…>, 5 years ago

(#61) Several fixes for the multipart/alternative (text+html) version of the verify
your account email:

  • Use premailer [1] to embed all styles into the body of the html part of the email (ensuring the styles are rendered correctly in most email clients)
  • Fixed some styles (RGB colors, transparencies, etc don't work in mail clients)
  • Ensure the different bodies (text and html) have the proper content type and charset set.

[1] https://pypi.org/project/premailer

  • Property mode set to 100644
File size: 2.3 KB
Line 
1import logging
2
3from pyramid_mailer import get_mailer
4from pyramid_mailer.message import Message, Attachment
5from pyramid.renderers import render
6from pyramid.i18n import TranslationStringFactory
7
8import premailer
9
10_ = TranslationStringFactory('OpenWorkouts')
11
12log = logging.getLogger(__name__)
13
14
15def idna_encode_recipients(message):
16    """
17    Given a pyramid_mailer.message.Message instance, check if the recipient(s)
18    domain name(s) contain non-ascii characters and, if so, encode the domain
19    name part of the address(es) properly
20
21    >>> from pyramid_mailer.message import Message
22    >>> message = Message(subject='s', recipients=['a@a.com'], body='b')
23    >>> assert message.recipients == ['a@a.com']
24    >>> message = idna_encode_recipients(message)
25    >>> assert message.recipients == ['a@a.com']
26    >>> message.recipients.append(u'a@\xfc.de')
27    >>> assert message.recipients == ['a@a.com', u'a@\xfc.de']
28    >>> message = idna_encode_recipients(message)
29    >>> message.recipients
30    ['a@a.com', 'a@xn--tda.de']
31    """
32    recipients = []
33    for rcpt in message.recipients:
34        username, domain = rcpt.split('@')
35        recipients.append(
36            '@'.join([username, domain.encode('idna').decode('utf-8')]))
37    message.recipients = recipients
38    return message
39
40
41def send_verification_email(request, user):
42    subject = _('Welcome to OpenWorkouts')
43    txt_template = 'ow:templates/mail_verify_account_txt.pt'
44    html_template = 'ow:templates/mail_verify_account_html.pt'
45    verify_link = request.resource_url(user, 'verify', user.verification_token)
46    context = {'user': user, 'verify_link': verify_link}
47    mailer = get_mailer(request)
48    txt_body = render(txt_template, context, request)
49    html_body = premailer.transform(render(html_template, context, request))
50    message = Message(
51        subject=subject,
52        recipients=[user.email],
53        body=Attachment(data=txt_body,
54                        content_type="text/plain; charset=utf-8",
55                        transfer_encoding="quoted-printable"),
56        html=Attachment(data=html_body,
57                        content_type="text/html; charset=utf-8",
58                        transfer_encoding="quoted-printable")
59    )
60    message = idna_encode_recipients(message)
61    mailer.send_to_queue(message)
Note: See TracBrowser for help on using the repository browser.