[英]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. 然后检查文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.