'Node.js - SendGrid : Doesn't send to my array recipients (only in production mode)

My env :

"next": "^12.0.8",
"@sendgrid/mail": "^7.6.1"

My code - API:

import sendgrid from '@sendgrid/mail';

sendgrid.setApiKey(process.env.SENDGRID_API_KEY);

export default async function sendEmailDestinataire(req, res) {
  const msg = {
    to: req.body.email,
    from: {
      email: '[email protected]',
      name: 'From my name',
    },
    templateId: 'd-xxxxxxxxxxxxxxxxx',
    dynamic_template_data: {
      name: req.body.name,
      email: req.body.email,
      message: req.body.message,
      file: req.body.file,
    },
  };
  console.log('====================================');
  console.log('api msg', msg.to);
  console.log('====================================');
  try {
    await sendgrid.sendMultiple(msg);
    res.json({ message: `Email has been sent` });
  } catch (error) {
    res.status(500).json({ error: 'Error sending email' });
  }
}

My code - FRONT

let listEmails = '';
if (getUserMail.edges && getUserMail?.edges[0]?.node?.email === email) {
  const tmpListEmails = getUserMail.edges[0]?.node?.listeEmails?.emails;
  listEmails = tmpListEmails.replace(' ', '').split(',');
}

const res2 = await fetch('/api/sendgridDestinataire', {
  body: JSON.stringify({
    email: listEmails.length > 0 ? listEmails : email,
    name: name,
    file: allUrls,
    message: message,
  }),
  headers: {
    'Content-Type': 'application/json',
  },
  method: 'POST',
});
await res.json();

My problem :

In my development environment everything works fine. But when I'm in production, the email is sent to the email state. And no, to those from my array. The email variable transmitted to the API sends me back the object...

Thank you in advance for your help.

And sorry for my English

Bruce



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source