/home/techb158/balavpn.abdallabala.com/src/lib
Edit: /home/techb158/balavpn.abdallabala.com/src/lib/email.js (2175B)
const SMTP_HOST = process.env.COSMIC_SMTP_HOST || '';
const SMTP_PORT = parseInt(process.env.COSMIC_SMTP_PORT, 10) || 587;
const SMTP_USER = process.env.COSMIC_SMTP_USER || '';
const SMTP_PASS = process.env.COSMIC_SMTP_PASS || '';
const SMTP_FROM = process.env.COSMIC_SMTP_FROM || 'noreply@cosmic-ai-risk.com';
const ENABLED = !!(SMTP_HOST && SMTP_USER && SMTP_PASS);
let transporter = null;
async function getTransporter() {
if (!ENABLED) return null;
if (transporter) return transporter;
try {
const nodemailer = require('nodemailer');
transporter = nodemailer.createTransport({
host: SMTP_HOST,
port: SMTP_PORT,
secure: SMTP_PORT === 465,
auth: { user: SMTP_USER, pass: SMTP_PASS },
});
await transporter.verify();
return transporter;
} catch {
transporter = null;
return null;
}
}
async function sendEmail({ to, subject, html, text }) {
const t = await getTransporter();
if (!t) return null;
try {
const info = await t.sendMail({ from: SMTP_FROM, to, subject, html, text });
return info;
} catch {
return null;
}
}
async function sendInviteEmail({ email, inviteLink, invitedByName, organizationName }) {
return sendEmail({
to: email,
subject: `You're invited to join ${organizationName} on COSMIC AI-Risk`,
html: `
You're invited!
${invitedByName} has invited you to join ${organizationName} on COSMIC AI-Risk.
Click the button below to accept the invitation:
Accept Invitation
This link expires in 7 days. If you didn't expect this invitation, you can ignore this email.
`,
text: `You're invited to join ${organizationName} on COSMIC AI-Risk. Accept here: ${inviteLink}`,
});
}
module.exports = { sendEmail, sendInviteEmail };