Sending PDF with other form data - ReactJS/Strapi/Nodemailer

Viewed 12

i am currently have a form in my react/next js app. From there i send data as values to strapi custom backend, where nodemailer will send email.

I am wondering how i can create attachment on front end and send it along with other values to strapi and then send email with values and uploaded files via nodemailer.

This is my code:

SENDING VALUES TO STRAPI:

const sendEmail = async (values): Promise<boolean | string> => {

  const { files } = values // values = { name, email, phone...+ uploaded file..

  try {
    await api.post(EMAIL_PATH, values);
    // console.log(values)
    return true;
  } catch (error) {
    captureMessage(`Kontaktní formulář selhal: ${error}`);
    console.log(values)
    return false;
  }
};

STRAPI CUSTOM BACKEND with NODEMAILER

module.exports = {
  index: async ({ request }) => {
    const { name, phoneNumber, email, agreeWithTerms, dynamicQuestions, receiveEmail, job, files} = request.body

    const dynamicText = dynamicQuestions?.reduce((acc, obj) => acc + `<p>${obj.question}</p> <p>${obj.answer}</p> </br>`, "");

    const messageText = `
    <p>Jméno a příjmení: ${name}</p> </br> 
    <p>Telefoní číslo: ${phoneNumber}</p> </br>
    <p>Email: ${email}</p> </br>
    <p>Souhlas s podmínkami: ${agreeWithTerms ? "ANO" : "NE"}</p> </br>` + 
    dynamicText

    console.log(files)

    

  // create reusable transporter object using the default SMTP transport
  const transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
      user: process.env.EMAIL_NAME,
      pass: process.env.EMAIL_PASSWORD
    },
  });
  
  // send mail with defined transport object
  const info = await transporter.sendMail({
    from: process.env.EMAIL_NAME, // sender address
    to: receiveEmail, // list of receivers
    subject: `Pozice: ${job}`, // Subject line
    html: messageText // html 
  })

  return info
  
}
}
0 Answers
Related