spatie / mailcoach-support

Questions and support for Mailcoach
https://mailcoach.app
31 stars 2 forks source link

PrepareEmailHtmlAction breaks html #159

Closed Gaia-Nutrition closed 4 years ago

Gaia-Nutrition commented 4 years ago

Html content for my campaigns is generated by rendering Mailables.

The ensureEmailHtmlHasSingleRootElement inside the PrepareEmailHtmlAction class wrongly assumes a missing <html> opening tag as the html content starts with <!DOCTYPE .... It then adds another opening <html> tag (but not a closing one as that exists). This results in ConvertHtmlToTextAction to fail due to misplaced DOCTYPE (so no text content is being generated for the campaign).

My current workaround for the ensureEmailHtmlHasSingleRootElement function (you could probably come up with something more robust):

protected function ensureEmailHtmlHasSingleRootElement($campaign)
{
    $emailHtml = preg_replace('/<!DOCTYPE.*?>\s/', '', trim($campaign->email_html));

    if (!Str::startsWith($emailHtml, '<html')) {
        $campaign->email_html = '<html>' . $campaign->email_html;
    }

    if (!Str::endsWith($emailHtml, '</html>')) {
        $campaign->email_html = $campaign->email_html . '</html>';
    }
}
Gaia-Nutrition commented 4 years ago

This could have also been the cause for https://github.com/spatie/mailcoach-support/issues/56

freekmurze commented 4 years ago

This has been fixed in the latest version.

electronick86 commented 4 years ago

Hello, can you tell my why mailcoach remove the doctype tag?

My html html begins with <!doctype html> and the layout works perfectly with the "Test your email first" function.

Once that the campaign is sent, this tag is removed and the final email html contains <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">. At that moment, my template is not responsive at all.

I use Mailcoach 2.15.5.

Is it the expected behavior?

freekmurze commented 4 years ago

Add @electronick86 could you open a new PR for this? Please include the html that you are using.