通过 Node-RED 发送电子邮件
本教程演示如何使用 Node-RED 从 OV20i 摄像头设置自动邮件通知。您将学习配置 Gmail 集成,并创建将检验警报、系统状态更新和故障通知直接发送到您的电子邮箱的工作流。
将构建的内容: 一个自动化邮件系统,使用 Gmail SMTP 将 OV20i 的检验结果、警报和通知发送给指定收件人。
实际应用: 在检验失败时获得即时邮件提醒,向管理层发送每日质量报告,或在系统出现问题时通知维护团队——所有都由您的视觉检验系统全自动完成。
先决条件
- 具备 Node-RED 访问权限的 OV20i 摄像头
- 用于发送邮件的 Gmail 帐户
- 对 Node-RED 流程的基本理解
- 可访问 Google 帐号安全设置
教程概览
我们将构建: 一个能够自动发送包含检验结果和系统警报的邮件通知的 Node-RED 流。
所需时间: 20-30 分钟(含 Gmail 设置)
所学技能: Gmail 应用密码设置、SMTP 配置、自动邮件通知
Step 1: Set Up Gmail App Password
1.1 启用两步验证
- 在 Google 帐户上登录 accounts.google.com
- 在左侧导航菜单中单击“Security”(安全性)
- 找到“Signing in to Google”(Google 登录) 部分
- 单击“2-Step Verification”(两步验证)
- 按照提示启用两步验证(若尚未启用)
在您创建应用密码之前,需要启用两步验证。
1.2 生成应用密码
- 在启用两步验证后返回安全性页面
- 在“Signing in to Google” 下单击“App passwords”
- 将应用类型选择为“Mail”
- 将设备类型选择为“Other”
- 输入一个名称,例如“OV20i Node-RED Email”
- 点击“Generate”
1.3 保存您的应用密码
- 复制 Google 显示的 16 字符密码
- 安全存储——你将在 Node-RED 配置中使用它
- 注意:此密码仅显示一次。若丢失,请重新生成
检查点: 您应已保存一个用于 Node-RED 的 16 字符 Gmail 应用密码。
Step 2: Install Email Nodes (If Needed)
2.1 检查是否存在邮件节点
- 在 OV20i 摄像头上打开 Node-RED
- 在左侧调色板的输出区查找“email”节点
- 如果找不到,则需要安装邮件包
2.2 安装邮件包(如有需要)
- 在 Node-RED 中点击汉堡菜单 (≡)
- 选择“Manage palette”(管理调色板)
- 点击“Install”(安装)标签
- 搜索“node-red-node-email”
- 在该软件包旁点击“Install”(安装)
Step 3: Create Basic Email Flow
3.1 添加所需节点
- 导航到 IO Block > Configure I/O 以访问 Node-RED
- 从调色板将以下节点拖放到画布上:
- Inject 节点(用于测试)
- Email 节点(来自输出部分)
- 将 Inject 输出连接到 Email 输入
3.2 基本流程结构
Inject → Email
流程目的: 用于测试和基本通知的简易邮件发送。
步骤 4:配置电子邮件内容
4.1 设置 Inject Node
- Double-click the inject node to open properties
- Set the payload:
- Payload type: "string"
- Payload value: Your email body text (e.g., "Inspection alert from OV20i")
- Add email subject:
-
Click "+ add" to add a property
-
Property name: "topic"
-
Property value: Your email subject (e.g., "OV20i Inspection Alert")
-
4.2 Example Basic Configuration
Payload (email body):
Inspection completed at Station 1
Status: Alert triggered
Time: Check timestamp for details
Topic (email subject):
OV20i Inspection Alert - Station 1
4.3 Save Inject Configuration
- Give the node a name like "Email Trigger"
- Click "Done" to save the configuration
步骤 5:配置 Email SMTP 设置
5.1 设置 Email Node
- Double-click the email node to open properties
- Configure basic settings:
- Name: "Send Alert Email" (or descriptive name)
- To: Recipient email address (e.g., quality@company.com)
5.2 配置 Gmail SMTP
- Server:
smtp.gmail.com
- Port:
465
- Check "Use secure connection"
- Auth type:
Basic
- Userid: Your full Gmail address (e.g., alerts@company.com)
- Password: The 16-character app password from Step 1
5.3 Security Settings
- Check "Check server certificate is valid"
- Verify all settings are entered correctly
- Click "Done" to save email node configuration
Checkpoint: Your email node should show no error indicators and display the recipient address.
步骤 6:测试您的邮件流程
6.1 部署与测试
- Click "Deploy" button in the top-right corner
- Wait for "Successfully deployed" message
- Click the inject node button (gray square on the left side)
6.2 验证邮件投递
- Check the recipient email for the test message
- Check spam folder if email doesn't appear in inbox
- Look for any error messages in Node-RED debug panel
6.3 如有需要的故障排除
Common issues:
- Wrong app password: Regenerate Gmail app password
- SMTP settings: Verify server and port are correct
- Firewall: Ensure outbound SMTP traffic is allowed
步骤 7:与检测结果的集成
7.1 连接到检测流程
要基于检测结果发送邮件:
- Find your main inspection flow (starts with "All Block Outputs")
- Add your email flow as a branch from inspection processing
- Connect after inspection logic but parallel to final results
7.2 示例集成流程
All Block Outputs → [Inspection Logic] → Final Pass/Fail
↓
Format Email → Send Email
7.3 动态邮件内容
将 inject 节点替换为函数节点以实现动态内容:
// Dynamic email based on inspection results
const result = msg.payload.result ? "PASSED" : "FAILED";
const timestamp = new Date().toLocaleString();
const station = global.get("station_name") || "Unknown Station";
// Set email subject
msg.topic = `Inspection ${result} - ${station}`;
// Set email body
msg.payload = `Inspection Report:
Status: ${result}
Station: ${station}
Time: ${timestamp}
Image: ${msg.payload.image_url || "No image available"}
Please review and take appropriate action.`;
return msg;
Step 8: Complete Flow Example
8.1 导入现成流程
您可以导入此完整的 flow JSON:
[
{
"id": "email_node_1",
"type": "e-mail",
"name": "Send Inspection Alert",
"server": "smtp.gmail.com",
"port": "465",
"secure": true,
"authtype": "BASIC",
"to": "quality@company.com"
},
{
"id": "format_email",
"type": "function",
"name": "Format Email Content",
"func": "const result = msg.payload.result ? 'PASSED' : 'FAILED';\nmsg.topic = `Inspection ${result}`;\nmsg.payload = `Status: ${result}\\nTime: ${new Date()}`;\nreturn msg;"
}
]
8.2 根据需要进行自定义
- 更新贵组织的电子邮件地址
- 修改电子邮件内容以满足您的具体需求
- 调整时序和触发条件
- 在生产部署前进行充分测试
故障排除
常见问题
问题 | 现象 | 解决方案 |
---|---|---|
认证失败 | “Login failed” 错误 | 请验证应用密码是否正确,并启用两步验证 |
连接超时 | 未发送邮件,出现超时错误 | 检查防火墙设置,验证 SMTP 服务器和端口 |
邮件在垃圾邮件中 | 邮件已投递但位于垃圾邮件文件夹 | 将发件人添加到安全列表,优化邮件内容 |
流未触发 | 无调试输出 | 检查流程连接和触发条件 |
调试您的邮件流程
- 在每一步之后添加调试节点以跟踪数据
- 检查 Node-RED 日志以获取详细错误信息
- 如有需要,使用外部邮件客户端测试 SMTP 设置
- 验证相机到 Gmail 服务器的网络连通性
成功!您的邮件集成已完成
现在,您的 OV20i 摄像头可以:
✅ 发送自动化的邮件通知用于检查结果
✅ 传递富文本格式的消息,包含检查细节
✅ 支持多收件人及升级工作流
✅ 提供定期报告及摘要
✅ 基于检查结果进行条件消息处理
最佳实践
邮件管理
- 使用具备清晰状态指示的描述性主题
- 保持信息简洁,但信息量充足
- 包含时间戳和工位标识
- 在通知中提供可执行信息
安全性与可靠性
- 保护应用密码 - 安全存储并定期轮换
- 为系统通知使用专用电子邮件账户
- 定期测试邮件投递以确保可靠性
- 监控投递失败情况,并具备备用通知方法
性能注意事项
- 限制邮件发送频率,以避免被垃圾邮件过滤器判定为垃圾邮件
- 为不同警报类型使用合适的收件人列表
- 对高流量系统实施速率限制
- 在包含大附件时考虑邮件大小
下一步
在设置好邮件通知后:
- 为不同类型的警报创建邮件模板
- 为各种利益相关者设置分发名单
- 为关键问题实现升级工作流
- 为管理层创建定期报告
- 与其他通知系统集成(SMS、Teams 等)