This commit is contained in:
@@ -513,3 +513,45 @@ export async function getEmailService(): Promise<EmailService> {
|
||||
export function createEmailService(config: SMTPConfig): EmailService {
|
||||
return new EmailService(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic send email function for custom templates
|
||||
*/
|
||||
export async function sendEmail(options: {
|
||||
to: string;
|
||||
subject: string;
|
||||
template: string;
|
||||
data: any;
|
||||
}): Promise<void> {
|
||||
try {
|
||||
const emailService = await getEmailService();
|
||||
|
||||
// Get the template
|
||||
const templateFunction = (emailService as any).getTemplate(options.template);
|
||||
if (!templateFunction) {
|
||||
throw new Error(`Email template '${options.template}' not found`);
|
||||
}
|
||||
|
||||
// Add common template data
|
||||
const templateData = {
|
||||
...options.data,
|
||||
logoUrl: options.data.logoUrl || 'https://portal.monacousa.org/MONACOUSA-Flags_376x376.png',
|
||||
baseUrl: process.env.NUXT_PUBLIC_DOMAIN || 'https://portal.monacousa.org'
|
||||
};
|
||||
|
||||
// Compile template with data
|
||||
const html = templateFunction(templateData);
|
||||
|
||||
// Send email using the private sendEmail method
|
||||
await (emailService as any).sendEmail({
|
||||
to: options.to,
|
||||
subject: options.subject,
|
||||
html
|
||||
});
|
||||
|
||||
console.log(`[sendEmail] ✅ Email sent successfully to ${options.to} using template '${options.template}'`);
|
||||
} catch (error) {
|
||||
console.error(`[sendEmail] ❌ Failed to send email to ${options.to}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user