简体   繁体   English

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

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

I need know that if a email is sent correctly for to do several operations but the function always return True. 我需要知道,如果正确发送了电子邮件以执行多项操作,但该功能始终返回True。

Any idea? 任何想法?

Thanks you. 谢谢。

There is no way to check that a mail has actually been received. 无法检查是否已实际收到邮件。 This is not because of a failing in Django, but a consequence of the way email works. 这不是因为Django失败,而是电子邮件工作方式的结果。

If you need some form of definite delivery confirmation, you need to use something other than email. 如果您需要某种形式的确定交付确认,则需要使用电子邮件以外的其他方式。

When running unit tests emails are stored as EmailMessage objects in a list at django.core.mail.outbox you can then perform any checks you want in your test class. 当运行单元测试的电子邮件存储为EmailMessage在对象列表中django.core.mail.outbox然后你可以在您的测试类执行任何你想要的检查。 Below is an example from django's docs . 以下是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')

Alternatively if you just want to visually check the contents of the email during development you can set the EMAIL_BACKEND to: 另外,如果您只想在开发过程中目视检查电子邮件的内容,则可以将EMAIL_BACKEND设置为:

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

and then look at you console. 然后看看您的控制台。

or 要么

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

then check the file. 然后检查文件。

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

In case of error, send_mail should raise an exception. 如果发生错误, send_mail应该引发异常。 The fail_silently argument makes possible to ignore the error. fail_silently参数可以忽略该错误。 Did you enable this option by mistake? 您是否错误地启用了此选项?

I hope it helps 希望对您有所帮助

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

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