使Django内置send_mail函数在默认情况下与html一起使用 [英] Make Django build-in send_mail function working with html by default

查看:166
本文介绍了使Django内置send_mail函数在默认情况下与html一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用内置的 send_mail 功能代替只能用于纯文本电子邮件的功能,而要使用我自己的智能的 send_mail 功能,该功能既生成html又生成纯文本.文本版本自动.对于我自己的电子邮件(在我自己的应用程序中定义),一切正常.我可以通过这种方式在views.py中做到这一点:

I want to substitute build-in send_mail function, that works only with plain-text emails, with my own smart send_mail function, that generates both html and plain-text versions automatically. Everything works as expected for my own emails, defined in my own application. I can do it in views.py in this fashion:

from django.core import mail
from utils import send_mail as send_html_mail
mail.send_mail = send_html_mail

但是问题仍然出现在第三方应用程序的电子邮件中.在使用 send_mail 函数进行猴子补丁的代码之前,所有导入都已经完成.

But the problem still appears with emails of third-side applications. There all imports already completed before my code with monkey-patching of send_mail function.

是否可以在所有django应用程序的所有导入之前覆盖此功能?或可能有这个问题的另一种解决方案.我真的不想通过发送这些第三方应用程序的电子邮件来修补代码.只需放置html-template即可.

Is it possible to override this function before all imports of all django applications? Or may be there is another solution of this issue. I really don't want to patch code with sending emails of these third-party applications. It's much easy just to put html-template.

推荐答案

我编写了用于将所有项目应用程序从纯文本电子邮件快速转换为基于html的电子邮件的应用程序.它无需修改任何Django代码或其他第三方应用程序的代码即可工作.

I wrote application for quickly switching from plain-text emails to html based emails for all project's applications. It will work without modifying any Django code or code of other third-part applications.

在此处查看: https://github.com/ramusus/django-email-html

这篇关于使Django内置send_mail函数在默认情况下与html一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆