mitva virvadiya
mitva virvadiya

Reputation: 1

Nodemailer Mail failed ERROR CODE :550 - 5.0.0 Invalid Recipients - Not catching in error handler

I am making a bulk mail utility in my project. We have made a functionality to send users mail in batches.

Using ZOHO SMTP servers and Node + Nodemailer for sending bulk mails. Mails were sending perfectly, but as soon I tested by putting invalid mail address, error should be catched in Nodemailer sendmail callback function. But error remains null even though mail is not sent.

I got mail on my ZOHO mail service that mail is not sent due to 550 error. But the real problem is if mail is not sent it should be catched in error of sendmail function by Nodemailer right?

const sendEmail = async (options) => {
  const mail = {
    from: "my mail address",
    to: options.email,
    subject: options.subject,
    text: options.textContent,
    html: options.htmlContent,
  };

  return new Promise((resolve, reject) => {
    transporter.sendMail(mail, (error, data) => {
      if (error) {
        console.error(
          `Transport-level error sending email to ${options.email}:`,
          error
        );
        reject({
          success: false,
          email: options.email,
          error: error.response || error.message,
        });
      } else {
        if (data.rejected && data.rejected.length > 0) {
          console.error(`Rejected recipients: ${data.rejected}`);
          reject({
            success: false,
            email: options.email,
            error: `550 - Invalid Recipient: ${data.rejected.join(", ")}`,
          });
        } else {
          console.log(`Email sent successfully to ${options.email}:`, data);
          resolve({
            success: true,
            email: options.email,
          });
        }
      }
    });
  });
};
try {
  const result = await sendEmail(emailOptions);
  console.log("result", result);

  if (result.success) {
    return result.email;
  } else {
    throw new Error(
      `Failed to send email: ${result.email}, Error: ${result.error}`
    );
  }
} catch (err) {
  throw {
    message: err.message,
    user: user.e_mail_id || user.client_id_mail,
  };
}

I am looking for a solution for this problem, as i am new to mail services and all, i would also like some advice on optimizing my bulk mail utility. any docs or tutorial would be great.

Upvotes: 0

Views: 102

Answers (0)

Related Questions