60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import nodemailer from 'nodemailer'
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
host: process.env.SMTP_HOST,
|
|
port: parseInt(process.env.SMTP_PORT || '465'),
|
|
secure: true, // true for 465, false for other ports
|
|
auth: {
|
|
user: process.env.SMTP_USER,
|
|
pass: process.env.SMTP_PASS,
|
|
},
|
|
})
|
|
|
|
export async function sendInvitationEmail(
|
|
to: string,
|
|
inviterName: string,
|
|
treeName: string,
|
|
role: string,
|
|
password?: string
|
|
) {
|
|
const loginUrl = `${process.env.NEXTAUTH_URL}/auth/signin`
|
|
const roleText = role === 'EDITOR' ? '编辑者' : '查看者'
|
|
|
|
let htmlContent = `
|
|
<div style="font-family: sans-serif; padding: 20px;">
|
|
<h2>家族树协作邀请</h2>
|
|
<p>您好,</p>
|
|
<p><strong>${inviterName}</strong> 邀请您作为 <strong>${roleText}</strong> 加入家族树 <strong>"${treeName}"</strong>。</p>
|
|
`
|
|
|
|
if (password) {
|
|
htmlContent += `
|
|
<p>您的初始登录密码为:<br>
|
|
<strong style="font-size: 18px; background: #eee; padding: 5px 10px; border-radius: 4px;">${password}</strong>
|
|
</p>
|
|
<p>请使用您的邮箱 (${to}) 和上述密码登录。</p>
|
|
`
|
|
} else {
|
|
htmlContent += `
|
|
<p>您可以使用现有的账号登录查看。</p>
|
|
`
|
|
}
|
|
|
|
htmlContent += `
|
|
<p>
|
|
<a href="${loginUrl}" style="display: inline-block; background: #2563eb; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">
|
|
立即登录
|
|
</a>
|
|
</p>
|
|
<p style="color: #666; font-size: 12px; margin-top: 30px;">此邮件由中华家谱系统自动发送,请勿回复。</p>
|
|
</div>
|
|
`
|
|
|
|
await transporter.sendMail({
|
|
from: process.env.SMTP_FROM,
|
|
to,
|
|
subject: `[中华家谱] ${inviterName} 邀请您协作`,
|
|
html: htmlContent,
|
|
})
|
|
}
|