index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import express from 'express';
  2. import nodemailer from 'nodemailer';
  3. import cors from 'cors';
  4. const app = express();
  5. const PORT = process.env.PORT || 3001;
  6. // Middleware
  7. app.use(cors());
  8. app.use(express.json());
  9. // SMTP Configuration
  10. const smtpConfig = {
  11. host: 'smtp-relay.brevo.com',
  12. port: 587,
  13. secure: false, // TLS
  14. auth: {
  15. user: '9e8314001@smtp-brevo.com',
  16. pass: 'xsmtpsib-d4a7d55a9c3d79a2b4d8f5c17b0ac5e47530f0615e7e9945e9fdf97b90487c32-LXW7Hihe7Qb5J2tM'
  17. }
  18. };
  19. // Create transporter
  20. const transporter = nodemailer.createTransport(smtpConfig);
  21. // Welcome email template
  22. const createWelcomeEmail = (customerEmail) => {
  23. return {
  24. from: '"CCDW" <bob.yuxinyang@gmail.com>',
  25. to: customerEmail,
  26. subject: '👋 欢迎来到 CCDW!我们很高兴认识您',
  27. html: `
  28. <!DOCTYPE html>
  29. <html lang="zh-CN">
  30. <head>
  31. <meta charset="UTF-8">
  32. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  33. <title>欢迎来到 CCDW</title>
  34. </head>
  35. <body style="margin: 0; padding: 0; font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif; background-color: #0A192F;">
  36. <table role="presentation" style="width: 100%; border-collapse: collapse;">
  37. <tr>
  38. <td align="center" style="padding: 40px 20px;">
  39. <table role="presentation" style="max-width: 600px; width: 100%; border-collapse: collapse; background-color: #112240; border-radius: 16px; overflow: hidden; box-shadow: 0 4px 24px rgba(0,0,0,0.3);">
  40. <!-- Header -->
  41. <tr>
  42. <td style="padding: 40px 40px 20px; text-align: center; background: linear-gradient(135deg, #112240 0%, #0A192F 100%);">
  43. <h1 style="margin: 0; color: #64FFDA; font-size: 28px; font-weight: bold;">
  44. ✨ CCDW
  45. </h1>
  46. <p style="margin: 10px 0 0; color: #8892b0; font-size: 14px;">
  47. AI智能解决方案
  48. </p>
  49. </td>
  50. </tr>
  51. <!-- Main Content -->
  52. <tr>
  53. <td style="padding: 30px 40px;">
  54. <h2 style="margin: 0 0 20px; color: #e6f1ff; font-size: 22px; font-weight: 600;">
  55. 您好!欢迎认识我们 👋
  56. </h2>
  57. <p style="margin: 0 0 20px; color: #a8b2d1; font-size: 16px; line-height: 1.8;">
  58. 非常高兴收到您的来信!感谢您对 CCDW 的关注。
  59. </p>
  60. <p style="margin: 0 0 20px; color: #a8b2d1; font-size: 16px; line-height: 1.8;">
  61. 我们是一家专注于 AI 智能化解决方案的公司,致力于帮助企业和个人提升工作效率,释放创造力。
  62. </p>
  63. <div style="background-color: #0A192F; border-left: 4px solid #64FFDA; padding: 20px; margin: 25px 0; border-radius: 0 8px 8px 0;">
  64. <p style="margin: 0; color: #64FFDA; font-size: 16px; font-weight: 600;">
  65. 🤔 请问您想了解什么呢?
  66. </p>
  67. <p style="margin: 10px 0 0; color: #8892b0; font-size: 14px; line-height: 1.6;">
  68. 无论是 AI 咨询、智能化转型、还是定制化开发需求,我们都很乐意与您交流探讨!
  69. </p>
  70. </div>
  71. <p style="margin: 0 0 20px; color: #a8b2d1; font-size: 16px; line-height: 1.8;">
  72. 您可以直接回复此邮件,或通过以下方式联系我们:
  73. </p>
  74. </td>
  75. </tr>
  76. <!-- Contact Info -->
  77. <tr>
  78. <td style="padding: 0 40px 30px;">
  79. <table role="presentation" style="width: 100%; border-collapse: collapse; background-color: #0A192F; border-radius: 12px; overflow: hidden;">
  80. <tr>
  81. <td style="padding: 25px;">
  82. <h3 style="margin: 0 0 15px; color: #64FFDA; font-size: 16px; font-weight: 600;">
  83. 📞 联系方式
  84. </h3>
  85. <table role="presentation" style="border-collapse: collapse;">
  86. <tr>
  87. <td style="padding: 8px 0; color: #8892b0; font-size: 14px; width: 80px;">邮箱:</td>
  88. <td style="padding: 8px 0;">
  89. <a href="mailto:bob@ccdw.xyz" style="color: #64FFDA; text-decoration: none; font-size: 14px;">bob@ccdw.xyz</a>
  90. </td>
  91. </tr>
  92. <tr>
  93. <td style="padding: 8px 0; color: #8892b0; font-size: 14px;">电话:</td>
  94. <td style="padding: 8px 0;">
  95. <a href="tel:+8618602109196" style="color: #64FFDA; text-decoration: none; font-size: 14px;">186 0210 9196</a>
  96. </td>
  97. </tr>
  98. <tr>
  99. <td style="padding: 8px 0; color: #8892b0; font-size: 14px;">联系人:</td>
  100. <td style="padding: 8px 0; color: #e6f1ff; font-size: 14px;">Bob</td>
  101. </tr>
  102. </table>
  103. </td>
  104. </tr>
  105. </table>
  106. </td>
  107. </tr>
  108. <!-- Closing -->
  109. <tr>
  110. <td style="padding: 0 40px 40px;">
  111. <p style="margin: 0 0 15px; color: #a8b2d1; font-size: 16px; line-height: 1.8;">
  112. 期待与您的交流! 🚀
  113. </p>
  114. <p style="margin: 0; color: #e6f1ff; font-size: 16px;">
  115. <strong>Bob</strong><br>
  116. <span style="color: #8892b0; font-size: 14px;">CCDW 团队</span>
  117. </p>
  118. </td>
  119. </tr>
  120. <!-- Footer -->
  121. <tr>
  122. <td style="padding: 20px 40px; background-color: #0A192F; border-top: 1px solid #233554; text-align: center;">
  123. <p style="margin: 0; color: #495670; font-size: 12px;">
  124. © 2026 CCDW - 释放AI的力量,创造无限可能
  125. </p>
  126. </td>
  127. </tr>
  128. </table>
  129. </td>
  130. </tr>
  131. </table>
  132. </body>
  133. </html>
  134. `,
  135. text: `
  136. 您好!欢迎认识我们 👋
  137. 非常高兴收到您的来信!感谢您对 CCDW 的关注。
  138. 我们是一家专注于 AI 智能化解决方案的公司,致力于帮助企业和个人提升工作效率,释放创造力。
  139. 🤔 请问您想了解什么呢?
  140. 无论是 AI 咨询、智能化转型、还是定制化开发需求,我们都很乐意与您交流探讨!
  141. 您可以直接回复此邮件,或通过以下方式联系我们:
  142. 📞 联系方式
  143. 邮箱:bob@ccdw.xyz
  144. 电话:186 0210 9196
  145. 联系人:Bob
  146. 期待与您的交流! 🚀
  147. Bob
  148. CCDW 团队
  149. © 2026 CCDW - 释放AI的力量,创造无限可能
  150. `
  151. };
  152. };
  153. // Send welcome email endpoint
  154. app.post('/api/send-welcome-email', async (req, res) => {
  155. try {
  156. const { email } = req.body;
  157. if (!email) {
  158. return res.status(400).json({
  159. success: false,
  160. message: '请提供邮箱地址'
  161. });
  162. }
  163. // Validate email format
  164. const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  165. if (!emailRegex.test(email)) {
  166. return res.status(400).json({
  167. success: false,
  168. message: '邮箱格式不正确'
  169. });
  170. }
  171. const mailOptions = createWelcomeEmail(email);
  172. await transporter.sendMail(mailOptions);
  173. console.log(`✅ Welcome email sent to: ${email}`);
  174. res.json({
  175. success: true,
  176. message: '欢迎邮件已发送!请查收您的邮箱。'
  177. });
  178. } catch (error) {
  179. console.error('❌ Email sending failed:', error);
  180. res.status(500).json({
  181. success: false,
  182. message: '邮件发送失败,请稍后重试。',
  183. error: error.message
  184. });
  185. }
  186. });
  187. // Health check
  188. app.get('/api/health', (req, res) => {
  189. res.json({ status: 'ok', timestamp: new Date().toISOString() });
  190. });
  191. // Serve static files in production
  192. // 假设部署结构:
  193. // /root
  194. // /dist (前端构建产物)
  195. // /server (后端代码)
  196. import path from 'path';
  197. import { fileURLToPath } from 'url';
  198. const __filename = fileURLToPath(import.meta.url);
  199. const __dirname = path.dirname(__filename);
  200. // 任何非 /api 开头的请求,都尝试服务静态文件
  201. // 注意:这应该放在 API 路由定义之后
  202. app.use(express.static(path.join(__dirname, '../dist')));
  203. // SPA Fallback: 所有未匹配的 GET 请求都返回 index.html
  204. app.get('*', (req, res) => {
  205. if (req.path.startsWith('/api')) {
  206. return res.status(404).json({ success: false, message: 'API endpoint not found' });
  207. }
  208. res.sendFile(path.join(__dirname, '../dist/index.html'));
  209. });
  210. // Start server
  211. app.listen(PORT, '0.0.0.0', () => {
  212. console.log(`🚀 Email server running on port ${PORT}`);
  213. });