# Testing EmailMultiAlternatives email

**URL:** <https://forum.djangoproject.com/t/testing-emailmultialternatives-email/13717>\
**Category:** Forms & APIs\
**Created:** [May 15, 2022, 7:40am UTC](https://forum.djangoproject.com/t/testing-emailmultialternatives-email/13717 "2022-05-15T07:40:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcd1000](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pcd1000/32/4499_2.png) [@pcd1000](https://forum.djangoproject.com/u/pcd1000)\
**Post date:** [May 15, 2022, 7:40am UTC](https://forum.djangoproject.com/t/testing-emailmultialternatives-email/13717/1 "2022-05-15T07:40:03Z")

</div>

So, my app renders an email using EmailMultiAlternatives, and I want to test email body. I am using the following script to access the email’s body;

```auto
mail.outbox[0].alternatives[0][0]

```

I am able to print the email using print(mail.outbox[0].alternatives[0][0])

```auto
Here is your code: 7997644198

```

But, when I try to test the body of the email using;

```auto
self.assertContains(mail.outbox[0].alternatives[0][0], escape('Here is your code:'))

```

I get an error, AttributeError: ‘SafeString’ object has no attribute ‘status\_code’

Please, what am I doing wrong?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 15, 2022, 1:23pm UTC](https://forum.djangoproject.com/t/testing-emailmultialternatives-email/13717/2 "2022-05-15T13:23:49Z")

</div>

Keep in mind that when you `print` an object, you’re not necessarily looking at the object. You’re looking at a representation of the object that the [` __str__ `](https://docs.python.org/3/reference/datamodel.html#object. __str__ ) method on that class generates.

If you want to examine that object to understand its structure and contents, you can either use the python debugger or the Django shell. (You can also look at the source code for it to understand how it’s defined and what the ` __str__ ` method is doing.)

You can possibly take the shortcut by setting up your test to test the string formatted version by wrapping that object in the `str` function (e.g. `str(mail.outbox[0].alternatives[0][0])`), however, I’m not entirely sure that that’s going to test what you’re really trying to validate here. That’s something you’re going to need to decide.

---

<div class="post-metadata">

**Author:** ![pcd1000](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pcd1000/32/4499_2.png) [@pcd1000](https://forum.djangoproject.com/u/pcd1000)\
**Post date:** [May 15, 2022, 2:55pm UTC](https://forum.djangoproject.com/t/testing-emailmultialternatives-email/13717/3 "2022-05-15T14:55:10Z")

</div>

Thanks Ken.

Actually, if I had read the error properly, I would have understood that assertContains is asking for a status\_code which is part of a normal response and certainly not part of an email.

Using assertIn worked.

```auto
self.assertIn('Here is your code:', mail.outbox[0].alternatives[0][0])

```
