How to include doctype declaration in email template using Meteor's core email package?

Greeting Meteorites!

According to most the best way to create an html template is with an xhtml transitional doctype. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
(campaignmonitor for example uses that notation for all it’s email templates) For example here’s an article https://www.campaignmonitor.com/blog/email-marketing/2010/11/correct-doctype-to-use-in-html-email/

Only problem is: when using Meteor’s core email package it doesn’t allow me to send an email with a doctype declaration. To be clear - it does work without the doctype declaration - I tried it!

Here is my setup:

  1. I am storing all my email templates in a top-level private directory.
  2. I am calling the email.send method server side using the ssr:meteorhacks package to render templates with passed data.
    Here is my server side code if you are interested:

Meteor.methods({
'sendEmail':function(){
SSR.compileTemplate('htmlEmail' , Assets.getText('email/trial_ending.html'));
var emailData = {
title: "Trial Ending Soon.",
addCardUrl : "CARDURL",
year : moment().year()
};

Email.send({
to: "anpolchert@gmail.com",
from: "Us<us@example.com>",
subject: "Trial is ending.",
text: "The contents of our email in plain text.",
html: SSR.render('htmlEmail', emailData)
});
}
})

The error I receive is Error: Unexpected Doctype For whatever reason the email package doesn’t seem to recognize any doctype declaration! Anyone know what’s going on here?

Thank you and look forward to a response.
Alex

I fixed this with this additional method for SSR:

SSR.renderEmail = function (...args) {
	const html = SSR.render(...args);
	return `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
${html}
</html>`;
};

And removing of the same strings from template .html files.
Imo Blaze caused the problem, because it forbid to use doctype block. Only and allowed.
PS: Also you can cache compiled template outside of the method itself to compile email once. And then only fill it with data.

4 Likes