在所有应用程式中替换django.core.mail.send_mail()? [英] Replace django.core.mail.send_mail() across all apps?

查看:126
本文介绍了在所有应用程式中替换django.core.mail.send_mail()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将整个项目(包括INSTALLED_APPS中的第三方项目)中的 django.core.mail.send_mail()替换为自定义 send_mail()

Is it possible to replace calls to django.core.mail.send_mail() across my entire project (including third-party projects in my INSTALLED_APPS) with a custom send_mail()?

我正在将django-mailer与我的项目集成,它为<$ c提供了一个替换 send_mail() $ C> django.core.mail.send_mail()。由于两者都使用相同的功能签名,文档建议在通常导入Django提供的版本的地方导入django-mailer版本,如下所示:

I'm integrating django-mailer with my project, which provides a replacement send_mail() for django.core.mail.send_mail(). Since both use the same function signature the docs suggest importing the django-mailer version in places where you'd normally import the Django-provided version like so:

# favour django-mailer but fall back to django.core.mail
from django.conf import settings

if "mailer" in settings.INSTALLED_APPS:
    from mailer import send_mail
else:
    from django.core.mail import send_mail

这适用于我自己的应用程序代码,但我也希望对使用 django.core.mail.send_mail()的第三方应用程序进行相同的更改。现在,当这些应用程序尝试发送电子邮件时,我遇到错误。

This works for my own app code, but I'd also like to make the same changes across third-party apps that use django.core.mail.send_mail(). Right now, I hit errors when these apps try to send e-mail.

Django是否提供任何类型的钩子来替换 django.core。 mail.send_mail(),还是有解决方法?现在我正在查看发送电子邮件和添加条件导入代码的每个第三方项目,但这显然不是很理想。

Does Django provide any kind of hook to replace django.core.mail.send_mail(), or is there a workaround? Right now I'm looking at forking each third-party project that sends e-mails and adding the conditional import code above, but that is obviously not ideal.

推荐答案

Django有电子邮件后端这可以让您重写来自 django.core.mail 的电子邮件的发送方式,而不是通过SMTP。有一些可以让你在本地保存,等等; django-mailer 还包括一个(如usage.txt 所述)做你想做的事情。

Django has email backends that let you override the way that emails from django.core.mail are sent, instead of going through SMTP. There are some that let you save them locally and so on; django-mailer also includes one (as described in usage.txt) to do exactly what you want to do.

这篇关于在所有应用程式中替换django.core.mail.send_mail()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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