1

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

4

1 回答 1

2

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

你当然可以随心所欲地修改它——它是一个非常简单的形式并且非常易读。

您需要添加一个 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

于 2010-12-23T20:30:07.670 回答