Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在金字塔视图中为电子邮件呈现jinja2模板的正确方法是什么?_Python_Jinja2_Pyramid - Fatal编程技术网

Python 在金字塔视图中为电子邮件呈现jinja2模板的正确方法是什么?

Python 在金字塔视图中为电子邮件呈现jinja2模板的正确方法是什么?,python,jinja2,pyramid,Python,Jinja2,Pyramid,我使用pyramid\u mailer软件包在我的pyramid应用程序中发送电子邮件 我找到了一些示例,其中作为电子邮件的主体仅使用简单的字符串。显然,在真正的应用程序中,我需要使用更复杂的模板,在视图中填充的信息是可调用的 例如: # templates/mail/notification.jinja2 Dear {{username}},<br> blah blah blah... 它可以工作,但看起来有点凌乱 因此,这里的问题是:这是正确的方法吗?请参阅以下代码: fro

我使用
pyramid\u mailer
软件包在我的pyramid应用程序中发送电子邮件

我找到了一些示例,其中作为电子邮件的主体仅使用简单的字符串。显然,在真正的应用程序中,我需要使用更复杂的模板,在视图中填充的信息是可调用的

例如:

# templates/mail/notification.jinja2
Dear {{username}},<br> blah blah blah... 
它可以工作,但看起来有点凌乱


因此,这里的问题是:这是正确的方法吗?

请参阅以下代码:

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)

谢谢我试试看。出于某种原因,我认为它会创建响应(带有标题,我不需要标题)。我会再次检查,并在完成后尽快接受答案。发送电子邮件不应该是查看(您现在有@view_config),而是从您希望发送电子邮件的视图调用的功能。通常在处理HTTP POST之后。