Reputation: 628
I need know that if a email is sent correctly for to do several operations but the function always return True.
Any idea?
Thanks you.
Upvotes: 4
Views: 10668
Reputation: 20150
You can analyse the message after sending
from django.core import mail
message = mail.EmailMultiAlternatives(
'this is subject',
'content for kazi',
'[email protected]',
['[email protected]'])
message.attach_alternative('<p>content for kazi</p>', "text/html")
message.send()
Analyse the message object:
self.assertEqual(message.from_email,'[email protected]')
self.assertEqual(message.to[0],'[email protected]')
self.assertEqual(message.subject,'this is subject')
self.assertTrue('kazi' in message.body)
Upvotes: 0
Reputation: 9463
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. Below is an example from django's 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.',
'[email protected]', ['[email protected]'],
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 = '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.
Upvotes: 9
Reputation: 8934
This post on integrating Django with Postmark describes how you can follow-up on e-mail delivery.
Upvotes: 3
Reputation: 600059
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.
If you need some form of definite delivery confirmation, you need to use something other than email.
Upvotes: 11