I want to send a payment form inside an email using node.js nodemailer.
My emails are successfully sent, but when I add an HTML file that I created the email has errors.
const fs = require("fs");
const { promisify } = require("util");
const readFile = promisify(fs.readFile);
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "mymail",
pass: "mypass",
},
});
let mailOptions = {
from: "[email protected]",
to: invoice.customeremail,
subject: "Payment forServices",
text: "Please pay to Recieve the order ",
html: readFile("C://Users//admin//task//payment.html", "utf8"),
};
transporter.sendMail(mailOptions, function (err, data) {
if (err) {
console.log("error", err);
} else {
console.log("email sent!");
}
});
I want the HTML file to be displayed in my email. Is there any way to do that?
fs.readFileinto a promise, therefore the value ofhtmlinmailOptionsis a pending promise which is not a valid string. Just use thefs.readFileSyncmethod instead or callawait readFilewhich will give an error because it's not in anasyncfunction.error TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Promise at validChunk (_stream_writable.js:281:10) at PassThrough.Writable.write (_stream_writable.js:316:21) at PassThrough.Writable.end (_stream_writable.js:585:10) at Immediate._onImmediate (C:\Users\admin\B.O.T task\node_modules\nodemailer\lib\mime-node\index.js:959:46) at processImmediate (internal/timers.js:456:21) { code: 'ESTREAM', command: 'API' }mailoptionsandsendMail()intofs.readFile()that will solve your issue.