Hello, guys
I'm trying to send an email with pdf attachment using nodemailer. In the request object I send the buffer, but in the backend part this part is empty.
Has anyone encountered such a issue ?
Also I try with different ways to send some random .txt attachment using like this:
Frontend request
Backend
I'm trying to send an email with pdf attachment using nodemailer. In the request object I send the buffer, but in the backend part this part is empty.
Has anyone encountered such a issue ?
Also I try with different ways to send some random .txt attachment using like this:
JavaScript:
{
// utf-8 string as an attachment
filename: "text1.txt",
content: "hello world!",
}
JavaScript:
async function fetchOffer() {
if (file) {
const buffer = await blob.arrayBuffer();
if (buffer) {
console.log('buffer', buffer);
const response = fetch(`${linkUrl()}/truckcovers-offer-email`, {
method: "POST",
body: JSON.stringify({ filename: file.name, file: buffer})
headers: {
'Accept': 'application/json',
'Content-Type': 'application/pdf'
},
}, setLoading(true)).then(
(response) => (response.json())
).then((response) => {
if (response.status === 'success') {
console.log('response: ' + response);
setVisible(true);
} else if (response.status === 'fail') {
console.log("Message failed to send.", response);
}
});
setLoading(false);
return response;
}
}
}
Backend
JavaScript:
app.post('/truckcovers-offer-email', async (req, res, next) => {
const buffer = await req.body.buffer;
let transporter = nodemailer.createTransport({
host: process.env.HOST_EMAIL,
port: process.env.PORT_EMAIL,
auth: {
user: process.env.USER_EMAIL,
pass: process.env.PASSWORD_EMAIL
}
});
const mailOptions = {
from: '[email protected]',
subject: 'Offer',
to: process.env.USER_EMAIL,
html: `<h1>Offer from client</h1>`,
attachments: [
{
filename: req.body.filename,
content: Buffer.from(JSON.stringify(req.body.file), "utf-8")
}
]
};
transporter.sendMail(mailOptions, (err, result) => {
if (err) {
console.error(err);
res.status(400).json({ "message": err })
} else {
res.status(200).json({ 'status': "success", 'message': 'Email sent:' + result.response });
}
});
});