smsohan / MvcMailer

A Mailer for ASP.Net MVC that forms the Email Body using MVC Views (Razor etc.) following Ruby on Rails ActionMailer style
MIT License
582 stars 178 forks source link

Specifying both html and text layouts #102

Open RobertDM opened 11 years ago

RobertDM commented 11 years ago

I have a multi-national application, so it would be very convenient for me to have a View directory structure as follows:

Views/UserMailer/en/ ..... Views/UserMailer/fr/ ....

With all of the language-specific templates (and Layout templates) in the en, fr, etc. subdirectories. The language used (and hence the directory) is specified at runtime according to the user.

The problem I am having is in specifying the Layouts. I need my emails to have both text and html in them, so that is two different layout files. Unfortunately I can only specify one x.MasterName when populating the messages, so the resulting messages will only have html, and not also plain text. If I leave off the .cshtml extension when specifying MasterName, then I get nothing at all. Specifying Layout in the cshtml message templates apparently is ignored.

Is there any reasonable way for me to specify the use of a subdirectory, including layout pages that will work? Or is this just a design limitation of MvcMailer?

Appreciate any advice.

smsohan commented 11 years ago

MvcMailer will use both the text and html master views if both exists and create multipart emails by default.

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Wed, Jul 17, 2013 at 2:13 PM, RobertDM notifications@github.com wrote:

I have a multi-national application, so it would be very convenient for me to have a View directory structure as follows:

Views/UserMailer/en/ ..... Views/UserMailer/fr/ ....

With all of the language-specific templates (and Layout templates) in the en, fr, etc. subdirectories. The language used (and hence the directory) is specified at runtime according to the user.

The problem I am having is in specifying the Layouts. I need my emails to have both text and html in them, so that is two different layout files. Unfortunately I can only specify one x.MasterName when populating the messages, so the resulting messages will only have html, and not also plain text. If I leave off the .cshtml extension when specifying MasterName, then I get nothing at all. Specifying Layout in the cshtml message templates apparently is ignored.

Is there any reasonable way for me to specify the use of a subdirectory, including layout pages that will work? Or is this just a design limitation of MvcMailer?

Appreciate any advice.

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/102 .

RobertDM commented 11 years ago

It can't create multi-part emails if you specify a custom MasterName, Unless I am missing something obvious?

smsohan commented 11 years ago

It should be fine with a custom MasterName, as long as you have both text and html templates for the given name. You can check the code at: https://github.com/smsohan/MvcMailer/blob/master/Mvc.Mailer/MailerBase.cs

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Wed, Jul 17, 2013 at 2:23 PM, RobertDM notifications@github.com wrote:

It can't create multi-part emails if you specify a custom MasterName, Unless I am missing something obvious?

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/102#issuecomment-21141902 .

RobertDM commented 11 years ago

The problem is that in order to specify a custom MasterName that is not in the default view directory for a controller, it has to be fully specified including the .cshtml extension. e.g. ~/Views/UserMailer/en/_Layout.cshtml. Same with the viewname itself... if it is not in the standard directory you need the .cshtml extension or Razor can't find it.

In MailerBase the code doesn't parse the names, it just appends a ".text" string to the given strings, so you end up with ....cshtml.text which does not exist, so it is ignored.

I see that MailerBase has several functions for separately populating text and html parts. Can I use those to populate a multipart message in more than one call?

smsohan commented 11 years ago

You can override the methods from MailerBase and provide your custom implementation.

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Wed, Jul 17, 2013 at 3:05 PM, RobertDM notifications@github.com wrote:

The problem is that in order to specify a custom MasterName that is not in the default view directory for a controller, it has to be fully specified including the .cshtml extension. e.g. ~/Views/UserMailer/en/_Layout.cshtml. Same with the viewname itself... if it is not in the standard directory you need the .cshtml extension or Razor can't find it.

In MailerBase the code doesn't parse the names, it just appends a ".text" string to the given strings, so you end up with ....cshtml.text which does not exist, so it is ignored.

I see that MailerBase has several functions for separately populating text and html parts. Can I use those to populate a multipart message in more than one call?

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/102#issuecomment-21144884 .