emails.py 文件源码

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

项目:registration 作者: HackAssistant 项目源码 文件源码
def render_mail(template_prefix, recipient_email, substitutions,
                from_email=FROM_EMAIL):
    """
    Renders an e-mail to `email`.  `template_prefix` identifies the
    e-mail that is to be sent, e.g. "account/email/email_confirmation"
    """
    substitutions.update(utils.get_substitutions_templates())
    subject = render_to_string('{0}_subject.txt'.format(template_prefix),
                               context=Context(substitutions))
    # remove superfluous line breaks
    subject = " ".join(subject.splitlines()).strip()
    prefix = '[' + settings.HACKATHON_NAME + ']'
    subject = prefix + ' ' + subject
    substitutions.update({'subject': subject})

    bodies = {}
    for ext in ['html', 'txt']:
        try:
            template_name = '{0}_message.{1}'.format(template_prefix, ext)
            bodies[ext] = render_to_string(template_name,
                                           Context(substitutions)).strip()
        except TemplateDoesNotExist:
            if ext == 'txt' and not bodies:
                # We need at least one body
                raise
    if 'txt' in bodies:
        msg = EmailMultiAlternatives(subject,
                                     bodies['txt'],
                                     from_email,
                                     [recipient_email])
        if 'html' in bodies:
            msg.attach_alternative(bodies['html'], 'text/html')
    else:
        msg = EmailMessage(subject,
                           bodies['html'],
                           from_email,
                           [recipient_email])
        msg.content_subtype = 'html'  # Main content is now text/html
    return msg
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号