emailService.js 5.9 KB

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