123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- const nodemailer = require('nodemailer');
- // 邮件配置 - 请根据实际情况修改
- const emailConfig = {
- // 使用QQ邮箱示例,您可以根据需要修改为其他邮箱服务
- service: 'qq', // 或者使用 'gmail', '163', 'outlook' 等
- auth: {
- user: 'your-email@qq.com', // 发送邮箱 - 请修改为实际邮箱
- pass: 'your-app-password' // 邮箱授权码(不是登录密码)- 请修改为实际授权码
- }
- };
- // 接收邮箱配置 - 请修改为实际接收邮箱
- const receiverEmail = 'admin@yourcompany.com'; // 接收留言的邮箱
- // 创建邮件传输器
- const transporter = nodemailer.createTransport(emailConfig);
- /**
- * 发送联系我们留言通知邮件
- * @param {Object} contactData 联系信息
- * @returns {Promise<boolean>} 发送结果
- */
- async function sendContactNotification(contactData) {
- try {
- 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 {
- 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
- };
|