# Mailer get body with layout

**URL:** <https://discourse.cakephp.org/t/mailer-get-body-with-layout/8326>\
**Category:** Need Help\
**Created:** [September 2, 2020, 4:42pm UTC](https://discourse.cakephp.org/t/mailer-get-body-with-layout/8326 "2020-09-02T16:42:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [September 2, 2020, 4:42pm UTC](https://discourse.cakephp.org/t/mailer-get-body-with-layout/8326/1 "2020-09-02T16:42:52Z")

</div>

I need to get back the whole layout of the email template without sending an email, is it possible?

I use this to send an email:

`$this->getMailer('Admin')->send('messageOrder', [$dataPost])`

Thank You

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [September 2, 2020, 4:56pm UTC](https://discourse.cakephp.org/t/mailer-get-body-with-layout/8326/2 "2020-09-02T16:56:25Z")

</div>

Have you looked into the [mail debugging features](https://book.cakephp.org/debugkit/4/en/index.html#using-the-mail-panel) of the DebugKit?

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [September 2, 2020, 5:49pm UTC](https://discourse.cakephp.org/t/mailer-get-body-with-layout/8326/3 "2020-09-02T17:49:03Z")

</div>

No DebugKit, I want me in the variable of the whole layout, including the content from the email message.

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [September 2, 2020, 6:08pm UTC](https://discourse.cakephp.org/t/mailer-get-body-with-layout/8326/4 "2020-09-02T18:08:28Z")

</div>

What have you tried? I’m not experienced with the Mailer system but, looking around the class there are several suggestive bits.

For one, Mailer [seems to use the normal ViewBuilder](https://book.cakephp.org/4/en/core-libraries/email.html#sending-templated-emails), layouts, and templates. This suggests to me that you can render your content outside of the Mailer system if you don’t actually want to email it.

Also, the [Message class mentioned here](https://book.cakephp.org/4/en/core-libraries/email.html#sending-emails-without-using-mailer) has a `getBody()` method. That seems like another avenue for exploration. The example code there suggest you might be able to prepare your message then get it’s body without sending it.

Have you looked into either of these possibilities?

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [September 2, 2020, 6:44pm UTC](https://discourse.cakephp.org/t/mailer-get-body-with-layout/8326/5 "2020-09-02T18:44:35Z")

</div>

Thanks for the information, I looked at it, but unfortunately none of it works for me, or I just can’t do it.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [September 2, 2020, 9:56pm UTC](https://discourse.cakephp.org/t/mailer-get-body-with-layout/8326/6 "2020-09-02T21:56:01Z")

</div>

The `Cake\Mailer\Email` class is the thing that renders the message. Unfortunately, the only call to the `_render` function in there is when you send, and the `_render` function itself is protected, so you can’t access it directly. My suggestion would be to make your own Email class, extending the core version, and just adding a function that calls `_render` and returns the results of that.

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [September 3, 2020, 4:50am UTC](https://discourse.cakephp.org/t/mailer-get-body-with-layout/8326/7 "2020-09-03T04:50:42Z")

</div>

Good idea, thank you!
