【问题标题】:Django sending email with templateDjango 使用模板发送电子邮件
【发布时间】:2020-09-08 20:38:45
【问题描述】:

我想使用 Django 发送电子邮件。在邮件正文中,我想发送包含“图像”和 HTML 内容的引导程序“卡片”。

发送邮件成功。但是,它没有在收到的邮件中显示图像。此外,它不是 Bootstrap“卡片”格式。

请有人帮助我。

views.py

​​>
emailSubject = "Mail from XYZ.COM"
emailOfSender = EMAIL_HOST_USER
emailOfRecipient = viewed_bog.profile.user.email

html_content = render_to_string('receipt_email.html', {'item': viewed_bog, }, request=request)

try:        
    emailMessage = EmailMultiAlternatives(subject=emailSubject, body=html_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)

receipt_email.html

这是包含使用 Django 模板语言的 Bootstrap 4“卡片”的 HTML 模板。在卡片中,我有图像。

现在,我可以成功发送邮件了。但是收到的邮件中没有显示图像。而且,邮件正文不是“卡片”格式。

【问题讨论】:

    标签: django image email templates


    【解决方案1】:

    我假设您从 bootstrap.css 文件加载 css,并且由于大多数邮件提供商出于安全原因阻止加载 css 和 js 文件,您应该在要发送的模板中使用内联样式。 最终,当我想将常规 css 编译为内联 css 时,我使用了一个在线工具。 css inline tool

    【讨论】:

    • Wallid,我试过上面提到的“css内联工具”。它没有解决问题。
    • 在收到邮件时,浏览器控制台是否会出现一些错误日志?
    • 是的瓦利德先生,错误是这样来的 1) 内容安全策略:忽略 script-src 中的“'unsafe-inline'”:指定“严格动态” 2) 内容安全策略:忽略“ https:” 在 script-src 中:指定了 'strict-dynamic'。在“receipt_email.html”文件中,我使用基本的引导程序“卡片”并根据 django 模板语言显示图像,{% if item.photo %} {{ item.photo.url }} {% endif %}。在收到的邮件中,它没有显示图像,也不是引导“卡片”格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多