Request data is empty

Joined
Nov 29, 2023
Messages
1
Reaction score
0
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:
JavaScript:
{
 // utf-8 string as an attachment
 filename: "text1.txt",
 content: "hello world!",
}
Frontend request
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 });
    }
  });
});
 

Attachments

  • Screenshot 2023-11-29 150610.png
    Screenshot 2023-11-29 150610.png
    40.1 KB · Views: 17
  • Screenshot 2023-11-29 150634.png
    Screenshot 2023-11-29 150634.png
    13.8 KB · Views: 18

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,057
Messages
2,570,443
Members
47,113
Latest member
XZJMike318

Latest Threads

Top