I am using the SendGrid API to send html emails from my site using the following function:
function sendFormData2() {
const subject = `Test HTML Email`;
const body = "<p>Hello<span style='color: #ff0000;'><strong>World</strong></span></p>";
sendEmail2(subject, body)
.then(response => console.log(response));
}
The email sends successfully but reads as follows:
This is due to the semicolon in the code after #ff0000. In this example I could remove the semicolon but there will be examples where a semicolon is required to separate multiple html styling parameters. When I remove the semicolon, the body of the email looks as follows (the way I want it to):
I thought I could use semicolons freely in a string but it seems to be terminating the line of code. Can anyone advise?
Hi, Iain Downer . This is WAG; does it work if you replace the assignment of the body constant with the following:
const body = '<p>Hello<span style="color: #ff0000;"><strong>World</strong></span></p>';
? (I “inverted” your use of apostrophes and double quotes.)
If that doesn’t work, I’m wondering if JavaScript is considering that the end of the statement and, as a result, the internal semicolon needs to be escaped (i.e., preceded by a backslash) as follows:
const body = '<p>Hello<span style="color: #ff0000\;"><strong>World</strong></span></p>';
or:
const body = "<p>Hello<span style='color: #ff0000\;'><strong>World</strong></span></p>";