import logging import traceback from flask.ext.mail import Message from app import mail, app, get_app_url from data import model from util.gravatar import compute_hash from util.jinjautil import get_template_env logger = logging.getLogger(__name__) template_env = get_template_env("emails") class CannotSendEmailException(Exception): pass def send_email(recipient, subject, template_file, parameters): app_title = app.config['REGISTRY_TITLE_SHORT'] app_url = get_app_url() def app_link_handler(url=None, title=None): real_url = app_url + '/' + url if url else app_url if not title: title = real_url if url else app_title return '%s' % (real_url, title) parameters.update({ 'subject': subject, 'app_logo': 'https://quay.io/static/img/quay-logo.png', # TODO: make this pull from config 'app_url': app_url, 'app_title': app_title, 'app_link': app_link_handler }) rendered_html = template_env.get_template(template_file + '.html').render(parameters) msg = Message('[%s] %s' % (app_title, subject), recipients=[recipient]) msg.html = rendered_html try: mail.send(msg) except Exception as ex: raise CannotSendEmailException(ex.message) def send_password_changed(username, email): send_email(email, 'Account password changed', 'passwordchanged', { 'username': username }) def send_email_changed(username, old_email, new_email): send_email(old_email, 'Account e-mail address changed', 'emailchanged', { 'username': username, 'new_email': new_email }) def send_change_email(username, email, token): send_email(email, 'E-mail address change requested', 'changeemail', { 'username': username, 'token': token }) def send_confirmation_email(username, email, token): send_email(email, 'Please confirm your e-mail address', 'confirmemail', { 'username': username, 'token': token }) def send_repo_authorization_email(namespace, repository, email, token): subject = 'Please verify your e-mail address for repository %s/%s' % (namespace, repository) send_email(email, subject, 'repoauthorizeemail', { 'namespace': namespace, 'repository': repository, 'token': token }) def send_recovery_email(email, token): subject = 'Account recovery' send_email(email, subject, 'recovery', { 'email': email, 'token': token }) def send_payment_failed(email, username): send_email(email, 'Subscription Payment Failure', 'paymentfailure', { 'username': username }) def send_org_invite_email(member_name, member_email, orgname, team, adder, code): send_email(member_email, 'Invitation to join team', 'teaminvite', { 'inviter': adder, 'token': code, 'organization': orgname, 'teamname': team }) def send_invoice_email(email, contents): # Note: This completely generates the contents of the email, so we don't use the # normal template here. msg = Message('Quay.io payment received - Thank you!', recipients=[email]) msg.html = contents mail.send(msg) # INTERNAL EMAILS BELOW def send_subscription_change(change_description, customer_id, customer_email, quay_username): SUBSCRIPTION_CHANGE_TITLE = 'Subscription Change - {0} {1}' SUBSCRIPTION_CHANGE = """ Change: {0}
Customer id: {1}
Customer email: {2}
Quay user or org name: {3}
""" title = SUBSCRIPTION_CHANGE_TITLE.format(quay_username, change_description) msg = Message(title, recipients=['stripe@quay.io']) msg.html = SUBSCRIPTION_CHANGE.format(change_description, customer_id, customer_email, quay_username) mail.send(msg)