使 Django 内置 send_mail 函数默认使用 html
我想用我自己的智能 send_mail 函数替换仅适用于纯文本电子邮件的内置 send_mail 函数,该函数会自动生成 html 和纯文本版本。对于我自己的电子邮件(在我自己的应用程序中定义),一切都按预期工作。我可以在views.py中以这种方式执行此操作:
from django.core import mail
from utils import send_mail as send_html_mail
mail.send_mail = send_html_mail
但是问题仍然出现在第三方应用程序的电子邮件中。在我的代码之前,所有导入都已通过 send_mail 函数的猴子修补完成。
是否可以在所有 django 应用程序的所有导入之前覆盖此函数?或者这个问题可能还有另一种解决方案。我真的不想通过发送这些第三方应用程序的电子邮件来修补代码。只需放置 html-template 就很容易了。
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
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Django 1.2 允许可插入电子邮件后端: http://docs.djangoproject.com/en/1.2/topics/email/#defining-a-custom-e-mail-backend
Django 1.2 allows for pluggable email backends: http://docs.djangoproject.com/en/1.2/topics/email/#defining-a-custom-e-mail-backend
来自 Django 文档:http://docs.djangoproject。 com/en/dev/topics/email/#sending-alternative-content-types
不需要 send_mail() 。
From Django docs: http://docs.djangoproject.com/en/dev/topics/email/#sending-alternative-content-types
No send_mail() required.
听起来很多人都误读了你的问题。
要覆盖从第三方模块的导入,覆盖 sys.modules['django.core.mail'] 可能有效。
它可以在简单的测试中工作,但还没有经过彻底的测试。
例如,如果我将该代码放入
settings.py
中并启动manage.py shell
并导入send_mail
,我就会得到我的版本。我的 shell 会话
免责声明
我从来没有做过这样的事情,也不知道是否会产生任何奇怪的后果。
Sounds like a lot of people are misreading your question.
To override the import from third party modules, overwriting
sys.modules['django.core.mail']
might work.It works in simple tests, but it's not thoroughly tested.
For example, if I put that code in my
settings.py
and launch themanage.py shell
and importsend_mail
, I get my version.My shell session
Disclaimer
I've never done anything like this and don't know if there are any funky ramifications.
我编写了一个应用程序,用于将所有项目的应用程序从纯文本电子邮件快速切换到基于 html 的电子邮件。它无需修改任何 Django 代码或其他第三方应用程序的代码即可工作。
在这里查看:https://github.com/ramusus/django-email-html
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.
Check it out here: https://github.com/ramusus/django-email-html
稍微改变一下
django
并用您自己的函数替换原始代码怎么样?实际上,您可以只添加自己的函数,而不删除旧的函数,因为将导入最后一个定义。我不喜欢这样的解决方案,因为我不喜欢修补外部库(您需要记住在升级时再次修补它们),但这会很快解决您的问题。How about changing
django
a little and substituting original code with your own function? You can actually just add your own function and don't remove the old, as the last definition will be imported. I don't like such solutions, because I don't like patching external libraries (you need to remember about patching them again when you upgrade), but this would solve your problem quickly.在 Django 1.7 中,向
send_mail()
函数添加了额外的属性html_message
来发送 html 消息。https://docs.djangoproject.com/en/1.8/topics/电子邮件/#发送邮件
In Django 1.7 added extra attribute
html_message
tosend_mail()
function to send html message.https://docs.djangoproject.com/en/1.8/topics/email/#send-mail