Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Inyección de etiquetas HTML de Nodemailer

Esta es la primera vez que hago una pregunta en StackOverflow.

Estoy usando Nodemailer para enviarme un correo electrónico cuando alguien ha llenado un formulario. Estoy usando ExpressJS para manejar la solicitud POST.

Todo parece estar bien ahora, excepto cuando traté de inyectar etiquetas HTML en el formulario. ¡Podría jugar con el correo html como quisiera!

Este es mi código para el sendMail

 transport.sendMail({ from: `Form Submittion <${myEmailAddress}>`, to: myOtherEmailAddress, subject: "New Form Submitted", text: `Hi, \nNew Form Submitted!\n${req.body.request}`, html: `<h1>Hi, <br />New Form Submitted!</h1> <h2>${req.body.request}</h2>`, });

Cuando req.body.request tiene etiquetas HTML, puede afectar la apariencia de mi correo electrónico, lo cual no me gusta.

¿Hay alguna manera de sanear el req.body.request del nodemailer o express o debo confiar en otras herramientas? Si es así, ¿podría sugerir algunas herramientas para hacer el trabajo?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • instalar multer
  • Analice los campos de la solicitud, utilizando multer
  • Páselo como argumento a una función que devuelve la cadena de plantilla HTML para el correo electrónico.
  • enviar el correo electrónico

 const express = require("express"); // for email const nodemailer = require("nodemailer"); // for handling the form submission const multer = require('multer'); const upload = multer(); const app = express(); // upload.none() for only accepting text fields from form submission // refer : http://expressjs.com/en/resources/middleware/multer.html app.post("/submit", upload.none(), async (req, res) => { // here name is field name at your html form const name = req.body.name; const email = req.body.email; let transporter = nodemailer.createTransport({ host: "your-smtp-server", port: 587, secure: false, // true for 465, false for other ports auth: { user: "your-crendentials", // generated ethereal user pass: "your-crendentials", // generated ethereal password }, }); const info = await transporter.sendMail({ from: '"Sender name" <foo@example.com>', // sender address to: email, subject: "New message from " + name, text: `Hi, \nNew Form Submitted!\n${name}`, html: getHTMLTemplateString(name), }); console.log("Message sent: %s", info.messageId); return res.send({ success: true, message: "Email sent successfully", }) }); function getHTMLTemplateString(name) { // add your css and html inside the string below return ` <h1>Hi ${name}, <br />New Form Submitted!</h1> <p>Thank you for submitting the form!</p> <p>We will get back to you as soon as possible.</p> `; } app.listen(3000, () => { console.log("Server started on port 3000"); });

Dependencias añadidas: multer

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!