123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- const nodemailer = require('nodemailer');
- // 邮件配置 - 使用环境变量或默认配置
- // 根据QQ邮箱官方文档配置SMTP服务器
- const emailConfig = {
- host: process.env.EMAIL_HOST || 'smtp.qq.com',
- port: process.env.EMAIL_PORT || 587, // 使用STARTTLS的端口
- secure: false, // 587端口使用STARTTLS
- auth: {
- user: process.env.EMAIL_USER || '739239437@qq.com', // QQ邮箱完整地址
- pass: process.env.EMAIL_PASS || 'zyggcylcanbgbddc' // QQ邮箱授权码(不是QQ密码)
- },
- tls: {
- rejectUnauthorized: false
- }
- };
- // 接收邮箱配置
- const receiverEmail = process.env.ADMIN_EMAIL || '1661073856@qq.com';
- // 检查邮件配置是否有效
- // 只要有邮箱用户名和密码/授权码就认为配置有效
- const isEmailConfigured = emailConfig.auth.user &&
- emailConfig.auth.pass &&
- emailConfig.auth.user.trim() !== '' &&
- emailConfig.auth.pass.trim() !== '';
- // 创建邮件传输器(仅在配置有效时)
- const transporter = isEmailConfigured ? nodemailer.createTransport(emailConfig) : null;
- console.log('邮件服务配置状态:', {
- configured: isEmailConfigured,
- host: emailConfig.host,
- port: emailConfig.port,
- user: emailConfig.auth.user,
- hasPassword: !!emailConfig.auth.pass
- });
- /**
- * 发送联系我们留言通知邮件
- * @param {Object} contactData 联系信息
- * @returns {Promise<boolean>} 发送结果
- */
- async function sendContactNotification(contactData) {
- try {
- // 检查邮件配置是否有效
- if (!isEmailConfigured || !transporter) {
- console.log('邮件服务未配置,跳过发送通知邮件');
- return false;
- }
- const { name, email, phone, company, subject, message, createdAt } = contactData;
-
- const mailOptions = {
- from: emailConfig.auth.user,
- to: receiverEmail,
- subject: `【网站留言】${subject}`,
- html: `
- <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
- <h2 style="color: #333; border-bottom: 2px solid #4CAF50; padding-bottom: 10px;">
- 新的联系我们留言
- </h2>
-
- <div style="background-color: #f9f9f9; padding: 20px; border-radius: 5px; margin: 20px 0;">
- <h3 style="color: #555; margin-top: 0;">联系人信息</h3>
- <p><strong>姓名:</strong> ${name}</p>
- <p><strong>邮箱:</strong> <a href="mailto:${email}">${email}</a></p>
- ${phone ? `<p><strong>电话:</strong> ${phone}</p>` : ''}
- ${company ? `<p><strong>公司:</strong> ${company}</p>` : ''}
- <p><strong>留言时间:</strong> ${new Date(createdAt).toLocaleString('zh-CN')}</p>
- </div>
-
- <div style="background-color: #fff; padding: 20px; border: 1px solid #ddd; border-radius: 5px;">
- <h3 style="color: #555; margin-top: 0;">留言主题</h3>
- <p style="font-size: 16px; font-weight: bold; color: #333;">${subject}</p>
-
- <h3 style="color: #555;">留言内容</h3>
- <div style="background-color: #f5f5f5; padding: 15px; border-radius: 3px; line-height: 1.6;">
- ${message.replace(/\n/g, '<br>')}
- </div>
- </div>
-
- <div style="margin-top: 20px; padding: 15px; background-color: #e8f5e8; border-radius: 5px;">
- <p style="margin: 0; color: #666; font-size: 14px;">
- <strong>提示:</strong>请及时回复客户留言,可直接回复到客户邮箱:<a href="mailto:${email}">${email}</a>
- </p>
- </div>
- </div>
- `
- };
- const result = await transporter.sendMail(mailOptions);
- console.log('邮件发送成功:', result.messageId);
- return true;
- } catch (error) {
- console.error('邮件发送失败:', error);
- return false;
- }
- }
- /**
- * 发送自动回复邮件给客户
- * @param {Object} contactData 联系信息
- * @returns {Promise<boolean>} 发送结果
- */
- async function sendAutoReply(contactData) {
- try {
- // 检查邮件配置是否有效
- if (!isEmailConfigured || !transporter) {
- console.log('邮件服务未配置,跳过发送自动回复邮件');
- return false;
- }
- const { name, email, subject } = contactData;
-
- const mailOptions = {
- from: emailConfig.auth.user,
- to: email,
- subject: `感谢您的留言 - ${subject}`,
- html: `
- <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
- <h2 style="color: #4CAF50;">感谢您的留言!</h2>
-
- <p>尊敬的 ${name},</p>
-
- <p>感谢您通过我们的网站联系我们。我们已经收到您的留言,我们的工作人员会在24小时内回复您。</p>
-
- <div style="background-color: #f9f9f9; padding: 15px; border-radius: 5px; margin: 20px 0;">
- <p><strong>您的留言主题:</strong>${subject}</p>
- <p><strong>提交时间:</strong>${new Date().toLocaleString('zh-CN')}</p>
- </div>
-
- <p>如有紧急事务,请直接拨打我们的客服电话:<strong>400-xxx-xxxx</strong></p>
-
- <hr style="border: none; border-top: 1px solid #eee; margin: 30px 0;">
-
- <p style="color: #666; font-size: 14px;">
- 此邮件为系统自动发送,请勿直接回复。<br>
- 如需联系我们,请访问:<a href="https://yourwebsite.com">https://yourwebsite.com</a>
- </p>
- </div>
- `
- };
- const result = await transporter.sendMail(mailOptions);
- console.log('自动回复邮件发送成功:', result.messageId);
- return true;
- } catch (error) {
- console.error('自动回复邮件发送失败:', error);
- return false;
- }
- }
- module.exports = {
- sendContactNotification,
- sendAutoReply
- };
|