Question:
Bad email format HTML
**[Which editor or feature is your
WIX ÉDITOR
What are you trying to achieve:
Hello Everybody,
I set up an automated email sending system, via Sendgrid, which works perfectly.
Everything is OK, and I have a field for the body of the email, which is called emailContent and it is formatted, in HTML format, with bold characters and underlines.
The format of the field for sending an email is fine and all ok on my site, but when I receive the email, I receive the email in HTML format, with the tags that appear.
the email is therefore incomprehensible. Is there a solution so that I receive the email in the same format as my field entered on my site.
Thank you very much
Hello code_Wizard
Thank you for your answer.
But :
1/ To fix this, make sure the emailContent
is included in the email as an HTML body, not as plain text. ==> *How? A parameter of my field?
2/ When setting up the SendGrid email request, specify the content
type as "text/html"
and pass your emailContent
field. ==> In the backend function or click event of the sendbutton?
The SendEmail function below :
import { Permissions, webMethod } from “wix-web-module”;
import sgMail from “@sendgrid/mail”;
import wixSecretsBackend from “wix-secrets-backend”;
export const sendEmail = webMethod(
Permissions.Anyone,
async (recipient, subject, body) => {
const sendGridSecret = JSON.parse(
await wixSecretsBackend.getSecret(“sendGridSecret”),
);
const key = sendGridSecret.key;
const senderEmail = sendGridSecret.senderEmail;
sgMail.setApiKey(key);
const msg = {
from: senderEmail,
to: recipient,
subject: subject,
text: body,
};
try {
return await sgMail.send(msg);
} catch (error) {
console.error("Error sending the email: " + error.message);
return false;
}
},
);
And The click event on sendButton:
$w(“#sendButton”).onClick(async () => {
const passedValidations = checkFormFields();
if (passedValidations) {
const emailResult = await sendEmail(
$w("#toEmail").value,
$w("#subject").value,
$w("#emailContent").value,
);
if (emailResult[0].statusCode === SUCCESS_CODE) {
clearFields();
displayMessage("L'E-mail du changement de statut de commande a bien été envoyé au client.");
$w('#dropdown1').collapse()
$w('#sendButton').collapse()
} else {
displayMessage(
"Erreur durant l'envoi du mail: merci de vérifier les paramètres de votre compte SendGrid...",
);
}
} else {
displayMessage("Erreur, merci de vérifier les données saisies.");
}
});
function checkFormFields() {
return (
$w(“#toEmail”).validity.valid &&
$w(“#subject”).validity.valid &&
$w(“#emailContent”).validity.valid
);
}
function clearFields() {
$w(“#toEmail”).value = “”;
$w(“#subject”).value = “”;
$w(“#emailContent”).value = “”;
$w(‘#emailContent’).value
$w(“#toEmail”).resetValidityIndication();
$w(“#subject”).resetValidityIndication();
$w(“#emailContent”).resetValidityIndication();
}
here my field emailContent :(i think it’s already in HTML format?)
Everything is Ok. I found my error thanks to you :
just change:
text: body,
to:
html: body,