测试在没有电子邮件服务器的情况下发送电子邮件
我有一个发送电子邮件的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以将您的应用程序配置为使用控制台后端来发送电子邮件。它将电子邮件写入标准输出而不是发送它们。
更改您的 settings.py 以包含此行:
不要忘记在生产中将其删除。
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:
Don't forget to remove it for production.
Python 有一个内置的小型 SMTP 服务器。您可以使用以下命令在第二个控制台中启动它:
这将简单地在控制台中打印发送到 localhost:1025 的所有邮件。
您必须在
settings.py
中将 Django 配置为使用此服务器:Python has a little SMTP server built-in. You can start it in a second console with this command:
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
:您可以将应用程序配置为将电子邮件写入临时文件而不是发送它们(类似于 Daniel Hepper 的答案)。
这会将每条新消息保存为单独的文件。如果您要发送大量电子邮件并且不想使用回滚,则非常有用。
You can configure your application to write emails out to temporary files instead of sending them (similar to Daniel Hepper's answer).
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.
如果您的测试从 django.test.testcases.TestCase 扩展,则无需执行任何操作。 Django 会将 EmailBackend 替换为“特殊”的。然后您可以测试将发送的内容,如下所示:
发件箱对象是一个特殊的对象,当
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 :
The outbox object is a special object that get injected into mail when
python manage.py test is run.
这详细说明了本杰明的答案。如果我没有安装 postfix、sendmail 或 exim 等本地电子邮件服务器,我测试电子邮件的一种方法是运行 python 电子邮件服务器。您可以使用 sudo 在端口 25 上运行它,或者只使用端口 > 1024(保留端口):
要使用当前的 django 应用程序代码进行测试,您可以临时更改 settings.py 以将其包含在底部:
现在测试您的电子邮件,或者您可以在另一个终端的 ./manage.py shell 中执行此操作像这样的窗口:
并粘贴此代码以发送电子邮件:
无需使用任何真实的电子邮件,因为您将在终端中看到所有内容。您可以将其转储到适当的容器(例如 .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):
For testing with your current django app code, you can change settings.py temporarily to include this at the botom:
Now test out your emails, or you can do this in ./manage.py shell in another terminal window like so:
And paste in this code to send an email:
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.
caktus 有一个很酷的应用程序 https://github.com/caktus/django-email-bandit
只需将其添加到您的 settings.py 文件中:
在您的电子邮件设置之上..所有电子邮件都将被转移到 '[电子邮件受保护]'
快乐编码...
There is a cool app for this by caktus https://github.com/caktus/django-email-bandit
Just add this to your settings.py file:
On top of your email setttings..All emails will be diverted to '[email protected]'
Happy coding...