emailService.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const nodemailer = require('nodemailer');
  2. // 邮件配置 - 请根据实际情况修改
  3. const emailConfig = {
  4. // 使用QQ邮箱示例,您可以根据需要修改为其他邮箱服务
  5. service: 'qq', // 或者使用 'gmail', '163', 'outlook' 等
  6. auth: {
  7. user: 'your-email@qq.com', // 发送邮箱 - 请修改为实际邮箱
  8. pass: 'your-app-password' // 邮箱授权码(不是登录密码)- 请修改为实际授权码
  9. }
  10. };
  11. // 接收邮箱配置 - 请修改为实际接收邮箱
  12. const receiverEmail = 'admin@yourcompany.com'; // 接收留言的邮箱
  13. // 创建邮件传输器
  14. const transporter = nodemailer.createTransport(emailConfig);
  15. /**
  16. * 发送联系我们留言通知邮件
  17. * @param {Object} contactData 联系信息
  18. * @returns {Promise<boolean>} 发送结果
  19. */
  20. async function sendContactNotification(contactData) {
  21. try {
  22. const { name, email, phone, company, subject, message, createdAt } = contactData;
  23. const mailOptions = {
  24. from: emailConfig.auth.user,
  25. to: receiverEmail,
  26. subject: `【网站留言】${subject}`,
  27. html: `
  28. <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
  29. <h2 style="color: #333; border-bottom: 2px solid #4CAF50; padding-bottom: 10px;">
  30. 新的联系我们留言
  31. </h2>
  32. <div style="background-color: #f9f9f9; padding: 20px; border-radius: 5px; margin: 20px 0;">
  33. <h3 style="color: #555; margin-top: 0;">联系人信息</h3>
  34. <p><strong>姓名:</strong> ${name}</p>
  35. <p><strong>邮箱:</strong> <a href="mailto:${email}">${email}</a></p>
  36. ${phone ? `<p><strong>电话:</strong> ${phone}</p>` : ''}
  37. ${company ? `<p><strong>公司:</strong> ${company}</p>` : ''}
  38. <p><strong>留言时间:</strong> ${new Date(createdAt).toLocaleString('zh-CN')}</p>
  39. </div>
  40. <div style="background-color: #fff; padding: 20px; border: 1px solid #ddd; border-radius: 5px;">
  41. <h3 style="color: #555; margin-top: 0;">留言主题</h3>
  42. <p style="font-size: 16px; font-weight: bold; color: #333;">${subject}</p>
  43. <h3 style="color: #555;">留言内容</h3>
  44. <div style="background-color: #f5f5f5; padding: 15px; border-radius: 3px; line-height: 1.6;">
  45. ${message.replace(/\n/g, '<br>')}
  46. </div>
  47. </div>
  48. <div style="margin-top: 20px; padding: 15px; background-color: #e8f5e8; border-radius: 5px;">
  49. <p style="margin: 0; color: #666; font-size: 14px;">
  50. <strong>提示:</strong>请及时回复客户留言,可直接回复到客户邮箱:<a href="mailto:${email}">${email}</a>
  51. </p>
  52. </div>
  53. </div>
  54. `
  55. };
  56. const result = await transporter.sendMail(mailOptions);
  57. console.log('邮件发送成功:', result.messageId);
  58. return true;
  59. } catch (error) {
  60. console.error('邮件发送失败:', error);
  61. return false;
  62. }
  63. }
  64. /**
  65. * 发送自动回复邮件给客户
  66. * @param {Object} contactData 联系信息
  67. * @returns {Promise<boolean>} 发送结果
  68. */
  69. async function sendAutoReply(contactData) {
  70. try {
  71. const { name, email, subject } = contactData;
  72. const mailOptions = {
  73. from: emailConfig.auth.user,
  74. to: email,
  75. subject: `感谢您的留言 - ${subject}`,
  76. html: `
  77. <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
  78. <h2 style="color: #4CAF50;">感谢您的留言!</h2>
  79. <p>尊敬的 ${name},</p>
  80. <p>感谢您通过我们的网站联系我们。我们已经收到您的留言,我们的工作人员会在24小时内回复您。</p>
  81. <div style="background-color: #f9f9f9; padding: 15px; border-radius: 5px; margin: 20px 0;">
  82. <p><strong>您的留言主题:</strong>${subject}</p>
  83. <p><strong>提交时间:</strong>${new Date().toLocaleString('zh-CN')}</p>
  84. </div>
  85. <p>如有紧急事务,请直接拨打我们的客服电话:<strong>400-xxx-xxxx</strong></p>
  86. <hr style="border: none; border-top: 1px solid #eee; margin: 30px 0;">
  87. <p style="color: #666; font-size: 14px;">
  88. 此邮件为系统自动发送,请勿直接回复。<br>
  89. 如需联系我们,请访问:<a href="https://yourwebsite.com">https://yourwebsite.com</a>
  90. </p>
  91. </div>
  92. `
  93. };
  94. const result = await transporter.sendMail(mailOptions);
  95. console.log('自动回复邮件发送成功:', result.messageId);
  96. return true;
  97. } catch (error) {
  98. console.error('自动回复邮件发送失败:', error);
  99. return false;
  100. }
  101. }
  102. module.exports = {
  103. sendContactNotification,
  104. sendAutoReply
  105. };