eduardokum / laravel-mail-auto-embed

MIT License
166 stars 36 forks source link

Handle images inside <!--[if mso]> blocks #28

Closed JorisDebonnet closed 6 months ago

JorisDebonnet commented 4 years ago

This awesome module skips images inside <!--[if mso]> blocks because during html parsing, all comments are removed. Could be fixed (perhaps with configuration) by replacinging the tags before and after handling?

JorisDebonnet commented 4 years ago

In my local installation, I handled it by extending \Eduardokum\LaravelMailAutoEmbed\Listeners\SwiftEmbedImages and overriding the beforeSendPerformed method:

public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
{
    $message = $evt->getMessage();

    // Uncomment mso tags.
    $message->setBody(
        str_replace(
            ['<!--[if mso]>', '<!--<![endif]-->', '<![endif]-->'],
            ['|!--[if mso]|', '|!--|![endif]--|', '|![endif]--|'],
            $message->getBody()
        )
    );

    parent::beforeSendPerformed($evt);

    // Recomment mso tags.
    $message->setBody(
        str_replace(
            ['|!--[if mso]|', '|!--|![endif]--|', '|![endif]--|'],
            ['<!--[if mso]>', '<!--<![endif]-->', '<![endif]-->'],
            $message->getBody()
        )
    );
}

It could be made even more advanced by supporting version targetting tags ("if mso 9" etc), ... but I think this goes a long way already. It supports "if Outlook" and "if not Outlook" tags.

I did notice that Laravel's Email Markdown parser also does not convert css class into inline styles when they're inside "if mso" tags... so I guess it's up to you whether to support something like that in this library.