import * as nodemailer from 'nodemailer'; import * as handlebars from 'handlebars'; import * as path from 'path'; import * as fs from 'fs'; import { TransactionPaymentType } from 'src/modules/transaction/transaction/constants'; export async function sendEmail(receivers, subject) { const smtpTransport = nodemailer.createTransport({ host: process.env.EMAIL_HOST, port: process.env.EMAIL_POST, auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_TOKEN, }, }); let templateName = 'payment-confirmation-bank'; for (const receiver of receivers) { try { if (receiver.payment_type == TransactionPaymentType.MIDTRANS) templateName = 'payment-confirmation-midtrans'; let templatePath = path.join( __dirname, `../email-template/${templateName}.html`, ); templatePath = templatePath.replace(/dist/g, 'src'); const templateSource = fs.readFileSync(templatePath, 'utf8'); const template = handlebars.compile(templateSource); const htmlToSend = template(receiver); const emailContext = { from: process.env.EMAIL_SENDER ?? 'no-reply@weplayground.app', to: receiver.email, subject: subject, html: htmlToSend, attachDataUrls: true, }; await new Promise((f) => setTimeout(f, 2000)); smtpTransport.sendMail(emailContext, function (err, data) { if (err) { console.log(err, `Error occurs on send to ${receiver.email}`); } else { console.log(`Email sent to ${receiver.email}`); } }); } catch (error) { console.log(error, `Error occurs on send to ${receiver.email}`); } } }