source: OpenWorkouts-current/ow/mail.py @ 76ebb1b

currentfeature/docs
Last change on this file since 76ebb1b was 76ebb1b, checked in by Borja Lopez <borja@…>, 5 years ago

(#29) Add user verification by email on signup.

From now on, when a new user signs up, we set the account into an "unverified"
state. In order to complete the signup procedure, the user has to click on a
link we send by email to the email address provided on signup.

IMPORTANT: A new dependency has been added, pyramid_mailer, so remember to
install it in any existing openworkouts environment (this is done automatically
if you use the ./bin/start script):

pip install pyramid_mailer

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