import { Resend } from 'resend';
const resend = new Resend(process.env.RESEND_API_KEY);
interface SendEmailParams {
to: string;
subject: string;
html: string;
}
export async function sendEmail({ to, subject, html }: SendEmailParams) {
try {
console.log('准备发送邮件:', {
to,
subject,
from: process.env.RESEND_FROM_EMAIL,
apiKeyExists: !!process.env.RESEND_API_KEY
});
if (!process.env.RESEND_API_KEY) {
console.error('RESEND_API_KEY未设置');
return { success: false, error: 'RESEND_API_KEY未设置' };
}
if (!process.env.RESEND_FROM_EMAIL) {
console.error('RESEND_FROM_EMAIL未设置');
return { success: false, error: 'RESEND_FROM_EMAIL未设置' };
}
const data = await resend.emails.send({
from: process.env.RESEND_FROM_EMAIL!,
to,
subject,
html,
});
console.log('邮件发送成功:', data);
return { success: true, data };
} catch (error) {
console.error('邮件发送失败详细信息:', {
error: error instanceof Error ? error.message : error,
stack: error instanceof Error ? error.stack : undefined,
to,
subject
});
return { success: false, error };
}
}
export function generateVerificationEmailHtml(verificationUrl: string, locale: string = 'en') {
const isZh = locale === 'zh';
return `
${isZh ? '您好!' : 'Hello!'}
${isZh ? '感谢您注册Aiartools!请点击下面的按钮验证您的邮箱地址:' : 'Thank you for signing up for Aiartools! Please click the button below to verify your email address:'}
${isZh ? '或者复制以下链接到浏览器中:' : 'Or copy and paste this link into your browser:'}
${verificationUrl}
${isZh ? '此链接将在24小时后过期。' : 'This link will expire in 24 hours.'}
${isZh ? '如果您没有注册Aiartools账户,请忽略此邮件。' : 'If you did not sign up for Aiartools, please ignore this email.'}
`;
}
export function generatePasswordResetEmailHtml(resetUrl: string, locale: string = 'zh'): string {
const isZh = locale === 'zh';
return `