Retry to send mail if not sent - javascript

I am using nodemailer to send emails through my node app. Sometimes Email does not work and throws an error until I try twice or thrice. I want my program to try again and again until the mail is successfully sent.
Here's my code:
const mailOptions = {
from: from,
to: client.email,
subject: 'Your Photos are ready',
html: mailTemplate
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
res.status(500).json({
message: "Mail not sent",
error
});
} else {
res.status(200).json({message: "Mail Sent", response: info.response});
}
});
How can I use the same function inside my if block?

Wrap sendMail in a function that returns a Promise
const promiseWrapper = mailOptions => new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
reject(error);
return;
}
resolve(info);
});
then in your route make the handler an async function and loop how many time that you want, then check if info exists if it does send 200 if not send 500
app.post('/sendmail', async (req, res) => {
let info;
let error;
for (let i = 0; i < 3; i++) {
try {
info = await promiseWrapper(mailOptions);
break;
} catch (e) {
error = e;
}
}
info
? res.status(200).json({ message: "Mail Sent", response: info.response })
: res.status(500).json({ message: "Mail not send", error }));
});

You can first separate the retry logic in a different file, so you can use it in various places.
Newer versions of nodemailer support promises for transporter.sendMail
// retry.js
// retries a function, called asynchronously, n amount of times
export async function retry(fn, n) {
for (let i = 0; i < n; i++) {
try {
return await fn()
} catch (err) {
console.log(err)
}
}
}
And pass the transporter function to the retry logic, with the amount of times you want to retry (in this example: 3)
import {retry} from '../utils/retry'
// ...
app.post('/sendmail', async (req, res) => {
try {
retry(
() =>
transporter.sendMail({
// your configuration
from: from,
to: client.email,
subject: 'Your Photos are ready',
html: mailTemplate
}),
3 // max retries
)
} catch (err) {
console.log(err)
// failed max retry times
res.sendStatus(500)
}
res.sendStatus(200)
})

const mailOptions = {
from: from,
to: client.email,
subject: 'Your Photos are ready',
html: mailTemplate
};
var i;
for(i = 0; i <= 1; i++) {
transporter.sendMail(mailOptions, function(error, info){
if (error) {
res.status(500).json({
message: "Mail not sent",
error
});
i = 0;
} else {
i = 2;
res.status(200).json({message: "Mail Sent", response: info.response});
}
});
}
Try the code above will run the function again and again if the error occur and it will exit the loop if no error occur.

Wrap it into a function and call in this way:
const mailOptions = {
from: from,
to: client.email,
subject: 'Your Photos are ready',
html: mailTemplate
};
function sendMail(mailOptions) {
transporter.sendMail(mailOptions, function(error, info){
if (error) {
return sendMail(mailOptions)
} else {
return res.status(200).json({message: "Mail Sent", response: info.response});
}
});
}
return sendMail(mailOptions);

Related

Getting result from MySQL

My backend is consist of Api and DB. When I want to get response from DB I have had delayed output by 1 query.
API (I think api is ok. Start read DB first)
app.post('/api/query', (req, res) => {
console.log(`\n Query input : ${JSON.stringify(req.body)}`);
let queryInput = (Object.values(req.body).join(' '));
if(!dbApi.checkArray(queryInput)){ //If array is not made from clear strings
res.json(dbApi.queryFromUser(queryInput));
}
else{
res.json(dbApi.queryOutput);
}
});
app.listen(dbConfig.server.port, () =>
console.log(`Server running on port ${dbConfig.server.port}`));
DB
queryOutput = [];
const receivingQuery =(queryInput) => {
db.query(queryInput, (err, result) =>{
if(err) throw err+' : '+queryInput;
queryOutput = result;
console.log("\nQuery output "+ JSON.stringify(queryOutput)); //Output (result) is ok
});
return queryOutput //Here is Output from previous query (sends to API)
}
module.exports = {
queryOutput: queryOutput,
queryFromUser: receivingQuery,
}
I tryied callback method and I rewrite it couple of times. But I dont have enough skill to solve it.
If You want to return result of query so simply do following things:
add query method to db module:
function query(sql, args = []) {
return new Promise(function(resolve, reject) {
db.query(sql, args, (err, result) => {
if (err) return reject(err);
resolve(result);
});
});
}
// extra feature, getting user by id
async function getUserById(id) {
const result = await query('SELECT * FROM users WHER id = ? LIMIT 1', [id]);
if (Array.isArray(result) && result[0]) return result[0];
return null;
}
module.exports = {
query,
getUserById, // export user by id
queryOutput,
queryFromUser: receivingQuery,
}
use it (with async and await):
app.post('/api/query', async (req, res) => {
try {
console.log('Query input:', req.body);
const queryInput = Object.values(req.body).join(' ');
const result = await dbApi.query(queryInput);
res.json(result);
}
catch (error) {
console.error(error);
res.status(500).json({message: 'Please try again soon'});
}
});
app.get('/api/users/:id', async (req, res) => {
try {
const user = await dbApi.getUserById(req.params.id);
if (!user) return res.status(404).json({message: 'User not found'});
res.status(200).json(user);
}
catch (error) {
console.error(error);
res.status(500).json({message: 'Please try again soon'});
}
});
app.listen(dbConfig.server.port, () =>
console.log('Server running on port', dbConfig.server.port));

Async/Await Ignoring Return

I want to send different SMS to different people and each call to send the SMS should be synchronous, I implemented the async await for my function to work like this, but for some reason it's not working as expected.
Here's my code:
After query for qualified users,
if(userQualifies) {
try{
await insertIntoTable();
} catch(err) {
console.log(err);
}
}
async function insertIntoTable(){
try{
await db.any(QUERY TO INSERT)
.then(async function(idCreated){
try{
var params = {
'messagingServiceSid': 'XXXXXXXXXX',
'to': ['1' + phone],
'body': message,
}
await sendMessage(params);
}catch(error){
console.log(error);
}
})
} catch(err){
console.log(err);
}
}
async function sendMessage(params) {
console.log('Im on sendMessage');
return client.messages.create(params)
.then( msg => {
console.log("SUCCESS:");
})
.catch(err => {
console.log("ERROR:");
});
console.log("message sent");
return 'done';
}
when I run this, I get the log of Im on sendMessage after inserting into the table but it doesn's send the message, it's ignoring the return of the sendMessage() function and sends all of the messages at the end at the same time.
Am I missing something to make it send the message when it goes from insertIntoTable() to sendMessage()
I'm not really sure what most of your code is doing, but here's the async pattern for waiting for one thing in an array to finish its async action before starting the next one:
async function sendMessage(params) {
console.log('Im on sendMessage');
return client.messages.create(params)
.then( msg => {
console.log("SUCCESS:");
})
.catch(err => {
console.log("ERROR:");
});
}
const messages = [{
'messagingServiceSid': 'XXXXXXXXXX',
'to': ['1000000'],
'body': 'first message',
},{
'messagingServiceSid': 'XXXXXXXXXX',
'to': ['2000000'],
'body': 'second message',
},
{
'messagingServiceSid': 'XXXXXXXXXX',
'to': ['3000000'],
'body': 'third message',
}];
async function setMultipleMessages(messages) {
for (let message of messages) {
await sendMessage(message);
}
}
setMultipleMessages(messages);

callback does not invoke

MongoClient.connect's callback does not invoke
I try to invoke the callback in aws lambda but it doesn't work, however if I remove exports.lambdaHandler and just invoke it by node app.js it works just fine.
exports.lambdaHandler = async (event, context) => {
try {
MongoClient.connect(url, {
useNewUrlParser: true
}, (err, client) => {
console.log("Connected successfully to server");
});
response = {
'statusCode': 200,
'body': JSON.stringify({
message: 'test'
})
}
return response
} catch (err) {
console.log(err);
return err;
}
};
should return
Connected successfully to server
edit
I changed it to await instead. now it's good
exports.lambdaHandler = async (event, context) => {
try {
const client = await MongoClient.connect(url, {
useNewUrlParser: true
})
const db = client.db(dbName)
await db.createCollection(collectionName)
client.close()
return {
'statusCode': 200,
'body': JSON.stringify({
message: 'test'
})
}
} catch (err) {
console.log(err);
return err;
}
};
Try this, it would return the results else throw an error which will be caught below and returned by Lambda
exports.lambdaHandler = async (event, context) => {
try {
MongoClient.connect(url, {useNewUrlParser: true}, (err, client) => {
if (err) { console.log(err); throw(err); }
else {
console.log("Connected successfully to server");
console.log(client);
response = {
'statusCode': 200,
'body': JSON.stringify({message: client})
};
return response;
};
});
} catch (err) {
console.log(err);
return err;
}
};

Promise returns undefined json in Express post request

I have a promise within a selectRecipientData function that returns some user data from an api.
export async function selectRecipientData({ email }) {
engage.selectRecipientData({
listId: listId,
email: email,
returnContactLists: false,
}, function(err, result) {
if(err) {
console.log(err);
} else {
let recipient = JSON.stringify(result);
// this logs successfully
console.log('Recipient details: ' + recipient );
return recipient;
}
});
}
When I call this function within a post request. The data is logged within the promise but is undefined when returned as per below:
server.post('/api/v1/public/selectrecipientdata', async (req, res) => {
formData = req.body;
let { email } = formData;
if (!email) {
res.json({ error: 'Email is required' });
return;
}
try {
let recipientData = await selectRecipientData({ email });
// why is this undefined?
console.log('This is Undefined: '+ JSON.stringify(recipientData) );
res.json({recipientData});
} catch (err) {
res.json({ error: err.message || err.toString() });
}
});
Anyone tell me why? Thanks
You've written selectRecipientData as a callback style function, but you're calling it as an async/await style. If engage.selectRecipientData returns a promise, you could do something like:
export async function selectRecipientData({email}) {
const result=await engage.selectRecipientData({
listId: listId,
email: email,
returnContactLists: false,
});
const recipient=JSON.stringify(result);
console.log('Recipient details: ' + recipient );
return recipient;
}
Otherwise, to convert it to a promise you could do something like:
export function selectRecipientData({email}) {
return new Promise((resolve,reject)=>{
engage.selectRecipientData({
listId: listId,
email: email,
returnContactLists: false,
}, function(err, result) {
if (err) {
reject(err);
}
else {
let recipient = JSON.stringify(result);
console.log('Recipient details: ' + recipient);
resolve(recipient);
}
});
});
}

node.js sending mails with a delay

I am doing a simple thing but that isn't working.
What I want to do is send mails with a delay of 30 seconds.
Here's the code:
user.forEach(function(data) {
var locals = {
fname: data.Name,
your_name: data.From,
}
template.render(locals, function(err, results) {
if (err) {
return console.error(err)
} else {
transporter.sendMail({
to: data.Email,
subject: "Welcome",
replyTo: data.ReplyTo,
html: results.html,
text: results.text,
}, function(error, info) {
console.log("here");
if (error) {
console.log(error);
} else {
console.log('Message sent: ' + info.response);
};
});
}
});
});
Here user is an array of objects with details like Email,from,Name etc.
Each object in array has details of a particular mail to be sent.
I want to send a mail and wait for 30s and then send the second one..and wait and so on.
I have used setInterval and also npm sleep, but that isn't working. It waits for 30s and then sends all mails at once.
You should replace syncronous forEach with asynchronous implementation.
Option1. Use async.js eachLimit and call callback with delay of 30 seconds
Option2. You can write wrapper for your send email function like:
var emails = ['email1', 'email2' /*...*/];
function sendEmailAndWait(email, callback){
// your stuff
transporter.sendMail(email, function(error, info) {
// handle results
if(!emails.length) return callback();
setTimeout(function () {
sendEmailAndWait(emails.shift(), callback);
}, 30*1000)
})
}
sendEmailAndWait(emails.shift(), function(){ /* allDone */});
setTimeout(function() {
template.render(locals, function(err, results) {
if (err) {
return console.error(err)
} else {
transporter.sendMail({
to: data.Email,
subject: "Welcome",
replyTo: data.ReplyTo,
html: results.html,
text: results.text,
}, function(error, info) {
console.log("here");
if (error) {
console.log(error);
} else {
console.log('Message sent: ' + info.response);
};
});
}
});
}, 3000);

Categories

Resources