![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.ca/public_html/src/utils/ |
import nodemailer from 'nodemailer';
// Create transporter with fallback to console logging if SMTP is not available
const createTransporter = () => {
if (!process.env.SMTP_HOST || !process.env.SMTP_USER || !process.env.SMTP_PASSWORD) {
console.warn('[EMAIL] SMTP configuration missing, emails will be logged to console only');
return null;
}
try {
return nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT || '587'),
secure: process.env.SMTP_SECURE === 'true',
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD,
},
// Add timeout to prevent hanging
connectionTimeout: 10000,
greetingTimeout: 10000,
socketTimeout: 10000,
});
} catch (error) {
console.error('[EMAIL] Failed to create SMTP transporter:', error);
return null;
}
};
const transporter = createTransporter();
interface EmailOptions {
to: string;
subject: string;
text: string;
html: string;
}
export async function sendEmail({ to, subject, text, html }: EmailOptions) {
try {
// Check if transporter is available
if (!transporter) {
console.log(`[EMAIL] SMTP not configured, logging email to console:`);
console.log(`[EMAIL] To: ${to}`);
console.log(`[EMAIL] Subject: ${subject}`);
console.log(`[EMAIL] Text: ${text}`);
console.log(`[EMAIL] HTML: ${html}`);
return { messageId: 'console-logged' };
}
const info = await transporter.sendMail({
from: process.env.SMTP_FROM,
to,
subject,
text,
html,
});
console.log('Email sent:', info.messageId);
return info;
} catch (error) {
console.error('Error sending email:', error);
console.log(`[EMAIL] Failed to send email to ${to}, subject: ${subject}`);
console.log(`[EMAIL] Email content would have been: ${text}`);
// In development, don't throw errors for email failures
if (process.env.NODE_ENV === 'development') {
console.log('[EMAIL] In development mode, continuing without email');
return { messageId: 'failed-but-continued' };
}
throw error;
}
}
export async function sendConfirmationEmail(email: string, firstName: string, isFrench: boolean = false) {
const subject = isFrench
? 'Confirmation de votre inscription à l\'action collective'
: 'Confirmation of your class action registration';
const text = isFrench
? `Cher ${firstName},\n\nNous avons bien reçu votre inscription à l'action collective. Nous vous contacterons bientôt pour la suite du processus.\n\nCordialement,\nL'équipe de l'action collective`
: `Dear ${firstName},\n\nWe have received your registration for the class action. We will contact you soon to proceed with the next steps.\n\nBest regards,\nThe Class Action Team`;
const html = isFrench
? `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2 style="color: #2563eb;">Confirmation de votre inscription</h2>
<p>Cher ${firstName},</p>
<p>Nous avons bien reçu votre inscription à l'action collective. Nous vous contacterons bientôt pour la suite du processus.</p>
<p>Cordialement,<br>L'équipe de l'action collective</p>
</div>
`
: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2 style="color: #2563eb;">Registration Confirmation</h2>
<p>Dear ${firstName},</p>
<p>We have received your registration for the class action. We will contact you soon to proceed with the next steps.</p>
<p>Best regards,<br>The Class Action Team</p>
</div>
`;
return sendEmail({ to: email, subject, text, html });
}
export async function sendPasswordResetEmail(email: string, token: string, lang: 'en' | 'fr' = 'en') {
const baseUrl = process.env.NODE_ENV === 'production'
? 'https://action-collective.info'
: 'https://localhost';
const resetUrl = `${baseUrl}/auth/reset-password?token=${token}`;
const subject = lang === 'fr'
? 'Réinitialisation de votre mot de passe'
: 'Reset your password';
const text = lang === 'fr'
? `Pour réinitialiser votre mot de passe, cliquez sur ce lien : ${resetUrl}\nCe lien expirera dans une heure.`
: `To reset your password, click this link: ${resetUrl}\nThis link will expire in one hour.`;
const html = lang === 'fr'
? `<p>Pour réinitialiser votre mot de passe, cliquez sur ce lien :</p><p><a href="${resetUrl}">${resetUrl}</a></p><p>Ce lien expirera dans une heure.</p>`
: `<p>To reset your password, click this link:</p><p><a href="${resetUrl}">${resetUrl}</a></p><p>This link will expire in one hour.</p>`;
return sendEmail({ to: email, subject, text, html });
}