Search code examples
asp.net-mvcpostal

Using Postal MVC with Layout parse Headers as mail body


It seems when I use Postal to send an email using Layout, the headers was not parsed and included on the mail message.

Views/Emails/_ViewStart.cshtml

@{ Layout = "~/Views/Emails/_EmailLayout.cshtml"; }

Views/Emails/_EmailLayout.cshtml

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ViewEmails</title>
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>

Views/Emails/ResetPassword.cshtml

To:  @ViewBag.To
From: @ViewBag.From
Subject: Reset Password
Views: Html

Views/Emails/ResetPassword.html.cshtml

Content-Type: text/html; charset=utf-8

Here is your link, etc ...

When i received the mail all the headers To, From, Subject and Views are included in the body.

Anyone know how to do it correctly ?

UPDATED (Thanks to Andrew), this works :

Views/Emails/_EmailLayout.cshtml

@RenderSection("Headers", false)
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ViewEmails</title>
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>

Views/Emails/ResetPassword.cshtml

@section Headers {
    To:  @ViewBag.To
    From: @ViewBag.From
    Subject: Reset Password
    Views: Html
}

Views/Emails/ResetPassword.html.cshtml

@section Headers {
    Content-Type: text/html; charset=utf-8
}

Here is your link, etc ...

Solution

  • One option is to use a Razor section.

    At the top of the layout add:

    @RenderSection("Headers")
    

    Then in the view add:

    @section Headers {
        To:  @ViewBag.To
        From: @ViewBag.From
        Subject: Reset Password
        Views: Html
    }