import nodeMailer from 'nodemailer'; export default async function sendMail({ emailId, link, type, status, designation, }) { // Check if we have email password in env if (!process.env.TravelPolicyEmail || !process.env.TravelPolicyEmailPass) { console.log("No email password found in .env, so I can't send emails."); return; } console.log("parametrs", emailId, link, type, status, designation); const transporter = nodeMailer.createTransport({ service: 'gmail', port: 465, secure: true, logger: true, debug: true, secureConnection: false, auth: { user: process.env.TravelPolicyEmail, pass: process.env.TravelPolicyEmailPass, }, tls: { rejectUnauthorized: true, }, }); let mailOptions; switch (type) { case 'validator': mailOptions = { from: process.env.TravelPolicyEmail, to: emailId, subject: 'You have a new travel policy application to review', html: `
You have a new travel policy application to review. Please click on the link below to review the application:
Review ApplicationThank you.
`, }; break; case 'applicant': mailOptions = { from: process.env.TravelPolicyEmail, to: emailId, subject: `Your travel policy application status: ${status}`, html: `Your travel policy application has been ${status} by ${designation}. Please click on the link below to view the status of your application:
View ApplicationThank you.
`, }; break; case "accounts": mailOptions = { from: process.env.TravelPolicyEmail, to: emailId, subject: "Transfer money to the applicant", html: `Please transfer the travel policy amount to the applicant's account. Click on the link below to view the application:
`, }; break; default: throw new Error("Invalid email type"); } try { await transporter.sendMail(mailOptions); console.log('Email sent successfully'); return true; } catch (error) { console.error('Error sending email:', error); return { status: 'error', message: 'Verification code not sent' }; } }