Ontokrat
Ontokrat

Reputation: 189

Server Side Rendering HTML text (write with HTML tags in DB) as effectively html in an email

On one hand

I've a field will in a collection, where user can save html text (through a WYSIWYG editor): it works fine and users can write/save some strings like this it's<strong>bold</strong> and sometimes <i>italic</i>. Nothing crazy..

On a second hand

When a user send this field will by email (as part of emailData), rendered with meteorhacks:ssr in an html template, the field show up as it's<strong>bold</strong> and sometimes <i>italic</i>. with HTML tags as normal text.

So, anyone know the trick to render in the html email's body: 'it's bold and sometimes italic.'? Thanks.


My code is very complicated, lot of const and succession of functions, but except of html rendering, it works fine and is finally structured like this:

SSR.compileTemplate('htmlEmail', Assets.getText('replycontact.html'));

var emailData = {
  message: `${message}`,
  will: `${will}`,
  origincontactdate: `${origincontactdate}`,
  contactname: `${contactname}`,
};
        
//send the mail
Email.send({
  to: to,
  from: from,
  subject: subject,
  html: SSR.render('htmlEmail', emailData),
});

Upvotes: 0

Views: 594

Answers (2)

Ontokrat
Ontokrat

Reputation: 189

The problem was that Meteor was escaping the HTML string.

Therefore, the solution is to use 3 brackets {{{ }}} instead of 2 - here for the emailData in the html template : instead of {{will}}, use {{{will}}}.

The code above in the question remain as it.

source: https://stackoverflow.com/a/16565529/7281870

Upvotes: 1

Mikkel
Mikkel

Reputation: 7777

Your data is most likely being saved as HTML encoded in the database, eg

<b>

is saved as

&lt;b&gt;

If so, you simply need to unencode it using the Javascript decodeURI() function, which you can read more about here:

http://www.w3schools.com/jsref/jsref_decodeuri.asp

So your code will look like this:

var emailData = {
  message: `${message}`,
  will: decodeURI(${will}),
  origincontactdate: `${origincontactdate}`,
  contactname: `${contactname}`,
};

//send the mail
Email.send({
  to: to,
  from: from,
  subject: subject,
  html: SSR.render('htmlEmail', emailData),
});

Upvotes: 0

Related Questions