Django 联系表单发送 html 电子邮件

发布于 2024-10-09 15:09:50 字数 48 浏览 4 评论 0原文

有没有办法使用 django-contact-form 发送 html 电子邮件?

Is there a way to use the django-contact-form to send a html email?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛放△進行李 2024-10-16 15:09:50

我只是查找了源代码,它不是内置的。

您当然可以随意修改它 - 这是一个非常简单的形式并且非常易读。

您需要添加 html_template 变量/路径并修改保存行,并添加一种方法来指定要使用的模板。

def save(self, fail_silently=False):
    """
    Build and send the email message.

    """
    data = self.get_message_dict()
    msg = EmailMultiAlternatives(subject=data['subject'], body=data['message'], from_email=data['from_email'], to=data['recipient_list'])

    msg.attach_alternative(loader.render_to_string('path_to_my_html_template.html', self.get_context()), "text/html")
    msg.send(fail_silently=fail_silently)

这样可以更轻松地将所有更改保存在一个位置。

否则,您可以重写大部分代码并更改 get_message_dict() 中的方法名称和字符串,以反映 EmailMessage 的新字段名称。

坦率地说,我是不确定为什么 send_mail 应该具有与 EmailMessage 不同的关键字

I just looked up the source and it's not built in.

You can certainly modify it however you please -- it's a very simple form and very readable.

You'd need to add a html_template variable/path and modify the save line, and add a way to specify which template to use.

def save(self, fail_silently=False):
    """
    Build and send the email message.

    """
    data = self.get_message_dict()
    msg = EmailMultiAlternatives(subject=data['subject'], body=data['message'], from_email=data['from_email'], to=data['recipient_list'])

    msg.attach_alternative(loader.render_to_string('path_to_my_html_template.html', self.get_context()), "text/html")
    msg.send(fail_silently=fail_silently)

This is just easier to keep all of your changes in one spot.

Otherwise, you could re-write most of the code and change the method names and the strings in get_message_dict() to reflect the new field names for EmailMessage

Frankly, i'm not sure why send_mail should have different keywords than EmailMessage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文