Convert email html body from multipart/alternative to UTF-8
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2023 09:19 AM
Hello experts,
I'm looking for a way to convert email html body to utf-8. (as sender cannot parse it before sending to SN side).
I tried to adjust this piece html code which do working on the web:
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent||tmp.innerText;
}
alert(strip(document.body.innerHTML));
Inbound action code:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2023 09:28 AM
Hi @tpeleg,
Try below script
var finalb = strip(email.body_text);
current.description = finalb;
function strip(html) {
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText;
}
current.insert();
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2023 09:34 AM
Hi @tpeleg
In the strip function, create a new DIV element, tmp, and set its innerHTML to the HTML content you want to process.
Use tmp.innerText first to get the text content, and if it's undefined, fall back to tmp.textContent. This should cover a wider range of cases, as different browsers may behave differently.
In your inbound action code, pass the HTML content of the email body email.body_html to the strip function, and then assign the resulting text to current.description.
function strip(html) {
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.innerText || tmp.textContent;
}
var finalb = strip(email.body_html);
current.description = finalb;
current.insert();
Mark as accepted solution & HIT helpful if it suffices your requirement.
BR, Sohith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2023 11:02 PM
@Anand Kumar P & @Sohithanjan G Thank you!
But unfortantely the code is broken and not working as it should. seems like line:
var tmp = document.createElement("DIV");
is not supported... (can see only before log).
any other suggestion?
Thanks again
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2023 11:12 PM
Hi @tpeleg ,
Try below script like converting html to string and removing special characters
var htmlBody = email.body_text.toString();
var plainText = htmlBody.replace(/<[^>]+>/g, '');
var finalb = strip(plainText);
current.description = finalb;
function strip(html) {
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText;
}
current.insert();Thanks,
Anand
