feat: move transaction emails to zeptomail
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const express = require('express');
|
||||
const zod = require('zod');
|
||||
const { getTransporter } = require('../utils/getTransporter');
|
||||
const { nodemailerTransporter, zohoClient } = require('../utils/email');
|
||||
const { withEmailTemplate } = require('../utils/withEmailTemplate');
|
||||
|
||||
const router = express.Router();
|
||||
@@ -9,13 +9,70 @@ let personalTransporter;
|
||||
let defaultTransporter;
|
||||
|
||||
// Initialize transporters
|
||||
getTransporter(true).then((t) => {
|
||||
nodemailerTransporter(true).then((t) => {
|
||||
personalTransporter = t;
|
||||
});
|
||||
getTransporter(false).then((t) => {
|
||||
nodemailerTransporter(false).then((t) => {
|
||||
defaultTransporter = t;
|
||||
});
|
||||
|
||||
async function sendWithNodemailer(emailData) {
|
||||
const { from, to, subject, content, isPersonalEmail, replyTo } = emailData;
|
||||
|
||||
const transporter = isPersonalEmail ? personalTransporter : defaultTransporter;
|
||||
|
||||
if (!transporter) {
|
||||
return;
|
||||
}
|
||||
|
||||
return await transporter.sendMail({
|
||||
from: from || '"Best from ClassroomIO" <[email protected]>',
|
||||
to,
|
||||
subject,
|
||||
replyTo,
|
||||
html: withEmailTemplate(content)
|
||||
});
|
||||
}
|
||||
|
||||
// format: "ClassroomIO Developers (via ClassroomIO.com)" <[email protected]>
|
||||
function extractNameAndEmail(str) {
|
||||
// Use regular expressions to match the name and email
|
||||
const regex = /"(.*?)"\s+<\s*(.*?)@(.*?)\s*>/;
|
||||
const match = str.match(regex);
|
||||
|
||||
if (match) {
|
||||
// Extract the name and email from the match groups
|
||||
const name = match[1];
|
||||
const email = match[2] + '@' + match[3];
|
||||
return { name, email };
|
||||
} else {
|
||||
// Return undefined if the format doesn't match
|
||||
return { name: from, email: from };
|
||||
}
|
||||
}
|
||||
|
||||
async function sendWithZoho(emailData) {
|
||||
const { from, to, subject, content } = emailData;
|
||||
|
||||
const fromData = extractNameAndEmail(from);
|
||||
|
||||
return zohoClient.sendMail({
|
||||
from: {
|
||||
address: fromData.email,
|
||||
name: fromData.name
|
||||
},
|
||||
to: [
|
||||
{
|
||||
email_address: {
|
||||
address: to
|
||||
}
|
||||
}
|
||||
],
|
||||
subject,
|
||||
htmlbody: content
|
||||
});
|
||||
}
|
||||
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const mySchema = zod.array(
|
||||
@@ -35,26 +92,18 @@ router.post('/', async (req, res) => {
|
||||
|
||||
Promise.all(
|
||||
emailDataArray.map(async (emailData) => {
|
||||
let res;
|
||||
|
||||
try {
|
||||
const { from, to, subject, content, isPersonalEmail, replyTo } = emailData;
|
||||
|
||||
const transporter = isPersonalEmail ? personalTransporter : defaultTransporter;
|
||||
|
||||
if (!transporter) {
|
||||
return;
|
||||
if (emailData.isPersonalEmail) {
|
||||
res = await sendWithNodemailer(emailData);
|
||||
} else {
|
||||
res = await sendWithZoho(emailData);
|
||||
}
|
||||
|
||||
const res = await transporter.sendMail({
|
||||
from: from || '"Best from ClassroomIO" <[email protected]>',
|
||||
to,
|
||||
subject,
|
||||
replyTo,
|
||||
html: withEmailTemplate(content)
|
||||
});
|
||||
|
||||
console.log('Email status', res);
|
||||
} catch (error) {
|
||||
console.error('Error sending email', error);
|
||||
console.error('Error sending email', error, error?.error?.details?.[0]);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
const { SendMailClient } = require('zeptomail');
|
||||
const nodemailer = require('nodemailer');
|
||||
|
||||
const {
|
||||
ZOHO_TOKEN,
|
||||
SMTP_HOST,
|
||||
SMTP_PORT,
|
||||
SMTP_USER,
|
||||
SMTP_PASSWORD,
|
||||
SMTP_USER_NOTIFY,
|
||||
SMTP_PASSWORD_NOTIFY
|
||||
} = process.env;
|
||||
|
||||
console.log({ ZOHO_TOKEN });
|
||||
|
||||
const zohoClient = new SendMailClient({ url: 'api.zeptomail.eu/', token: ZOHO_TOKEN });
|
||||
|
||||
const nodemailerTransporter = async (isPersonal = false) => {
|
||||
if (
|
||||
!SMTP_HOST ||
|
||||
!SMTP_PORT ||
|
||||
!SMTP_USER ||
|
||||
!SMTP_PASSWORD ||
|
||||
!SMTP_USER_NOTIFY ||
|
||||
!SMTP_PASSWORD_NOTIFY
|
||||
) {
|
||||
console.error('SMTP configuration missing');
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: SMTP_HOST,
|
||||
port: parseInt(SMTP_PORT || '465', 10),
|
||||
secure: true,
|
||||
auth: {
|
||||
user: isPersonal ? SMTP_USER : SMTP_USER_NOTIFY,
|
||||
pass: isPersonal ? SMTP_PASSWORD : SMTP_PASSWORD_NOTIFY
|
||||
}
|
||||
});
|
||||
await transporter.verify();
|
||||
return transporter;
|
||||
} catch (error) {
|
||||
console.error('Transporter error:', error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { nodemailerTransporter, zohoClient };
|
||||
Reference in New Issue
Block a user