emailService.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. const nodemailer = require('nodemailer');
  2. // 邮件配置 - 使用环境变量或默认配置
  3. // 根据QQ邮箱官方文档配置SMTP服务器
  4. const emailConfig = {
  5. host: process.env.EMAIL_HOST || 'smtp.qq.com',
  6. port: process.env.EMAIL_PORT || 587, // 使用STARTTLS的端口
  7. secure: false, // 587端口使用STARTTLS
  8. auth: {
  9. user: process.env.EMAIL_USER || '739239437@qq.com', // QQ邮箱完整地址
  10. pass: process.env.EMAIL_PASS || 'zyggcylcanbgbddc' // QQ邮箱授权码(不是QQ密码)
  11. },
  12. tls: {
  13. rejectUnauthorized: false
  14. }
  15. };
  16. // 接收邮箱配置
  17. const receiverEmail = process.env.ADMIN_EMAIL || '1661073856@qq.com';
  18. // 检查邮件配置是否有效
  19. // 只要有邮箱用户名和密码/授权码就认为配置有效
  20. const isEmailConfigured = emailConfig.auth.user &&
  21. emailConfig.auth.pass &&
  22. emailConfig.auth.user.trim() !== '' &&
  23. emailConfig.auth.pass.trim() !== '';
  24. // 创建邮件传输器(仅在配置有效时)
  25. const transporter = isEmailConfigured ? nodemailer.createTransport(emailConfig) : null;
  26. console.log('邮件服务配置状态:', {
  27. configured: isEmailConfigured,
  28. host: emailConfig.host,
  29. port: emailConfig.port,
  30. user: emailConfig.auth.user,
  31. hasPassword: !!emailConfig.auth.pass
  32. });
  33. /**
  34. * 发送联系我们留言通知邮件
  35. * @param {Object} contactData 联系信息
  36. * @returns {Promise<boolean>} 发送结果
  37. */
  38. async function sendContactNotification(contactData) {
  39. try {
  40. // 检查邮件配置是否有效
  41. if (!isEmailConfigured || !transporter) {
  42. console.log('邮件服务未配置,跳过发送通知邮件');
  43. return false;
  44. }
  45. const { name, email, phone, company, subject, message, createdAt } = contactData;
  46. const mailOptions = {
  47. from: emailConfig.auth.user,
  48. to: receiverEmail,
  49. subject: `【网站留言】${subject}`,
  50. html: `
  51. <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
  52. <h2 style="color: #333; border-bottom: 2px solid #4CAF50; padding-bottom: 10px;">
  53. 新的联系我们留言
  54. </h2>
  55. <div style="background-color: #f9f9f9; padding: 20px; border-radius: 5px; margin: 20px 0;">
  56. <h3 style="color: #555; margin-top: 0;">联系人信息</h3>
  57. <p><strong>姓名:</strong> ${name}</p>
  58. <p><strong>邮箱:</strong> <a href="mailto:${email}">${email}</a></p>
  59. ${phone ? `<p><strong>电话:</strong> ${phone}</p>` : ''}
  60. ${company ? `<p><strong>公司:</strong> ${company}</p>` : ''}
  61. <p><strong>留言时间:</strong> ${new Date(createdAt).toLocaleString('zh-CN')}</p>
  62. </div>
  63. <div style="background-color: #fff; padding: 20px; border: 1px solid #ddd; border-radius: 5px;">
  64. <h3 style="color: #555; margin-top: 0;">留言主题</h3>
  65. <p style="font-size: 16px; font-weight: bold; color: #333;">${subject}</p>
  66. <h3 style="color: #555;">留言内容</h3>
  67. <div style="background-color: #f5f5f5; padding: 15px; border-radius: 3px; line-height: 1.6;">
  68. ${message.replace(/\n/g, '<br>')}
  69. </div>
  70. </div>
  71. <div style="margin-top: 20px; padding: 15px; background-color: #e8f5e8; border-radius: 5px;">
  72. <p style="margin: 0; color: #666; font-size: 14px;">
  73. <strong>提示:</strong>请及时回复客户留言,可直接回复到客户邮箱:<a href="mailto:${email}">${email}</a>
  74. </p>
  75. </div>
  76. </div>
  77. `
  78. };
  79. const result = await transporter.sendMail(mailOptions);
  80. console.log('邮件发送成功:', result.messageId);
  81. return true;
  82. } catch (error) {
  83. console.error('邮件发送失败:', error);
  84. return false;
  85. }
  86. }
  87. /**
  88. * 发送自动回复邮件给客户
  89. * @param {Object} contactData 联系信息
  90. * @returns {Promise<boolean>} 发送结果
  91. */
  92. async function sendAutoReply(contactData) {
  93. try {
  94. // 检查邮件配置是否有效
  95. if (!isEmailConfigured || !transporter) {
  96. console.log('邮件服务未配置,跳过发送自动回复邮件');
  97. return false;
  98. }
  99. const { name, email, subject } = contactData;
  100. const mailOptions = {
  101. from: emailConfig.auth.user,
  102. to: email,
  103. subject: `感谢您的留言 - ${subject}`,
  104. html: `
  105. <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
  106. <h2 style="color: #4CAF50;">感谢您的留言!</h2>
  107. <p>尊敬的 ${name},</p>
  108. <p>感谢您通过我们的网站联系我们。我们已经收到您的留言,我们的工作人员会在24小时内回复您。</p>
  109. <div style="background-color: #f9f9f9; padding: 15px; border-radius: 5px; margin: 20px 0;">
  110. <p><strong>您的留言主题:</strong>${subject}</p>
  111. <p><strong>提交时间:</strong>${new Date().toLocaleString('zh-CN')}</p>
  112. </div>
  113. <p>如有紧急事务,请直接拨打我们的客服电话:<strong>400-xxx-xxxx</strong></p>
  114. <hr style="border: none; border-top: 1px solid #eee; margin: 30px 0;">
  115. <p style="color: #666; font-size: 14px;">
  116. 此邮件为系统自动发送,请勿直接回复。<br>
  117. 如需联系我们,请访问:<a href="https://yourwebsite.com">https://yourwebsite.com</a>
  118. </p>
  119. </div>
  120. `
  121. };
  122. const result = await transporter.sendMail(mailOptions);
  123. console.log('自动回复邮件发送成功:', result.messageId);
  124. return true;
  125. } catch (error) {
  126. console.error('自动回复邮件发送失败:', error);
  127. return false;
  128. }
  129. }
  130. module.exports = {
  131. sendContactNotification,
  132. sendAutoReply
  133. };