0.0.8.0
This commit is contained in:
Executable
+101
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* 邮件发送测试脚本
|
||||
* 用于测试 SMTP 配置是否正确
|
||||
*
|
||||
* 使用方法:
|
||||
* node scripts/test-email.js <收件人邮箱>
|
||||
*/
|
||||
|
||||
require('dotenv').config()
|
||||
const nodemailer = require('nodemailer')
|
||||
|
||||
// 获取命令行参数
|
||||
const recipientEmail = process.argv[2]
|
||||
|
||||
if (!recipientEmail) {
|
||||
console.error('❌ 错误:请提供收件人邮箱地址')
|
||||
console.log('使用方法:node scripts/test-email.js <收件人邮箱>')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// 验证环境变量
|
||||
const requiredEnvVars = ['SMTP_HOST', 'SMTP_PORT', 'SMTP_USER', 'SMTP_PASS', 'SMTP_FROM']
|
||||
const missingVars = requiredEnvVars.filter(varName => !process.env[varName])
|
||||
|
||||
if (missingVars.length > 0) {
|
||||
console.error('❌ 错误:缺少必要的环境变量:')
|
||||
missingVars.forEach(varName => console.error(` - ${varName}`))
|
||||
console.log('\n请检查 .env 文件配置')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// 显示配置信息
|
||||
console.log('📧 邮件配置信息:')
|
||||
console.log(` SMTP 服务器: ${process.env.SMTP_HOST}`)
|
||||
console.log(` SMTP 端口: ${process.env.SMTP_PORT}`)
|
||||
console.log(` 发件人: ${process.env.SMTP_FROM}`)
|
||||
console.log(` 收件人: ${recipientEmail}`)
|
||||
console.log('')
|
||||
|
||||
// 创建传输器
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: parseInt(process.env.SMTP_PORT || '465'),
|
||||
secure: true,
|
||||
auth: {
|
||||
user: process.env.SMTP_USER,
|
||||
pass: process.env.SMTP_PASS,
|
||||
},
|
||||
})
|
||||
|
||||
// 测试邮件内容
|
||||
const mailOptions = {
|
||||
from: process.env.SMTP_FROM,
|
||||
to: recipientEmail,
|
||||
subject: '[测试] 中华家谱邮件服务测试',
|
||||
html: `
|
||||
<div style="font-family: sans-serif; padding: 20px; max-width: 600px;">
|
||||
<h2 style="color: #2563eb;">✅ 邮件服务测试成功</h2>
|
||||
<p>您好,</p>
|
||||
<p>这是一封来自 <strong>中华家谱系统</strong> 的测试邮件。</p>
|
||||
|
||||
<div style="background: #f3f4f6; padding: 15px; border-radius: 8px; margin: 20px 0;">
|
||||
<h3 style="margin-top: 0;">配置信息</h3>
|
||||
<ul style="list-style: none; padding: 0;">
|
||||
<li>📧 SMTP 服务器: ${process.env.SMTP_HOST}</li>
|
||||
<li>🔌 端口: ${process.env.SMTP_PORT}</li>
|
||||
<li>👤 发件账号: ${process.env.SMTP_USER}</li>
|
||||
<li>⏰ 发送时间: ${new Date().toLocaleString('zh-CN')}</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p>如果您收到这封邮件,说明邮件服务配置正确,可以正常发送邮件。</p>
|
||||
|
||||
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 30px 0;">
|
||||
|
||||
<p style="color: #6b7280; font-size: 12px;">
|
||||
此邮件由中华家谱系统自动发送,请勿回复。<br>
|
||||
如有问题,请联系系统管理员。
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
}
|
||||
|
||||
// 发送测试邮件
|
||||
console.log('📤 正在发送测试邮件...')
|
||||
|
||||
transporter.sendMail(mailOptions, (error, info) => {
|
||||
if (error) {
|
||||
console.error('❌ 邮件发送失败:')
|
||||
console.error(error)
|
||||
process.exit(1)
|
||||
} else {
|
||||
console.log('✅ 邮件发送成功!')
|
||||
console.log(` Message ID: ${info.messageId}`)
|
||||
console.log(` Response: ${info.response}`)
|
||||
console.log('')
|
||||
console.log('请检查收件箱(可能在垃圾邮件中)')
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user