繁体   English   中英

我如何知道Django / Python是否正确发送了电子邮件?

[英]How can I know if a email is sent correctly with Django/Python?

我需要知道,如果正确发送了电子邮件以执行多项操作,但该功能始终返回True。

任何想法?

谢谢。

无法检查是否已实际收到邮件。 这不是因为Django失败,而是电子邮件工作方式的结果。

如果您需要某种形式的确定交付确认,则需要使用电子邮件以外的其他方式。

当运行单元测试的电子邮件存储为EmailMessage在对象列表中django.core.mail.outbox然后你可以在您的测试类执行任何你想要的检查。 以下是django docs的示例。

from django.core import mail
from django.test import TestCase

class EmailTest(TestCase):
    def test_send_email(self):
        # Send message.
        mail.send_mail('Subject here', 'Here is the message.',
            'from@example.com', ['to@example.com'],
            fail_silently=False)

        # Test that one message has been sent.
        self.assertEqual(len(mail.outbox), 1)

        # Verify that the subject of the first message is correct.
        self.assertEqual(mail.outbox[0].subject, 'Subject here')

另外,如果您只想在开发过程中目视检查电子邮件的内容,则可以将EMAIL_BACKEND设置为:

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

然后看看您的控制台。

要么

EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location

然后检查文件。

这篇有关将Django与Postmark集成的文章描述了如何继续进行电子邮件传递。

如果发生错误, send_mail应该引发异常。 fail_silently参数可以忽略该错误。 您是否错误地启用了此选项?

希望对您有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM