import nodemailer from 'nodemailer'; const BASE_URL = process.env.BASE_URL || 'https://www.life-kline.com'; // Create transporter with config const transporter = nodemailer.createTransport({ host: process.env.MAIL_SMTP_HOST || 'mail.life-kline.cn', port: parseInt(process.env.MAIL_SMTP_PORT) || 25, secure: process.env.MAIL_SMTP_SECURE === 'true', tls: { rejectUnauthorized: false }, auth: { user: process.env.MAIL_FROM || 'code@life-kline.com', pass: process.env.MAIL_PASSWORD } }); // Email template wrapper with branding and footer function emailTemplate(content, title = '比迹') { return ` ${title}

比迹

命理加强版

${content}

这是一封来自比迹的系统邮件,请勿直接回复。

如有任何问题,请联系我们的客服团队。

取消订阅 | 访问网站

© ${new Date().getFullYear()} 比迹. All rights reserved.

`.trim(); } // Button component for emails function buttonHTML(text, url, color = '#667eea') { return `
${text}
`; } // Base send function export async function sendEmail(to, subject, html, text) { try { const info = await transporter.sendMail({ from: `"比迹" <${process.env.MAIL_FROM || 'code@life-kline.com'}>`, to, subject: `比迹命理加强版 - ${subject}`, html, text }); console.log('Email sent successfully:', info.messageId); return { success: true, messageId: info.messageId }; } catch (error) { console.error('Error sending email:', error); throw error; } } // Send verification email export async function sendVerificationEmail(email, token) { const verificationUrl = `${BASE_URL}/verify-email?token=${token}`; const content = `

验证您的邮箱

您好!

感谢您注册人生k线。请点击下方按钮验证您的邮箱地址:

${buttonHTML('验证邮箱', verificationUrl)}

此验证链接将在 24小时 后失效。如果您没有注册人生k线账户,请忽略此邮件。

如果按钮无法点击,请复制以下链接到浏览器访问:
${verificationUrl}

`; const text = ` 验证您的邮箱 您好! 感谢您注册人生k线。请访问以下链接验证您的邮箱地址: ${verificationUrl} 此验证链接将在24小时后失效。如果您没有注册人生k线账户,请忽略此邮件。 `.trim(); return sendEmail(email, '验证您的邮箱', emailTemplate(content), text); } // Send password reset email export async function sendPasswordResetEmail(email, token) { const resetUrl = `${BASE_URL}/reset-password?token=${token}`; const content = `

重置您的密码

您好!

我们收到了重置您账户密码的请求。请点击下方按钮重置密码:

${buttonHTML('重置密码', resetUrl, '#e74c3c')}

此重置链接将在 1小时 后失效。如果您没有请求重置密码,请忽略此邮件,您的账户仍然安全。

如果按钮无法点击,请复制以下链接到浏览器访问:
${resetUrl}

`; const text = ` 重置您的密码 您好! 我们收到了重置您账户密码的请求。请访问以下链接重置密码: ${resetUrl} 此重置链接将在1小时后失效。如果您没有请求重置密码,请忽略此邮件,您的账户仍然安全。 `.trim(); return sendEmail(email, '重置密码', emailTemplate(content), text); } // Send welcome email export async function sendWelcomeEmail(email) { const loginUrl = `${BASE_URL}/login`; const content = `

欢迎加入人生k线!

您好!

欢迎加入人生k线命理加强版!我们很高兴您成为我们的一员。

您可以使用以下功能:

新用户奖励:我们已为您准备了初始积分,快去探索吧!

${buttonHTML('立即开始', loginUrl)}

祝您使用愉快!

`; const text = ` 欢迎加入人生k线! 您好! 欢迎加入人生k线命理加强版!我们很高兴您成为我们的一员。 您可以使用以下功能: - 创建和管理您的命理档案 - 查看每日、每月、每年运势 - 获取个性化的命理分析 - 设置运势提醒,不错过重要时刻 新用户奖励:我们已为您准备了初始积分,快去探索吧! 立即访问:${loginUrl} 祝您使用愉快! `.trim(); return sendEmail(email, '欢迎加入人生k线!', emailTemplate(content), text); } // Send fortune reminder export async function sendFortuneReminder(email, type, profileName) { const loginUrl = `${BASE_URL}/fortune`; const typeMap = { daily: { title: '每日运势', emoji: '📅', desc: '新的一天开始了' }, monthly: { title: '每月运势', emoji: '📆', desc: '新的月份已经到来' }, yearly: { title: '流年运势', emoji: '🎊', desc: '新的一年开启新篇章' }, birthday: { title: '生日运势', emoji: '🎂', desc: '祝您生日快乐' } }; const typeInfo = typeMap[type] || typeMap.daily; const content = `

${typeInfo.emoji} ${typeInfo.title}提醒

您好,${profileName}

${typeInfo.desc},您的${typeInfo.title}已更新,快来查看吧!

了解运势,把握机遇

点击下方按钮查看详细运势分析

${buttonHTML('查看运势', loginUrl)}

祝您好运常伴!

`; const text = ` ${typeInfo.title}提醒 您好,${profileName}! ${typeInfo.desc},您的${typeInfo.title}已更新,快来查看吧! 了解运势,把握机遇 访问链接查看详细运势分析:${loginUrl} 祝您好运常伴! `.trim(); return sendEmail(email, `${typeInfo.title}提醒`, emailTemplate(content), text); } // Send low points reminder export async function sendLowPointsReminder(email, currentPoints) { const rechargeUrl = `${BASE_URL}/recharge`; const content = `

您的积分即将耗尽

您好!

您的账户积分即将不足,可能会影响您继续使用人生k线的服务。

当前剩余积分

${currentPoints}

为了不影响您的使用体验,建议您及时充值。我们提供多种充值方案供您选择。

${buttonHTML('立即充值', rechargeUrl, '#ffc107')}

感谢您对人生k线的支持!

`; const text = ` 您的积分即将耗尽 您好! 您的账户积分即将不足,可能会影响您继续使用人生k线的服务。 当前剩余积分:${currentPoints} 为了不影响您的使用体验,建议您及时充值。我们提供多种充值方案供您选择。 访问充值页面:${rechargeUrl} 感谢您对人生k线的支持! `.trim(); return sendEmail(email, '您的积分即将耗尽', emailTemplate(content), text); } // Send feature update email export async function sendFeatureUpdateEmail(email, features) { const loginUrl = `${BASE_URL}/login`; const featuresList = features.map(feature => `

${feature.icon || '✨'} ${feature.title}

${feature.description}

`).join(''); const content = `

人生k线有新功能啦!

您好!

我们很高兴地通知您,人生k线推出了新功能,为您带来更好的使用体验!

${featuresList}

快来体验这些全新功能吧!

${buttonHTML('立即体验', loginUrl)}

感谢您一直以来的支持!

`; const featuresText = features.map(f => `- ${f.title}\n ${f.description}`).join('\n\n'); const text = ` 人生k线有新功能啦! 您好! 我们很高兴地通知您,人生k线推出了新功能,为您带来更好的使用体验! 新功能列表: ${featuresText} 快来体验这些全新功能吧! 访问链接:${loginUrl} 感谢您一直以来的支持! `.trim(); return sendEmail(email, '人生k线有新功能啦!', emailTemplate(content), text); } // Send promotional email export async function sendPromotionalEmail(email, promotion) { const promotionUrl = `${BASE_URL}/promotion/${promotion.id}`; const content = `

${promotion.title}

您好!

${promotion.banner ? `
${promotion.title}
` : ''}

${promotion.description}

${promotion.highlights ? `

活动亮点:

` : ''} ${promotion.validUntil ? `

活动截止时间:${new Date(promotion.validUntil).toLocaleDateString('zh-CN')}

` : ''} ${buttonHTML(promotion.buttonText || '立即参与', promotionUrl, '#f39c12')}

机会难得,不容错过!

`; const highlightsText = promotion.highlights ? promotion.highlights.map(h => `- ${h}`).join('\n') : ''; const text = ` ${promotion.title} 您好! ${promotion.description} ${highlightsText ? `活动亮点:\n${highlightsText}\n\n` : ''}${promotion.validUntil ? `活动截止时间:${new Date(promotion.validUntil).toLocaleDateString('zh-CN')}\n\n` : ''}访问链接:${promotionUrl} 机会难得,不容错过! `.trim(); return sendEmail(email, `[活动] ${promotion.title}`, emailTemplate(content), text); } // Verify transporter configuration transporter.verify((error, success) => { if (error) { console.error('SMTP configuration error:', error); } else { console.log('Email service is ready to send emails'); } }); export default { sendEmail, sendVerificationEmail, sendPasswordResetEmail, sendWelcomeEmail, sendFortuneReminder, sendLowPointsReminder, sendFeatureUpdateEmail, sendPromotionalEmail };