【问题标题】:Creating email templates with Django使用 Django 创建电子邮件模板
【发布时间】:2011-02-18 01:38:18
【问题描述】:

我想发送 HTML 电子邮件,使用这样的 Django 模板:

<html>
<body>
hello <strong>{{username}}</strong>
your account activated.
<img src="mysite.com/logo.gif" />
</body>

我找不到任何关于 send_mail 的信息,django-mailer 只发送 HTML 模板,没有动态数据。

如何使用 Django 的模板引擎生成电子邮件?

【问题讨论】:

标签: django email django-email django-mailer


【解决方案1】:

来自the docs,要发送 HTML 电子邮件,您想使用其他内容类型,如下所示:

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

您可能需要两个电子邮件模板 - 一个看起来像这样的纯文本模板,存储在模板目录中的 email.txt 下:

Hello {{ username }} - your account is activated.

还有一个 HTMLy,存储在email.html:

Hello <strong>{{ username }}</strong> - your account is activated.

然后您可以使用get_template 使用这两个模板发送电子邮件,如下所示:

from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from django.template import Context

plaintext = get_template('email.txt')
htmly     = get_template('email.html')

d = Context({ 'username': username })

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = plaintext.render(d)
html_content = htmly.render(d)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

【讨论】:

  • 我认为您可以使用render_to_string 简化此操作,这将使您失去将模板分配给plaintexthtmly 的单独行,而只需在定义text_content 时设置模板和上下文和html_content
  • @cms_mgr 你能详细说明你想说什么以及我们如何使用它
  • 感谢您的帮助。如今,上下文应该只是一个字典。所以代替 d = Context({...}),d = {...} 是正确的方法;)
  • @Hafnernuss 答案适用于 2021 年的 Django 3.1.5 和 python 3.8.5。无需from django.template import Context。只需d = { 'username': username }
【解决方案2】:

由于 Django 的 1.7 在 send_email 方法中添加了 html_message 参数。

html_message:如果提供了 html_message,则生成的电子邮件将是 以消息为文本/纯内容的多部分/替代电子邮件 type 和 html_message 作为 text/html 内容类型。

所以你可以:

from django.core.mail import send_mail
from django.template.loader import render_to_string


msg_plain = render_to_string('templates/email.txt', {'some_params': some_params})
msg_html = render_to_string('templates/email.html', {'some_params': some_params})

send_mail(
    'email title',
    msg_plain,
    'some@sender.com',
    ['some@receiver.com'],
    html_message=msg_html,
)

【讨论】:

  • 请注意,如果 'email.txt' 和 'email.html' 位于设置中定义的目录模板中,而不仅仅是 render_to_string('email.txt', {'some_params': some_params}_
  • 很好的解决方案!但是,使用send_mail 无法设置一些自定义标头,例如Return-Path,可以使用EmailMultiAlternatives's constructor header parameter 进行设置
  • 'templates' 需要从文件名中删除(至少在 python 3.8 中),否则会产生django.template.exceptions.TemplateDoesNotExist 错误。默认情况下,路径是相对于模板文件夹的
【解决方案3】:

受此解决方案的启发,我制作了django-templated-email 来解决这个问题(并且需要在某些时候从使用 django 模板切换到使用 mailchimp 等用于交易模板化电子邮件的模板集对于我自己的项目)。虽然它仍在进行中,但对于上面的示例,您可以:

from templated_email import send_templated_mail
send_templated_mail(
        'email',
        'from@example.com',
        ['to@example.com'],
        { 'username':username }
    )

在 settings.py 中添加以下内容(以完成示例):

TEMPLATED_EMAIL_DJANGO_SUBJECTS = {'email':'hello',}

这将自动在普通的 django 模板目录/加载器中分别为普通和 html 部分查找名为“templated_email/email.txt”和“templated_email/email.html”的模板(如果找不到至少一个其中)。

【讨论】:

  • 我觉得不错。我已经把它剪掉了,并把它扔进了一张票里添加django.shortcuts.send_templated_mailcode.djangoproject.com/ticket/17193
  • 酷,很高兴看到它被提议作为 django 核心的工具。我对 lib 的用例/重点不仅仅是快捷方式(在具有用于邮件发送的键/值 api 的邮件提供商之间轻松切换),但它确实感觉像是核心缺少的功能
【解决方案4】:

我知道这是一个老问题,但我也知道有些人和我一样,总是在寻找最新的答案,因为如果不更新旧答案,有时可能包含已弃用的信息。

现在是 2020 年 1 月,我正在使用 Django 2.2.6 和 Python 3.7

注意:我使用DJANGO REST FRAMEWORK,下面用于发送电子邮件的代码在我的views.pymodel viewset

所以在阅读了多个不错的答案之后,这就是我所做的。

from django.template.loader import render_to_string
from django.core.mail import EmailMultiAlternatives

def send_receipt_to_email(self, request):

    emailSubject = "Subject"
    emailOfSender = "email@domain.com"
    emailOfRecipient = 'xyz@domain.com'

    context = ({"name": "Gilbert"}) #Note I used a normal tuple instead of  Context({"username": "Gilbert"}) because Context is deprecated. When I used Context, I got an error > TypeError: context must be a dict rather than Context

    text_content = render_to_string('receipt_email.txt', context, request=request)
    html_content = render_to_string('receipt_email.html', context, request=request)

    try:
        #I used EmailMultiAlternatives because I wanted to send both text and html
        emailMessage = EmailMultiAlternatives(subject=emailSubject, body=text_content, from_email=emailOfSender, to=[emailOfRecipient,], reply_to=[emailOfSender,])
        emailMessage.attach_alternative(html_content, "text/html")
        emailMessage.send(fail_silently=False)

    except SMTPException as e:
        print('There was an error sending an email: ', e) 
        error = {'message': ",".join(e.args) if len(e.args) > 0 else 'Unknown Error'}
        raise serializers.ValidationError(error)

重要! 那么render_to_string 是如何得到receipt_email.txtreceipt_email.html 的呢? 在我的settings.py 中,我有TEMPLATES,下面是它的外观

关注DIRS,有这行os.path.join(BASE_DIR, 'templates', 'email_templates') .这条线使我的模板可以访问。在我的 project_dir 中,我有一个名为 templates 的文件夹和一个名为 email_templates 的子目录,例如 project_dir-&gt;templates-&gt;email_templates。我的模板receipt_email.txtreceipt_email.htmlemail_templates 子目录下。

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'templates', 'email_templates')],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]

让我补充一下,我的recept_email.txt 看起来像这样;

Dear {{name}},
Here is the text version of the email from template

而且,我的receipt_email.html 看起来像这样;

Dear {{name}},
<h1>Now here is the html version of the email from the template</h1>

【讨论】:

    【解决方案5】:

    使用 EmailMultiAlternatives 和 render_to_string 来使用两个替代模板(一个是纯文本模板,一个是 html 模板):

    from django.core.mail import EmailMultiAlternatives
    from django.template import Context
    from django.template.loader import render_to_string
    
    c = Context({'username': username})    
    text_content = render_to_string('mail/email.txt', c)
    html_content = render_to_string('mail/email.html', c)
    
    email = EmailMultiAlternatives('Subject', text_content)
    email.attach_alternative(html_content, "text/html")
    email.to = ['to@example.com']
    email.send()
    

    【讨论】:

      【解决方案6】:

      我创建了Django Simple Mail,以便为您要发送的每封交易电子邮件提供一个简单、可自定义且可重复使用的模板。

      电子邮件内容和模板可以直接从 django 的管理员中编辑。

      使用您的示例,您将注册您的电子邮件:

      from simple_mail.mailer import BaseSimpleMail, simple_mailer
      
      
      class WelcomeMail(BaseSimpleMail):
          email_key = 'welcome'
      
          def set_context(self, user_id, welcome_link):
              user = User.objects.get(id=user_id)
              return {
                  'user': user,
                  'welcome_link': welcome_link
              }
      
      
      simple_mailer.register(WelcomeMail)
      

      并以这种方式发送:

      welcome_mail = WelcomeMail()
      welcome_mail.set_context(user_id, welcome_link)
      welcome_mail.send(to, from_email=None, bcc=[], connection=None, attachments=[],
                         headers={}, cc=[], reply_to=[], fail_silently=False)
      

      我很想得到任何反馈。

      【讨论】:

      • 如果你在你的仓库上传一个你的包的演示应用程序会很有帮助。
      • 嗨@ans2human 感谢这个建议,我将它添加到改进列表中!
      【解决方案7】:

      示例中有错误....如果按照写的方式使用,会出现如下错误:

      : 'dict' 对象没有属性 'render_context'

      您需要添加以下导入:

      from django.template import Context
      

      并将字典更改为:

      d = Context({ 'username': username })
      

      http://docs.djangoproject.com/en/1.2/ref/templates/api/#rendering-a-context

      【讨论】:

        【解决方案8】:

        Django Mail Templated 是一个功能丰富的 Django 应用程序,用于使用 Django 模板系统发送电子邮件。

        安装:

        pip install django-mail-templated
        

        配置:

        INSTALLED_APPS = (
            ...
            'mail_templated'
        )
        

        模板:

        {% block subject %}
        Hello {{ user.name }}
        {% endblock %}
        
        {% block body %}
        {{ user.name }}, this is the plain text part.
        {% endblock %}
        

        Python:

        from mail_templated import send_mail
        send_mail('email/hello.tpl', {'user': user}, from_email, [user.email])
        

        更多信息:https://github.com/artemrizhov/django-mail-templated

        【讨论】:

        • 您好,如何将所有收件人设置为密件抄送?
        • @aldesabido 这只是 Django 标准 EmailMessage 类的包装。所以在寻找这些功能时你应该阅读官方文档:docs.djangoproject.com/en/1.10/topics/email 也可以看看类似的问题:stackoverflow.com/questions/3470172/…
        • 更准确地说,标准的 EmailMessage 不是包装的,而是继承的。 IE。这是标准类的扩展:)
        • 可以在模板中包含 JS/CSS 吗?
        • 如果您将内联 js/css 插入 html 正文,不确定它是否适用于大多数电子邮件阅读器,但这是可能的,为什么不呢。
        【解决方案9】:

        send_emai() 对我不起作用,所以我使用了EmailMessage here in django docs

        我已经包含了两个版本的分析器:

        1. 仅限 html 电子邮件版本
        2. 带有纯文本电子邮件和 html 电子邮件版本
        from django.template.loader import render_to_string 
        from django.core.mail import EmailMessage
        
        # import file with html content
        html_version = 'path/to/html_version.html'
        
        html_message = render_to_string(html_version, { 'context': context, })
        
        message = EmailMessage(subject, html_message, from_email, [to_email])
        message.content_subtype = 'html' # this is required because there is no plain text email version
        message.send()
        

        如果您想包含纯文本版本的电子邮件,请像这样修改上面的内容:

        from django.template.loader import render_to_string 
        from django.core.mail import EmailMultiAlternatives # <= EmailMultiAlternatives instead of EmailMessage
        
        plain_version = 'path/to/plain_version.html' # import plain version. No html content
        html_version = 'path/to/html_version.html' # import html version. Has html content
        
        plain_message = render_to_string(plain_version, { 'context': context, })
        html_message = render_to_string(html_version, { 'context': context, })
        
        message = EmailMultiAlternatives(subject, plain_message, from_email, [to_email])
        message.attach_alternative(html_message, "text/html") # attach html version
        message.send()
        

        我的纯文本和 html 版本如下所示: plain_version.html:

        Plain text {{ context }}
        

        html_version.html

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
         <head>
         ...
         </head>
        <body>
        <table align="center" border="0" cellpadding="0" cellspacing="0" width="320" style="border: none; border-collapse: collapse; font-family:  Arial, sans-serif; font-size: 14px; line-height: 1.5;">
        ...
        {{ context }}
        ...
        </table>
        </body>
        </html>
        

        【讨论】:

        • 你的第一个解决方案对我来说就像一个魅力。我不想与txt 文件有任何关系,所以我使用了EmailMessage 类。感谢这个很棒的解决方案伙伴! :-)
        【解决方案10】:

        我喜欢使用此工具来轻松发送电子邮件 HTML 和 TXT,并进行简单的上下文处理:https://github.com/divio/django-emailit

        【讨论】:

          【解决方案11】:

          我写了一个snippet,它允许您发送使用存储在数据库中的模板呈现的电子邮件。一个例子:

          EmailTemplate.send('expense_notification_to_admin', {
              # context object that email template will be rendered with
              'expense': expense_request,
          })
          

          【讨论】:

            【解决方案12】:

            如果您想要邮件的动态电子邮件模板,请将电子邮件内容保存在数据库表中。 这是我在数据库中保存为 HTML 代码的内容 =

            <p>Hello.. {{ first_name }} {{ last_name }}.  <br> This is an <strong>important</strong> {{ message }}
            <br> <b> By Admin.</b>
            
             <p style='color:red'> Good Day </p>
            

            在你看来:

            from django.core.mail import EmailMultiAlternatives
            from django.template.loader import get_template
            
            def dynamic_email(request):
                application_obj = AppDetails.objects.get(id=1)
                subject = 'First Interview Call'
                email = request.user.email
                to_email = application_obj.email
                message = application_obj.message
            
                text_content = 'This is an important message.'
                d = {'first_name': application_obj.first_name,'message':message}
                htmly = FirstInterviewCall.objects.get(id=1).html_content #this is what i have saved previously in database which i have to send as Email template as mentioned above HTML code
            
                open("partner/templates/first_interview.html", "w").close() # this is the path of my file partner is the app, Here i am clearing the file content. If file not found it will create one on given path.
                text_file = open("partner/templates/first_interview.html", "w") # opening my file
                text_file.write(htmly) #putting HTML content in file which i saved in DB
                text_file.close() #file close
            
                htmly = get_template('first_interview.html')
                html_content = htmly.render(d)  
                msg = EmailMultiAlternatives(subject, text_content, email, [to_email])
                msg.attach_alternative(html_content, "text/html")
                msg.send()
            

            这将发送您保存在 Db 中的动态 HTML 模板。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-09-08
              • 1970-01-01
              • 2012-12-26
              • 1970-01-01
              • 1970-01-01
              • 2014-04-18
              相关资源
              最近更新 更多