Daniel Flippance Daniel Flippance - 3 months ago 22
HTML Question

Sendgrid Always Sends Text Email

I have an HTML sendgrid template and am sending it via Node.js using the npm package 'sendgrid'. Problem is that I always receive the email as text format not HTML even though the template has HTML.

Code:

var email = new sendgrid.Email({
to : 'me@here.com',
from : 'you@there.com',
subject : 'Saying Hi with HTML Template',
text : 'Body' //This is required
});

email.addFilter('templates', 'enable', 1);
email.addFilter('templates', 'template_id', '12131331.....');
email.addSubstitution('{{TOKEN1}}', 'value');

sendgrid.send(email, function(err, json) {
if (err) { console.error(err); }
console.log(json);
});


Template

<html>
<head><title></title></head>
<body>
<h1>This is a test</h1>
<p>{{TOKEN1}}</p>
<p><a href="http://www.there.com">There</a></p>

<div>&lt;%body%&gt;</div>
</body>
</html>


Is there a code parameter I'm supposed to set? Or a setting on the template itself to allow HTML?

Answer

According to the documentation. You only use the text property if you're sending plain-text. Instead, use the html property to create an HTML message. You can use the built in setHtml method like the following:

var email     = new sendgrid.Email(); 
email.setHtml('<h1>Some html</h1>');
sendgrid.send(email, function(err, json) { });