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

PreMailer to inline css styles #69

Open syska opened 11 years ago

syska commented 11 years ago

Hi,

On an other project we just used http://nuget.org/packages/PreMailer.Net/1.1.2 to automatically inline style sheets so the chance is its looks the same in all email clients is better.

I did not however see any options for doing this in MvcMailer or am I just missing this? ( get the output from the Html ) ...

I guess one could just have a seperate Controller action use that view and render it ... and return the Html. Just wanted to make sure I was not missing anything. I don't see any options for this in the https://github.com/smsohan/MvcMailer/wiki

For now I love MvcMailer ... keep up the good work.

bchristie commented 11 years ago

Something like the following should work from inside your mailer:

public override MvcMailMessage Populate(Action<MvcMailMessage> action)
{
    MvcMailMessage mailMessage = base.Populate(action);
    if (mailMessage.IsBodyHtml)
    {
        mailMessage.Body = new PreMailer.Net.PreMailer().MoveCssInline(mailMessage.Body, true).Html;
    }
    return mailMessage;
}

Obviously, if you find you have a lot of mailers, make your own base class and have all mailers inherit from that.

Disclaimer: I haven't tested; I'm just going by what I can see on the PreMailer project docs.

syska commented 11 years ago

I will try that. Havent thought of it .... stupid me :-)

luiseduardobraschi commented 9 years ago

I've tried this approach, but the "mailMessage.Body" is always returning empty.

Best regards.