Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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      : '[email protected]',
    from    : '[email protected]',
    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?

like image 561
Daniel Flippance Avatar asked Nov 25 '15 00:11

Daniel Flippance


1 Answers

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) { });
like image 71
JD Davis Avatar answered Nov 15 '22 08:11

JD Davis