测试在没有电子邮件服务器的情况下发送电子邮件

发布于 2024-10-10 19:41:06 字数 117 浏览 7 评论 0原文

我有一个发送电子邮件的 Django 应用程序。生产服务器有电子邮件服务器,但我的本地盒子没有。我希望能够在本地测试电子邮件的发送。有什么方法可以让 django 不通过电子邮件服务器发送它,而只是打印到文件或控制台吗?

I have a Django application that sends an email. The production server has an email server but my local box does not. I would like to be able to test sending of email locally. Is there any way that I can have django not send it through the email server and just print out to a file or console?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

素染倾城色 2024-10-17 19:41:06

您可以将您的应用程序配置为使用控制台后端来发送电子邮件。它将电子邮件写入标准输出而不是发送它们。

更改您的 settings.py 以包含此行:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

不要忘记在生产中将其删除。

You can configure your application to use the Console Backend for sending e-mail. It writes e-mails to standard out instead of sending them.

Change your settings.py to include this line:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

Don't forget to remove it for production.

So尛奶瓶 2024-10-17 19:41:06

Python 有一个内置的小型 SMTP 服务器。您可以使用以下命令在第二个控制台中启动它:

python -m smtpd -n -c DebuggingServer localhost:1025

这将简单地在控制台中打印发送到 localhost:1025 的所有邮件。

您必须在 settings.py 中将 Django 配置为使用此服务器:

EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025

Python has a little SMTP server built-in. You can start it in a second console with this command:

python -m smtpd -n -c DebuggingServer localhost:1025

This will simply print all the mails sent to localhost:1025 in the console.

You have to configure Django to use this server in your settings.py:

EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
薄荷梦 2024-10-17 19:41:06

您可以将应用程序配置为将电子邮件写入临时文件而不是发送它们(类似于 Daniel Hepper 的答案)。

EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = 'tmp/email-messages/'

这会将每条新消息保存为单独的文件。如果您要发送大量电子邮件并且不想使用回滚,则非常有用。

You can configure your application to write emails out to temporary files instead of sending them (similar to Daniel Hepper's answer).

EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = 'tmp/email-messages/'

This saves each new message as a separate file. Useful if you are sending heaps of emails, and don't want to have to use the scrollback.

向地狱狂奔 2024-10-17 19:41:06

如果您的测试从 django.test.testcases.TestCase 扩展,则无需执行任何操作。 Django 会将 EmailBackend 替换为“特殊”的。然后您可以测试将发送的内容,如下所示:

def testMethodThatSendAEmail(self):
    ...
    from django.core import mail
    object.method_that_send_email(to='[email protected]')
    self.assertEqual(len(mail.outbox), 1)
    self.assertEqual(mail.outbox[0].to, ['[email protected]'])
    ...#etc

发件箱对象是一个特殊的对象,当
python manage.py 测试已运行。

If your tests extends from django.test.testcases.TestCase then nothing has to be done. Django will replace the EmailBackend to a "special" one. Then you can test what would had been sent like this :

def testMethodThatSendAEmail(self):
    ...
    from django.core import mail
    object.method_that_send_email(to='[email protected]')
    self.assertEqual(len(mail.outbox), 1)
    self.assertEqual(mail.outbox[0].to, ['[email protected]'])
    ...#etc

The outbox object is a special object that get injected into mail when
python manage.py test is run.

你在我安 2024-10-17 19:41:06

这详细说明了本杰明的答案。如果我没有安装 postfix、sendmail 或 exim 等本地电子邮件服务器,我测试电子邮件的一种方法是运行 python 电子邮件服务器。您可以使用 sudo 在端口 25 上运行它,或者只使用端口 > 1024(保留端口):

python -m smtpd -n -c DebuggingServer localhost:1025
#sudo python -m smtpd -n -c DebuggingServer localhost:25

要使用当前的 django 应用程序代码进行测试,您可以临时更改 settings.py 以将其包含在底部:

EMAIL_HOST, EMAIL_PORT, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD = 'localhost', 1025, None, None

现在测试您的电子邮件,或者您可以在另一个终端的 ./manage.py shell 中执行此操作像这样的窗口:

python manage.py shell

并粘贴此代码以发送电子邮件:

from django.core.mail import send_mail​
send_mail('Subject here', 'Here is the message.', '[email protected]',['[email protected]'], fail_silently=False)

无需使用任何真实的电子邮件,因为您将在终端中看到所有内容。您可以将其转储到适当的容器(例如 .html)以进行进一步测试。

This elaborates on the answer from Benjamin. One way that I test emails if I don't have a local email server like postfix, sendmail or exim installed is to run the python email server. You can run it on port 25 with sudo, or just use a port > 1024 (reserved ports):

python -m smtpd -n -c DebuggingServer localhost:1025
#sudo python -m smtpd -n -c DebuggingServer localhost:25

For testing with your current django app code, you can change settings.py temporarily to include this at the botom:

EMAIL_HOST, EMAIL_PORT, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD = 'localhost', 1025, None, None

Now test out your emails, or you can do this in ./manage.py shell in another terminal window like so:

python manage.py shell

And paste in this code to send an email:

from django.core.mail import send_mail​
send_mail('Subject here', 'Here is the message.', '[email protected]',['[email protected]'], fail_silently=False)

No need to use any real emails since you will see everything in your terminal. You can dump it to the appropriate container like .html for further testing.

梅倚清风 2024-10-17 19:41:06

caktus 有一个很酷的应用程序 https://github.com/caktus/django-email-bandit
只需将其添加到您的 settings.py 文件中:

EMAIL_BACKEND = 'bandit.backends.smtp.HijackSMTPBackend'
BANDIT_EMAIL = '[email protected]'

在您的电子邮件设置之上..所有电子邮件都将被转移到 '[电子邮件受保护]'

快乐编码...

There is a cool app for this by caktus https://github.com/caktus/django-email-bandit
Just add this to your settings.py file:

EMAIL_BACKEND = 'bandit.backends.smtp.HijackSMTPBackend'
BANDIT_EMAIL = '[email protected]'

On top of your email setttings..All emails will be diverted to '[email protected]'

Happy coding...

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