utils.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:pyconjp-website 作者: pyconjp 项目源码 文件源码
def send_email_message(template_name, from_, to, context, headers=None):
    """
    Send an email message.

    :param template_name: Use to construct the real template names for the
    subject and body like this: "finaid/email/%(template_name)s/subject.txt"
    and "finaid/email/%(template_name)s/body.txt"
    :param from_: From address to use
    :param to: List of addresses to send to
    :param context: Dictionary with context to use when rendering the
    templates.
    :param headers: dict of optional, additional email headers
    """
    context = Context(context)

    name = "finaid/email/%s/subject.txt" % template_name
    subject_template = get_template(name)
    subject = subject_template.render(context)
    # subjects must be a single line, no newlines
    # if there's a trailing newline, strip it; anything more than that,
    # let it fail (tests will fail) so we know the subject template is
    # not valid.
    subject = subject.rstrip(u"\n")

    name = "finaid/email/%s/body.txt" % template_name
    body_template = get_template(name)
    body = body_template.render(context)

    # Important: By default, the MIME type of the body parameter in an
    # EmailMessage is "text/plain". That makes it safe to use "|safe" in
    # our email templates, and we do. If you change this to, say, send
    # HTML format email, you must go through the email templates and do
    # something better about escaping user data for safety.

    email = EmailMessage(subject, body, from_, to, headers=headers)
    email.send()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号