![](/img/trans.png)
[英]How can I schedule an email to be sent at some point in the future in django?
[英]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
然后檢查文件。
如果發生錯誤, send_mail應該引發異常。 fail_silently參數可以忽略該錯誤。 您是否錯誤地啟用了此選項?
希望對您有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.