12

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 ...
puntapret
  • 201
  • 4
  • 10

2 Answers2

16

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
}
Andrew Davey
  • 5,441
  • 3
  • 43
  • 57
  • I can't get Postal to resolve the path to the _Layout.cshtml. Can you provide an example of that? – Andrew Oct 14 '15 at 20:27
0

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>
flarex
  • 1
  • 2