Skip to content
Snippets Groups Projects
email.py 710 B
Newer Older
  • Learn to ignore specific revisions
  • from flask import current_app, render_template
    
    Patrick Jentsch's avatar
    Patrick Jentsch committed
    from flask_mail import Message
    
    from typing import Any, Text
    
    Patrick Jentsch's avatar
    Patrick Jentsch committed
    from . import mail
    
    from .decorators import background
    
    def create_message(
        recipient: str,
        subject: str,
        template: str,
        **kwargs: Any
    ) -> Message:
        subject_prefix: str = current_app.config['NOPAQUE_MAIL_SUBJECT_PREFIX']
        msg: Message = Message(
    
            f'{subject_prefix} {subject}',
            recipients=[recipient]
        )
    
    Patrick Jentsch's avatar
    Patrick Jentsch committed
        msg.body = render_template(f'{template}.txt.j2', **kwargs)
        msg.html = render_template(f'{template}.html.j2', **kwargs)
    
    def send(msg: Message, *args, **kwargs):
    
    Patrick Jentsch's avatar
    Patrick Jentsch committed
        with kwargs['app'].app_context():
    
    Patrick Jentsch's avatar
    Patrick Jentsch committed
            mail.send(msg)