Webflow电商环境下基于EasyPost API开发追踪号自动邮件程序的步骤
实现步骤
1. 配置EasyPost Webhook触发事件
- 登录EasyPost后台,进入Webhooks设置页面
- 添加新Webhook,选择触发事件:选
shipment.created对应运单生成时触发,选tracker.updated更适合包裹发出且追踪号可用的场景 - 填写后续搭建的后端服务接收URL(比如
https://your-server.com/easypost-webhook),保存配置
2. 搭建简易后端服务(Node.js/Express为例)
- 初始化项目:
mkdir easypost-tracker-email && cd easypost-tracker-email npm init -y npm install express body-parser nodemailer dotenv - 创建
server.js文件,编写核心逻辑:require('dotenv').config(); const express = require('express'); const bodyParser = require('body-parser'); const nodemailer = require('nodemailer'); const app = express(); app.use(bodyParser.json()); // 配置邮件发送器(以Gmail为例,可替换其他邮箱服务) const transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS // Gmail需开启应用专用密码 } }); // 处理EasyPost Webhook请求 app.post('/easypost-webhook', async (req, res) => { const webhookData = req.body; // 提取关键信息:买家邮箱、追踪号、运单ID const buyerEmail = webhookData.result.buyer_address.email; const trackingNumber = webhookData.result.tracking_code; const shipmentId = webhookData.result.id; // 跳过EasyPost的测试Webhook请求 if (webhookData.description === 'Test Webhook') { return res.status(200).send('Test received'); } // 构造邮件内容 const mailOptions = { from: process.env.EMAIL_USER, to: buyerEmail, subject: `你的包裹追踪信息已更新`, text: `您好!你的包裹已发出,追踪号:${trackingNumber}\n可通过对应快递官网查询物流状态。运单ID:${shipmentId}` }; try { await transporter.sendMail(mailOptions); console.log(`追踪号已发送至 ${buyerEmail}`); res.status(200).send('Email sent successfully'); } catch (error) { console.error('发送邮件失败:', error); res.status(500).send('Failed to send email'); } }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`服务运行在端口 ${PORT}`); }); - 创建
.env文件,填写敏感信息:EMAIL_USER=你的发件邮箱地址 EMAIL_PASS=邮箱应用专用密码(或对应服务商允许的密码)
3. 本地测试
- 启动服务:
node server.js - 用ngrok生成临时公网URL(让EasyPost能把Webhook请求发到本地):
ngrok http 3000 - 把ngrok生成的URL(比如
https://xxxx-xx-xx-xx-xx.ngrok.io)加上/easypost-webhook,更新EasyPost的Webhook URL - 在EasyPost后台触发测试Webhook,检查服务是否收到请求,同时验证邮件是否正常发送
4. 部署后端服务
- 可选平台:Vercel、Netlify Functions、Heroku,以Vercel为例:
- 把代码上传到GitHub仓库
- 登录Vercel,导入该仓库,按提示部署(Vercel会自动识别Node.js项目)
- 部署完成后,获取Vercel分配的域名,更新EasyPost的Webhook URL为
https://your-vercel-domain.vercel.app/easypost-webhook - 在Vercel的环境变量设置中添加
.env里的EMAIL_USER和EMAIL_PASS
5. 验证生产环境流程
- 在Webflow后台创建测试订单并生成运单
- 查看EasyPost的Webhook日志,确认请求已发送到你的服务
- 检查测试买家邮箱,确认收到追踪号邮件
内容的提问来源于stack exchange,提问作者Jedd Soh
相关产品推荐
相关产品推荐

