zcash-grant-system/backend/grant/email/send.py

64 lines
1.9 KiB
Python
Raw Normal View History

2018-11-27 12:35:16 -08:00
from flask import render_template, Markup, current_app
from grant.extensions import mail
default_template_args = {
'home_url': 'https://grant.io',
'account_url': 'https://grant.io/user',
'email_settings_url': 'https://grant.io/user/settings',
'unsubscribe_url': 'https://grant.io/unsubscribe',
}
2018-12-14 11:36:22 -08:00
2018-11-16 08:16:52 -08:00
def signup_info(email_args):
return {
'subject': 'Confirm your email on Grant.io',
'title': 'Welcome to Grant.io!',
'preview': 'Welcome to Grant.io, we just need to confirm your email address.',
2018-11-16 08:16:52 -08:00
}
2018-12-14 11:36:22 -08:00
2018-11-16 08:16:52 -08:00
def team_invite_info(email_args):
return {
2018-12-03 18:45:18 -08:00
'subject': '{} has invited you to a project'.format(email_args['inviter'].display_name),
2018-11-16 08:16:52 -08:00
'title': 'Youve been invited!',
2018-12-03 18:45:18 -08:00
'preview': 'Youve been invited to the "{}" project team'.format(email_args['proposal'].title)
2018-11-16 08:16:52 -08:00
}
2018-12-14 11:36:22 -08:00
2018-11-16 08:16:52 -08:00
get_info_lookup = {
'signup': signup_info,
'team_invite': team_invite_info
}
def send_email(to, type, email_args):
if current_app and current_app.config.get("TESTING"):
2018-11-27 12:35:16 -08:00
return
try:
2018-11-16 08:16:52 -08:00
info = get_info_lookup[type](email_args)
body_text = render_template('emails/%s.txt' % (type), args=email_args)
body_html = render_template('emails/%s.html' % (type), args=email_args)
html = render_template('emails/template.html', args={
**default_template_args,
2018-11-16 08:16:52 -08:00
**info,
'body': Markup(body_html),
})
text = render_template('emails/template.txt', args={
**default_template_args,
2018-11-16 08:16:52 -08:00
**info,
'body': body_text,
})
res = mail.send_email(
to_email=to,
2018-11-16 08:16:52 -08:00
subject=info['subject'],
text=text,
html=html,
)
print('Just sent an email to %s of type %s, response code: %s' % (to, type, res.status_code))
except Exception as e:
print('An error occured while sending an email to %s - %s: %s' % (to, e.__class__.__name__, e))