1. OpenClaw邮件通知功能实现方案
OpenClaw作为一款新兴的AI智能体平台,其任务执行结果的自动通知功能是很多用户关心的核心需求。根据我的实际部署经验,确实可以通过配置实现任务结果邮件通知,但需要理解其底层工作机制。
1.1 系统架构与邮件通知原理
OpenClaw的任务执行流程分为三个关键阶段:
- 任务提交阶段(CLI/API)
- 任务处理阶段(Gateway+Agent)
- 结果返回阶段(Webhook/DB)
邮件通知的实现需要在第三阶段介入,目前有两种主流方案:
| 方案类型 | 触发方式 | 配置复杂度 | 实时性 |
|---|---|---|---|
| Webhook回调 | 任务完成时主动触发 | 中等 | 高(秒级) |
| 数据库轮询 | 定期检查任务状态表 | 低 | 中(分钟级) |
1.2 具体配置步骤
方案一:通过Webhook实现(推荐)
- 准备SMTP服务配置:
# 在OpenClaw配置文件中添加 [notifications] smtp_server = smtp.example.com smtp_port = 587 smtp_username = your_email@example.com smtp_password = your_password from_address = no-reply@yourdomain.com- 创建邮件模板(保存为
/etc/openclaw/templates/email.html):
<!DOCTYPE html> <html> <body> <p>任务ID: {{.TaskID}}</p> <p>执行状态: {{.Status}}</p> <pre>{{.Result}}</pre> </body> </html>- 启用Webhook通知:
openclaw config set notifications.webhook_enabled true openclaw config set notifications.email_recipients "user@example.com"方案二:通过数据库轮询(适合已有监控系统)
- 配置任务状态表查询:
-- 创建定时查询脚本 SELECT task_id, status, result FROM openclaw_tasks WHERE status = 'completed' AND notified = false;- 使用Python脚本发送邮件(示例):
import smtplib from email.mime.text import MIMEText def send_notification(task): msg = MIMEText(f"Task {task['id']} completed\nResult: {task['result']}") msg['Subject'] = 'OpenClaw Task Notification' msg['From'] = 'openclaw@example.com' msg['To'] = 'user@example.com' with smtplib.SMTP('smtp.example.com', 587) as server: server.starttls() server.login('user', 'password') server.send_message(msg)1.3 安全注意事项
重要:SMTP密码应使用环境变量或密钥管理服务,切勿直接写在配置文件中
- 建议为OpenClaw创建专用邮件账户
- 启用SMTP TLS加密(端口587)
- 限制邮件发送频率(建议<30封/分钟)
- 监控邮件发送失败日志
2. 常见问题排查指南
2.1 邮件发送失败处理
症状:任务完成但未收到邮件
- 检查Gateway日志:
journalctl -u openclaw-gateway -f- 验证SMTP连通性:
telnet smtp.example.com 587 openssl s_client -connect smtp.example.com:587 -starttls smtp典型错误解决方案:
| 错误代码 | 可能原因 | 解决方法 |
|---|---|---|
| 535 5.7.8 | 认证失败 | 检查用户名/密码,启用"允许不安全应用" |
| 421 4.7.0 | 连接超时 | 检查防火墙/安全组规则 |
| 552 5.2.3 | 邮件大小超限 | 压缩任务结果或分页发送 |
2.2 性能优化建议
当处理大批量任务时:
- 启用邮件队列(建议使用Redis):
# config.yaml queue: enabled: true type: redis address: "localhost:6379"- 合并通知(相同用户的多任务结果合并发送):
openclaw config set notifications.batch_enabled true openclaw config set notifications.batch_window 300 # 5分钟3. 高级配置技巧
3.1 条件性邮件通知
通过添加任务标签控制通知行为:
openclaw run --tags "notify=email" your_task.json对应配置规则:
rules: - condition: "'notify=email' in tags" actions: - type: email template: urgent.html recipients: "team@example.com"3.2 邮件模板变量扩展
除基本变量外,还可使用这些特殊变量:
| 变量 | 说明 | 示例 |
|---|---|---|
| {{.Duration}} | 任务耗时 | 2m15s |
| {{.Metadata}} | 任务元数据 | {"priority": "high"} |
| {{.Error}} | 错误详情(失败时) | "Timeout exceeded" |
3.3 与企业通讯工具集成
飞书/微信接入方案:
- 通过邮件网关中转:
# 飞书机器人适配器 import requests def send_to_feishu(content): webhook = "https://open.feishu.cn/..." requests.post(webhook, json={ "msg_type": "text", "content": {"text": content} })- 直接调用企业微信API(需配置CORPID):
openclaw config set notifications.wecom_id "your_corpid"4. 监控与维护
4.1 健康检查配置
创建监控脚本/usr/local/bin/check_openclaw_mail.sh:
#!/bin/bash # 检查最近1小时邮件发送成功率 success_count=$(grep -c "Email sent successfully" /var/log/openclaw/notifications.log) failure_count=$(grep -c "Failed to send email" /var/log/openclaw/notifications.log) if [ $failure_count -gt 3 ]; then echo "CRITICAL: Email failure rate high" exit 2 elif [ $success_count -eq 0 ]; then echo "WARNING: No emails sent in last hour" exit 1 else echo "OK: Email notifications working" exit 0 fi添加到cron定时任务:
*/5 * * * * /usr/local/bin/check_openclaw_mail.sh | logger -t openclaw_monitor4.2 日志轮转配置
创建/etc/logrotate.d/openclaw:
/var/log/openclaw/*.log { daily rotate 7 compress delaycompress missingok notifempty create 640 openclaw adm sharedscripts postrotate systemctl reload openclaw-gateway endscript }我在实际生产环境中发现,邮件通知功能最关键的其实是可靠性和及时性的平衡。建议对于关键任务采用Webhook+邮件双重通知,普通任务可以使用批处理模式。同时要注意监控SMTP服务的可用性,我曾经遇到过因为邮件服务器临时维护导致任务积压的情况。