def send_template_message(template=None, ctx=None, *args, **kwargs):
    """
    Send a templated e-mail using a similar signature as Flask-Mail:
    http://pythonhosted.org/Flask-Mail/

    Except, it also supports template rendering. If you want to use a template
    then just omit the body and html kwargs to Flask-Mail and instead supply
    a path to a template. It will auto-lookup and render text/html messages.

    Example:
        ctx = {'user': current_user, 'reset_token': token}
        send_template_message('Password reset from Foo', ['*****@*****.**'],
                              template='user/mail/password_reset', ctx=ctx)

    :param subject:
    :param recipients:
    :param body:
    :param html:
    :param sender:
    :param cc:
    :param bcc:
    :param attachments:
    :param reply_to:
    :param date:
    :param charset:
    :param extra_headers:
    :param mail_options:
    :param rcpt_options:
    :param template: Path to a template without the extension
    :param context: Dictionary of anything you want in the template context
    :return: None
    """
    if ctx is None:
        ctx = {}

    if template is not None:
        if 'body' in kwargs:
            raise Exception(_('You cannot have both a template and body arg.'))
        elif 'html' in kwargs:
            raise Exception(_('You cannot have both a template and body arg.'))

        kwargs['body'] = _try_renderer_template(template, **ctx)
        kwargs['html'] = _try_renderer_template(template, ext='html', **ctx)

    mail.send_message(*args, **kwargs)

    return None
Example #2
0
def deliver_support_email(issue_id, subject, message):
    """
    Send a contact message to the person who sent an issue.

    :param user_id: Id of the user
    :type user_id: int
    :param subject: E-mail subject
    :type subject: str
    :param message: E-mail message
    :type message: str
    :return: None
    """
    issue = Issue.query.get(issue_id)

    if issue is None:
        return

    mail.send_message(sender=celery.conf.get('MAIL_USERNAME'),
                      recipients=[issue.email],
                      reply_to=celery.conf.get('MAIL_USERNAME'),
                      subject=subject,
                      body=message)

    return None