Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ...
like image 489
puntapret Avatar asked Apr 18 '14 13:04

puntapret


People also ask

Which MVC view engines work with postal?

Postal can work with any ASP.NET MVC view engine. Postal uses SmtpClient, which can be configured in Web.config. Learn more… Not everyone likes using dynamic objects. Postal lets you strongly type your email data if you need to. Learn more… Verify that your code would send emails, without actually sending them. Learn more…

How do I create an email in ASP NET postal?

Install Postal into your ASP.NET web application using Nuget. Use Postal in your controller action method. Create an Email, passing it the name of the view to use for the email.

Why use postal instead of dynamic objects?

Learn more… Not everyone likes using dynamic objects. Postal lets you strongly type your email data if you need to. Learn more… Verify that your code would send emails, without actually sending them.

How to add a view to a header in WordPress?

Whatever look you want to give to your header do it here. Now again right-click on the Shared Folder and select Add-> View. Image 6. Image 7. Now add a View by right-clicking on the Action Method Name and select your Layout or Master Page like the following.


1 Answers

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
}
like image 148
Andrew Davey Avatar answered Oct 13 '22 08:10

Andrew Davey