puntapret
puntapret

Reputation: 201

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 ...

Upvotes: 12

Views: 3791

Answers (2)

Andrew Davey
Andrew Davey

Reputation: 5451

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
}

Upvotes: 16

flarex
flarex

Reputation: 1

Move the first line

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

from Views/Emails/ResetPassword.html.cshtml to Views/Emails/_EmailLayout.cshtml

Content-Type: text/html; charset=utf-8
<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>ViewEmails</title>
    </head>
    <body>
        <div>
            @RenderBody()
        </div>
    </body>
</html>

Upvotes: 0

Related Questions