MvcMailer icon indicating copy to clipboard operation
MvcMailer copied to clipboard

Specifying both html and text layouts

Open RobertDM opened this issue 11 years ago • 5 comments

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.

RobertDM avatar Jul 17 '13 20:07 RobertDM

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 [email protected] 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 .

smsohan avatar Jul 17 '13 20:07 smsohan

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

RobertDM avatar Jul 17 '13 20:07 RobertDM

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 [email protected] 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 .

smsohan avatar Jul 17 '13 20:07 smsohan

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?

RobertDM avatar Jul 17 '13 21:07 RobertDM

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 [email protected] 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 .

smsohan avatar Jul 17 '13 21:07 smsohan