
1. OpenClaw邮件通知功能实现方案OpenClaw作为一款新兴的AI智能体平台其任务执行结果的自动通知功能是很多用户关心的核心需求。根据我的实际部署经验确实可以通过配置实现任务结果邮件通知但需要理解其底层工作机制。1.1 系统架构与邮件通知原理OpenClaw的任务执行流程分为三个关键阶段任务提交阶段CLI/API任务处理阶段GatewayAgent结果返回阶段Webhook/DB邮件通知的实现需要在第三阶段介入目前有两种主流方案方案类型触发方式配置复杂度实时性Webhook回调任务完成时主动触发中等高秒级数据库轮询定期检查任务状态表低中分钟级1.2 具体配置步骤方案一通过Webhook实现推荐准备SMTP服务配置# 在OpenClaw配置文件中添加 [notifications] smtp_server smtp.example.com smtp_port 587 smtp_username your_emailexample.com smtp_password your_password from_address no-replyyourdomain.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 userexample.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(fTask {task[id]} completed\nResult: {task[result]}) msg[Subject] OpenClaw Task Notification msg[From] openclawexample.com msg[To] userexample.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 notifyemail your_task.json对应配置规则rules: - condition: notifyemail in tags actions: - type: email template: urgent.html recipients: teamexample.com3.2 邮件模板变量扩展除基本变量外还可使用这些特殊变量变量说明示例{{.Duration}}任务耗时2m15s{{.Metadata}}任务元数据{priority: high}{{.Error}}错误详情失败时Timeout exceeded3.3 与企业通讯工具集成飞书/微信接入方案通过邮件网关中转# 飞书机器人适配器 import requests def send_to_feishu(content): webhook https://open.feishu.cn/... requests.post(webhook, json{ msg_type: text, content: {text: content} })直接调用企业微信API需配置CORPIDopenclaw config set notifications.wecom_id your_corpid4. 监控与维护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服务的可用性我曾经遇到过因为邮件服务器临时维护导致任务积压的情况。