/**
* Resend Email Client
* Handles sending emails via Resend API
*/
import { Resend } from 'resend';
// Lazy initialization to avoid build-time errors when API key isn't available
let resendClient: Resend | null = null;
function getResendClient(): Resend | null {
if (!process.env.RESEND_API_KEY) {
return null;
}
if (!resendClient) {
resendClient = new Resend(process.env.RESEND_API_KEY);
}
return resendClient;
}
const FROM_EMAIL = process.env.RESEND_FROM_EMAIL || 'digests@canadagpt.ca';
const FROM_NAME = process.env.RESEND_FROM_NAME || 'CanadaGPT';
interface SendEmailParams {
to: string;
subject: string;
html: string;
text?: string;
}
interface SendEmailResult {
success: boolean;
data?: { data?: { id: string } };
error?: string;
}
/**
* Send a digest email via Resend
*/
export async function sendDigestEmail(params: SendEmailParams): Promise<SendEmailResult> {
try {
const resend = getResendClient();
if (!resend) {
console.warn('RESEND_API_KEY not configured, skipping email send');
return {
success: true,
data: { data: { id: 'skipped-no-api-key' } },
};
}
const { data, error } = await resend.emails.send({
from: `${FROM_NAME} <${FROM_EMAIL}>`,
to: params.to,
subject: params.subject,
html: params.html,
text: params.text,
});
if (error) {
console.error('Resend error:', error);
return {
success: false,
error: error.message,
};
}
return {
success: true,
data: { data: { id: data?.id || 'unknown' } },
};
} catch (error) {
console.error('Error sending email:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
}