From de55e518159dbfdf98036e8034caea6e9a55ac49 Mon Sep 17 00:00:00 2001 From: Rohit Paul Kuruvilla Date: Wed, 10 Sep 2014 01:31:17 +0530 Subject: [PATCH] Sending emails --- defaults.env | 2 +- gratipay/models/participant.py | 14 ++++++++++++-- gratipay/utils/emails.py | 34 ++++++++++++++++++++++++++++++++++ js/gratipay/account.js | 1 + 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 gratipay/utils/emails.py diff --git a/defaults.env b/defaults.env index 9870989087..7ef088f36b 100644 --- a/defaults.env +++ b/defaults.env @@ -60,6 +60,6 @@ ASPEN_WWW_ROOT=www/ # https://github.com/benoitc/gunicorn/issues/186 GUNICORN_OPTS="--workers=1 --timeout=99999999" -MANDRILL_KEY= +MANDRILL_KEY=eQeaTXtlBIuoBKb5ymL0aA RAISE_CARD_EXPIRATION=no diff --git a/gratipay/models/participant.py b/gratipay/models/participant.py index 7df7f417b4..ee9ea97746 100644 --- a/gratipay/models/participant.py +++ b/gratipay/models/participant.py @@ -39,6 +39,7 @@ from gratipay.utils.username import safely_reserve_a_username from gratipay import billing from gratipay.utils import is_card_expiring +from gratipay.utils.emails import send_verification_email ASCII_ALLOWED_IN_USERNAME = set("0123456789" @@ -550,19 +551,28 @@ def update_email(self, email, confirmed=False): current_email = self.email.address if hasattr(self.email,'address') else '' ctime = self.email.ctime if hasattr(self.email,'ctime') else utcnow() was_confirmed = self.email.confirmed if hasattr(self.email,'confirmed') else '' - if (email != current_email) or (email == current_email and confirmed == was_confirmed == False): + should_verify = (email != current_email) or (email == current_email and confirmed == was_confirmed == False) + if should_verify: confirmed = False hash_string = str(uuid.uuid4()) ctime = utcnow() - # Send the user an email here with self.db.get_cursor() as c: add_event(c, 'participant', dict(id=self.id, action='set', values=dict(current_email=email))) r = c.one("UPDATE participants SET email = ROW(%s, %s, %s, %s) WHERE username=%s RETURNING email" , (email, confirmed, hash_string, ctime,self.username) ) self.set_attributes(email=r) + if should_verify: + send_verification_email(self) return r + def get_verification_link(self): + hash_string = self.email.hash + username = self.username_lower + link = "%s://%s/%s/verify-email.html?hash=%s" % (gratipay.canonical_scheme, gratipay.canonical_host, username, hash_string) + return link + + def update_goal(self, goal): typecheck(goal, (Decimal, None)) with self.db.get_cursor() as c: diff --git a/gratipay/utils/emails.py b/gratipay/utils/emails.py new file mode 100644 index 0000000000..fbd6e2eb89 --- /dev/null +++ b/gratipay/utils/emails.py @@ -0,0 +1,34 @@ +import mandrill +import gratipay.wireup + +class MandrillError(Exception): pass + +def send_email(to_address, to_name, subject, body): + message = { + 'from_email': 'notifications@gratipay.com', + 'from_name': 'Gratipay', + 'to': [{'email': to_address, + 'name': to_name + }], + 'subject': subject, + 'html': body + } + try: + env = gratipay.wireup.env() + mail = gratipay.wireup.mail(env) + result = mail.messages.send(message=message) + return result + except mandrill.Error, e: + raise MandrillError + +def send_verification_email(participant): + subject = "Welcome to Gratipay!" + link = participant.get_verification_link() + # TODO - Improve body text + body = """ + Welcome to Gratipay! + + Click on this link to verify your email. + + """ % link + return send_email(participant.email.address, participant.username, subject, body) diff --git a/js/gratipay/account.js b/js/gratipay/account.js index 3995357b74..83fcc6287f 100644 --- a/js/gratipay/account.js +++ b/js/gratipay/account.js @@ -136,6 +136,7 @@ Gratipay.account.init = function() { $('.email-address').text(data.email); $('.email').toggle(); $('.toggle-email').show(); + Gratipay.notification('Your email address has been changed', 'notice'); if (data.email === '') { $('.toggle-email').text('+ Add'); // TODO i18n } else {