2015-11-21 44 views
3

我使用pyramid_mailer包在我的金字塔應用程序發送電子郵件。什麼是在金字塔視圖中爲電子郵件呈現jinja2模板的正確方法?

我找到了一些例子,其中電子郵件的正文只使用簡單的字符串。顯然,在真正的應用程序中,我需要使用更復雜的模板,並將視圖中填充的信息調用。

例如:

# templates/mail/notification.jinja2 
Dear {{username}},<br> blah blah blah... 

我用Jinja2的爲模板,用以下解決方案上來:

@view_config(route_name='email', renderer='templates/email.jinja2') 
def send_email(request): 
    import pyramid_jinja2 
    user = # ... got user from DB 
    env = pyramid_jinja2.get_jinja2_environment(request) 
    template = env.get_template('/templates/mail/notification.jinja2') 
    message = template.render({'username': user.name}) 

它的工作原理,但看起來有點混亂。

所以,這裏的問題是:這是正確的方式做到這一點?

回答

1

請參見下面的代碼:

from pyramid.renderers import render 

from pyramid_mailer import get_mailer 
from pyramid_mailer.message import Message 

import premailer 


def send_templated_mail(request, recipients, template, context, sender=None): 
    """Send out templatized HTML and plain text emails. 

    Each HTML email should have a plain text fallback. Premailer package is used to convert any CSS styles in HTML email messages to inline, so that email clients display them. 

    The email is assembled from three different templates: 

    * Read subject from a subject specific template $template.subject.txt 

    * Generate HTML email from HTML template, $template.body.html 

    * Generate plain text email from HTML template, $template.body.txt 

    Make sure you have configured your template engine (Jinja 2) to read TXT templates beside HTML. 

    :param request: HTTP request, passed to the template engine. Request configuration is used to get hold of the configured mailer. 

    :param recipients: List of recipient emails 

    :param template: Template filename base string for template tripled (subject, HTML body, plain text body). For example ``email/my_message`` would map to templates ``email/my_message.subject.txt``, ``email/my_message.body.txt``, ``email/my_message.body.html`` 

    :param context: Template context variables as a dict 

    :param sender: Override the sender email - if not specific use the default set in the config as ``mail.default_sender`` 
    """ 

    assert recipients 
    assert len(recipients) > 0 
    assert type(recipients) != str, "Please give a list of recipients, not a string" 

    subject = render(template + ".subject.txt", context, request=request) 
    subject = subject.strip() 

    html_body = render(template + ".body.html", context, request=request) 
    text_body = render(template + ".body.txt", context, request=request) 

    if not sender: 
     sender = request.registry.settings["mail.default_sender"] 

    # Inline CSS styles 
    html_body = premailer.transform(html_body) 

    message = Message(subject=subject, sender=sender, recipients=recipients, body=text_body, html=html_body) 

    mailer = get_mailer(request) 
    mailer.send(message) 
+0

謝謝!我會嘗試。出於某種原因,我認爲它創建了響應(使用標題,我不需要)。我會再檢查一次,並儘快接受答案。 –

+1

發送電子郵件不應該是查看(您現在擁有@view_config),而是您希望發送電子郵件的視圖中調用的函數。通常在處理HTTP POST之後。 –

相關問題