2018-11-27 12:35:16 -08:00
|
|
|
|
from flask import render_template, Markup, current_app
|
2018-10-19 22:18:27 -07:00
|
|
|
|
|
2018-10-19 15:20:51 -07:00
|
|
|
|
from grant.extensions import mail
|
|
|
|
|
|
|
|
|
|
default_template_args = {
|
2018-10-19 22:18:27 -07:00
|
|
|
|
'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-10-19 15:20:51 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 08:16:52 -08:00
|
|
|
|
def signup_info(email_args):
|
|
|
|
|
return {
|
2018-10-19 22:18:27 -07:00
|
|
|
|
'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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def team_invite_info(email_args):
|
|
|
|
|
return {
|
|
|
|
|
'subject': '{} has invited you to a project'.format(email_args.inviter.display_name),
|
|
|
|
|
'title': 'You’ve been invited!',
|
|
|
|
|
'preview': 'You’ve been invited to the "{}" project team'.format(email_args.proposal.title)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get_info_lookup = {
|
|
|
|
|
'signup': signup_info,
|
|
|
|
|
'team_invite': team_invite_info
|
2018-10-19 15:20:51 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 22:18:27 -07:00
|
|
|
|
|
2018-10-19 15:20:51 -07:00
|
|
|
|
def send_email(to, type, email_args):
|
2018-11-28 13:56:19 -08:00
|
|
|
|
if current_app and current_app.config.get("TESTING"):
|
2018-11-27 12:35:16 -08:00
|
|
|
|
return
|
|
|
|
|
|
2018-10-19 22:18:27 -07:00
|
|
|
|
try:
|
2018-11-16 08:16:52 -08:00
|
|
|
|
info = get_info_lookup[type](email_args)
|
2018-10-19 22:18:27 -07:00
|
|
|
|
body_text = render_template('emails/%s.txt' % (type), args=email_args)
|
|
|
|
|
body_html = render_template('emails/%s.html' % (type), args=email_args)
|
2018-10-19 15:20:51 -07:00
|
|
|
|
|
2018-10-19 22:18:27 -07:00
|
|
|
|
html = render_template('emails/template.html', args={
|
|
|
|
|
**default_template_args,
|
2018-11-16 08:16:52 -08:00
|
|
|
|
**info,
|
2018-10-19 22:18:27 -07:00
|
|
|
|
'body': Markup(body_html),
|
|
|
|
|
})
|
|
|
|
|
text = render_template('emails/template.txt', args={
|
|
|
|
|
**default_template_args,
|
2018-11-16 08:16:52 -08:00
|
|
|
|
**info,
|
2018-10-19 22:18:27 -07:00
|
|
|
|
'body': body_text,
|
|
|
|
|
})
|
2018-10-19 15:20:51 -07:00
|
|
|
|
|
2018-10-19 22:18:27 -07:00
|
|
|
|
res = mail.send_email(
|
|
|
|
|
to_email=to,
|
2018-11-16 08:16:52 -08:00
|
|
|
|
subject=info['subject'],
|
2018-10-19 22:18:27 -07:00
|
|
|
|
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))
|