Reputation: 11
Whenever I am trying to send mail to over a list of 50 emails, first email is getting sent perfectly to around 10 of them and then after a certain period of time its giving error.
Your message wasn't delivered because the destination email system rejected your message for security or policy reasons. For example, the email address might only accept messages from certain senders, or it might not accept certain types of messages, like those larger than a specific size.
Contact the recipient (by phone, for example) and work with them and their email admin to determine what policy or setting blocked your message and what you should do to make sure that future messages from you won't be rejected.
This is my code:
const path = require("path");
const nodemailer = require("nodemailer");
const hbs = require("nodemailer-express-handlebars");
require('dotenv').config();
// const { mail_list } = require("./csvjson");
const { mail } = require("./testing");
const filteredMail = mail;
const sendMail = (name, email, indx) => {
return new Promise((resolve, reject) => {
try {
const transporter = nodemailer.createTransport({
host: process.env.EMAIL_SERVICE,
port: parseInt(process.env.EMAIL_PORT),
secure: parseInt(process.env.EMAIL_PORT) == 465,
secureConnection: false,
tls: {
ciphers: "SSLv3",
},
requireTLS: true,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD,
},
pool: true,
connectionTimeout: 10000,
maxConnections: 1, // Use one connection at a time
maxMessages: 3, // Send a max of 3 messages per connection
// logger: true,
// debug: true
});
const handlebarOptions = {
viewEngine: {
extName: ".handlebars",
partialsDir: path.resolve("./views"),
defaultLayout: false,
},
viewPath: path.resolve("./views"),
extName: ".handlebars",
};
transporter.use("compile", hbs(handlebarOptions));
const mailOptions = {
from: process.env.EMAIL_USER,
to: email,
subject: "Ok",
template: "email",
context: {
title: "Ok",
email: email,
name: name,
}
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(`Error sending email: ${indx}:`, error.message);
reject(error);
} else {
console.log(`Success: ${indx} : ${email}`);
resolve(info);
}
});
} catch (error) {
console.error(`Error setting up email: ${indx}:`, error.message);
reject(error);
}
});
};
const sendAllMails = (indx = 0) => {
if (indx < filteredMail.length) {
sendMail(filteredMail[indx].Name, filteredMail[indx].Email, indx)
.then(() => {
setTimeout(() => {
sendAllMails(indx + 1);
}, 15000); // 5000 milliseconds = 5 seconds
})
.catch(() => {
setTimeout(() => {
sendAllMails(indx); // Retry the same index
}, 180000); // 180000 milliseconds = 3 min
});
}
};
sendAllMails();
my env is:
EMAIL_SERVICE = "smtp.hostinger.com"
EMAIL_PORT = 465
EMAIL_USER = "email"
EMAIL_PASSWORD = "password"
can anyone resolve the issue?
I have business premium pack of Hostinger email.
Upvotes: 1
Views: 52