mirror of
https://github.com/django/django.git
synced 2025-06-01 09:39:12 +00:00
Fixed #35528 -- Added EmailMultiAlternatives.body_contains() helper method.
This commit is contained in:
parent
7a0cd09f9f
commit
5fef6d2445
@ -505,3 +505,16 @@ class EmailMultiAlternatives(EmailMessage):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
def body_contains(self, text):
|
||||||
|
"""
|
||||||
|
Checks that ``text`` occurs in the email body and in all attached MIME
|
||||||
|
type text/* alternatives.
|
||||||
|
"""
|
||||||
|
if text not in self.body:
|
||||||
|
return False
|
||||||
|
|
||||||
|
for content, mimetype in self.alternatives:
|
||||||
|
if mimetype.startswith("text/") and text not in content:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
@ -143,6 +143,10 @@ Email
|
|||||||
<django.core.mail.EmailMultiAlternatives.alternatives>` is now a list of
|
<django.core.mail.EmailMultiAlternatives.alternatives>` is now a list of
|
||||||
named tuples, as opposed to regular tuples.
|
named tuples, as opposed to regular tuples.
|
||||||
|
|
||||||
|
* The new :meth:`~django.core.mail.EmailMultiAlternatives.body_contains` method
|
||||||
|
returns a boolean indicating whether a provided text is contained in the
|
||||||
|
email ``body`` and in all attached MIME type ``text/*`` alternatives.
|
||||||
|
|
||||||
Error Reporting
|
Error Reporting
|
||||||
~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -436,6 +436,26 @@ Django's email library, you can do this using the
|
|||||||
msg.attach_alternative(html_content, "text/html")
|
msg.attach_alternative(html_content, "text/html")
|
||||||
msg.send()
|
msg.send()
|
||||||
|
|
||||||
|
.. method:: body_contains(text)
|
||||||
|
|
||||||
|
.. versionadded:: 5.2
|
||||||
|
|
||||||
|
Returns a boolean indicating whether the provided ``text`` is
|
||||||
|
contained in the email ``body`` and in all attached MIME type
|
||||||
|
``text/*`` alternatives.
|
||||||
|
|
||||||
|
This can be useful when testing emails. For example::
|
||||||
|
|
||||||
|
def test_contains_email_content(self):
|
||||||
|
subject = "Hello World"
|
||||||
|
from_email = "from@example.com"
|
||||||
|
to = "to@example.com"
|
||||||
|
msg = EmailMultiAlternatives(subject, "I am content.", from_email, [to])
|
||||||
|
msg.attach_alternative("<p>I am content.</p>", "text/html")
|
||||||
|
|
||||||
|
self.assertIs(msg.body_contains("I am content"), True)
|
||||||
|
self.assertIs(msg.body_contains("<p>I am content.</p>"), False)
|
||||||
|
|
||||||
Updating the default content type
|
Updating the default content type
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -1163,6 +1163,24 @@ class MailTests(HeadersCheckMixin, SimpleTestCase):
|
|||||||
with self.assertRaisesMessage(ValueError, msg):
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
email_msg.attach_alternative("<p>content</p>", None)
|
email_msg.attach_alternative("<p>content</p>", None)
|
||||||
|
|
||||||
|
def test_body_contains(self):
|
||||||
|
email_msg = EmailMultiAlternatives()
|
||||||
|
email_msg.body = "I am content."
|
||||||
|
self.assertIs(email_msg.body_contains("I am"), True)
|
||||||
|
self.assertIs(email_msg.body_contains("I am content."), True)
|
||||||
|
|
||||||
|
email_msg.attach_alternative("<p>I am different content.</p>", "text/html")
|
||||||
|
self.assertIs(email_msg.body_contains("I am"), True)
|
||||||
|
self.assertIs(email_msg.body_contains("I am content."), False)
|
||||||
|
self.assertIs(email_msg.body_contains("<p>I am different content.</p>"), False)
|
||||||
|
|
||||||
|
def test_body_contains_alternative_non_text(self):
|
||||||
|
email_msg = EmailMultiAlternatives()
|
||||||
|
email_msg.body = "I am content."
|
||||||
|
email_msg.attach_alternative("I am content.", "text/html")
|
||||||
|
email_msg.attach_alternative(b"I am a song.", "audio/mpeg")
|
||||||
|
self.assertIs(email_msg.body_contains("I am content"), True)
|
||||||
|
|
||||||
|
|
||||||
@requires_tz_support
|
@requires_tz_support
|
||||||
class MailTimeZoneTests(SimpleTestCase):
|
class MailTimeZoneTests(SimpleTestCase):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user