使 Django 内置 send_mail 函数默认使用 html

发布于 2024-10-12 12:29:50 字数 504 浏览 2 评论 0原文

我想用我自己的智能 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 技术交流群。

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

发布评论

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

评论(6

看海 2024-10-19 12:29:50

来自 Django 文档:http://docs.djangoproject。 com/en/dev/topics/email/#sending-alternative-content-types

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', '[email protected]', '[email protected]'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

不需要 send_mail() 。

From Django docs: http://docs.djangoproject.com/en/dev/topics/email/#sending-alternative-content-types

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', '[email protected]', '[email protected]'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

No send_mail() required.

痴情换悲伤 2024-10-19 12:29:50

听起来很多人都误读了你的问题。

要覆盖从第三方模块的导入,覆盖 sys.modules['django.core.mail'] 可能有效。

它可以在简单的测试中工作,但还没有经过彻底的测试。

import sys
from django.core import mail 

def my_send_mail(*args, **kwargs):
    print "Sent!11"

mail.send_mail = my_send_mail
sys.modules['django.core.mail'] = mail


from django.core.mail import send_mail
send_mail('blah')
# output: Sent!11

例如,如果我将该代码放入 settings.py 中并启动 manage.py shell 并导入 send_mail,我就会得到我的版本。

我的 shell 会话

In [1]: from django.core.mail import send_mail
In [2]: send_mail()
Sent!11

免责声明
我从来没有做过这样的事情,也不知道是否会产生任何奇怪的后果。

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.

import sys
from django.core import mail 

def my_send_mail(*args, **kwargs):
    print "Sent!11"

mail.send_mail = my_send_mail
sys.modules['django.core.mail'] = mail


from django.core.mail import send_mail
send_mail('blah')
# output: Sent!11

For example, if I put that code in my settings.py and launch the manage.py shell and import send_mail, I get my version.

My shell session

In [1]: from django.core.mail import send_mail
In [2]: send_mail()
Sent!11

Disclaimer
I've never done anything like this and don't know if there are any funky ramifications.

挽清梦 2024-10-19 12:29:50

我编写了一个应用程序,用于将所有项目的应用程序从纯文本电子邮件快速切换到基于 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

睫毛上残留的泪 2024-10-19 12:29:50

稍微改变一下 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.

我早已燃尽 2024-10-19 12:29:50

在 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 to send_mail() function to send html message.

https://docs.djangoproject.com/en/1.8/topics/email/#send-mail

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