Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Sending emails
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Sep 10, 2014
1 parent 0be9a03 commit de55e51
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion defaults.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 12 additions & 2 deletions gratipay/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
34 changes: 34 additions & 0 deletions gratipay/utils/emails.py
Original file line number Diff line number Diff line change
@@ -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': '[email protected]',
'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!
<a href="%s">Click on this link</a> to verify your email.
""" % link
return send_email(participant.email.address, participant.username, subject, body)
1 change: 1 addition & 0 deletions js/gratipay/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit de55e51

Please sign in to comment.