Advertisement
xxZeus

juvin mail send

Jun 14th, 2021
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // should be outside, need to do it just once not everytime
  2. transporter.verify(function (error, success) {
  3.   if (error) {
  4.     console.log('Failed to connect to SMTP server');
  5.     console.log(error);
  6.   } else {
  7.     console.log('Connected to SMTP server!');
  8.   }
  9. });
  10.  
  11. // promise wrapper to send email, so that we can use this function easily in async method easily later on:
  12. const sendEmailPromise = (mailOptions) => new Promise((resolve, reject) => {
  13.   transporter.sendMail(mailOptions, async function (err, data) {
  14.     // not rejecting in any scenario, if error is there just add err: True to result
  15.     // this is because we are going to use it in Promise.all and if even one fails, Promise.all throws error which we dont want
  16.     // attaching originalOptions just to log it later on if needed
  17.     if (err) {
  18.       resolve({ error: true, err: err, originalOptions: mailOptions });
  19.     }
  20.     else {
  21.       resolve({ ...data, originalOptions: mailOptions });
  22.     }
  23.   });
  24. });
  25.  
  26.  
  27. exports.mailSend = catchAsyncErrors(async (req, res, next) => {
  28.   const { list, buildId } = req.body;
  29.   var rejected = 0; // should not be const, const value cannot change so ++rejected would not work actually
  30.   const build = await Build.findById(buildId);
  31.   if (!build) {
  32.     return next(new ErrorHandler("Build not found", 404));
  33.   }
  34.   var mail_queue = [];
  35.   list.forEach((item, index) => {
  36.     const mailOptions = {
  37.       // from: process.env.EMAIL,
  38.       to: item.email,
  39.       subject: "Invitation to Test",
  40.       html: `<h1>You have Been Invited to Test an Application</h1><p>Click <a href='${req.protocol}://${process.env.REQ_HOST}/invites/${buildId}'>here</a> or visit your profile on <a href="${req.protocol}://${process.env.REQ_HOST}/profile">${req.protocol}://${process.env.REQ_HOST}/profile</a> to view.</p>`,
  41.     };
  42.     mail_queue.push(sendEmailPromise(mailOptions));
  43.   });
  44.   // now that we have pushed each email in array we actually need to wait for each to be finished, this is where we use Promise.all
  45.   const mailResults = await Promise.all(mail_queue);
  46.   // now we loop through mailResults and just see if err: true OR .rejected.length > 0 as you were checking
  47.   mailResults.forEach((result, index) => {
  48.     if (result.err || (result.rejected && result.rejected.length > 0)) {
  49.       console.log("Failed to send eamil for", result.originalOptions);
  50.       console.log(result);
  51.       console.log(); // blank line for clarity in logs
  52.       rejected++;
  53.     }
  54.     else {
  55.       build.invites.push({ tester: item._id, status: "pending" });
  56.     }
  57.   });
  58.   await build.save(); // save build
  59.   if (rejected > 0)
  60.     return next(
  61.       new ErrorHandler(
  62.         "A few testers didn't get invited. Check the invited list.",
  63.         417
  64.       )
  65.     );
  66.   res.status(200).json({
  67.     success: true,
  68.   });
  69. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement