← 返回
沟通协作 Key 中文

AgentMail sending and receiving with Python scripts

python files which are used to send an email and to download received emails from an inbox. The email provider is agentmail.to, which offers an API. This way...
用于发送邮件并从收件箱下载收到的邮件的 Python 文件。邮件服务提供商为 agentmail.to,提供 API 接口。使用此方式...
lausser
沟通协作 clawhub v1.0.0 1 版本 100000 Key: 需要
★ 0
Stars
📥 646
下载
💾 50
安装
1
版本
#agentmail#email#latest

概述

Skill: Email via agentmail

You can send and receive email as REPLACE_WITH_IDENTITY@agentmail.to using two Python scripts.

The scripts live in this skill folder and are deployed to ~/.openclaw/workspace/agentmail/.

The scripts contain placeholders which need to be replaced with real-life value:

  • REPLACE_WITH_IDENTITY
  • REPLACE_WITH_THE_HUMANS_EMAIL_ADDRESS

1. Setup

Deploy the skill

Copy the scripts and create the venv in the workspace:

DEST=~/.openclaw/workspace/agentmail
SKILL_DIR="$(dirname "$(readlink -f "$0")")"    # if running from a script
# or just point SKILL_DIR to this skill folder

mkdir -p "$DEST"
cp "$SKILL_DIR/check_mail.py" "$SKILL_DIR/send_email.py" "$DEST/"

cd "$DEST"

# Create venv (prefer uv, fall back to python)
if command -v uv &>/dev/null; then
  uv venv venv
  uv pip install --python venv/bin/python agentmail python-dotenv
else
  python3 -m venv venv
  venv/bin/pip install agentmail python-dotenv
fi

Create the .env file

cat > ~/.openclaw/workspace/agentmail/.env << 'EOF'
AGENTMAIL_API_KEY=am_us_.....
EOF

This is only necessary if you don't have the environment variable in your running environment or if you can't read it from your openclaw.json config file.

Verify

cd ~/.openclaw/workspace/agentmail
source venv/bin/activate
python check_mail.py        # should print "No new mail." on a fresh inbox

2. Receiving Email

Run

cd ~/.openclaw/workspace/agentmail
source venv/bin/activate
python check_mail.py

This downloads all unread messages as JSON files into the workspace directory and marks them as read. Running it again only fetches new mail.

Output files

Each message is saved as MAIL.. — timestamp from the message, 3-digit sequence number within the batch.

Example: MAIL.20260226T134244.001

The JSON inside contains:

{
  "message_id": "<...>",
  "thread_id": "...",
  "timestamp": "2026-02-26 13:42:44+00:00",
  "from": "Sender Name <sender@example.com>",
  "to": ["REPLACE_WITH_IDENTITY@agentmail.to"],
  "cc": null,
  "subject": "Re: Hello",
  "text": "Plain-text body...",
  "html": "<p>HTML body...</p>",
  "labels": ["received", "unread"],
  "in_reply_to": "<original-message-id>",
  "attachments": []
}

Reading downloaded mail

# List all mail files (oldest first)
ls -1 MAIL.* 2>/dev/null | sort

# Read one
cat MAIL.20260226T134244.001

# Extract just the text body
python -c "import json,sys; print(json.load(open(sys.argv[1]))['text'])" MAIL.20260226T134244.001

Exit codes

CodeMeaning
---------------
0Success (mail downloaded or inbox empty)
1Missing API key
2API error on initial listing
3Total failure (all messages errored)

3. Sending Email

Run

cd ~/.openclaw/workspace/agentmail
source venv/bin/activate
python send_email.py

send_email.py is a template with hardcoded recipient/subject/body. For real use, modify its parameters or write a one-off script using the same pattern:

import os
from dotenv import load_dotenv
from agentmail import AgentMail

load_dotenv()
client = AgentMail(api_key=os.getenv("AGENTMAIL_API_KEY"))

client.inboxes.messages.send(
    inbox_id="REPLACE_WITH_IDENTITY@agentmail.to",
    to="recipient@example.com",
    subject="Subject line",
    text="Plain-text body.",
)

send() parameters

ParameterRequiredDescription
----------------------------------
inbox_idyesAlways "REPLACE_WITH_IDENTITY@agentmail.to"
toyesRecipient email address
subjectnoSubject line
textnoPlain-text body
htmlnoHTML body
ccnoCC addresses
bccnoBCC addresses

4. Replying to a Message

To reply in the same thread, use reply() with the message_id from a downloaded MAIL.* file:

import json, os
from dotenv import load_dotenv
from agentmail import AgentMail

load_dotenv()
client = AgentMail(api_key=os.getenv("AGENTMAIL_API_KEY"))

# Load the message you want to reply to
with open("MAIL.20260226T134244.001") as f:
    msg = json.load(f)

client.inboxes.messages.reply(
    inbox_id="REPLACE_WITH_IDENTITY@agentmail.to",
    message_id=msg["message_id"],
    text="This is my reply.",
)

This preserves threading — the reply appears in the same conversation as the original.

5. Typical Workflow

  1. Check mail: python check_mail.py
  2. Read: inspect the MAIL.* files that were created
  3. Process: act on the content of each message
  4. Reply if needed: use the reply() pattern from section 4
  5. Clean up: rm MAIL.* when done processing
  6. Repeat: run check_mail.py again later for new messages

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-30 01:33 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

communication-collaboration

Gmail

byungkyu
Gmail API 集成,托管 OAuth,支持读取、发送和管理邮件、线程、标签及草稿,适用于需要与 Gmail 交互的场景。
★ 72 📥 37,725
communication-collaboration

Slack

steipete
当需要通过 slack 工具从 Clawdbot 控制 Slack 时使用,包括在频道或私信中回复消息或置顶/取消置顶项目。
★ 157 📥 47,678
communication-collaboration

imap-smtp-email

gzlicanyi
使用IMAP/SMTP读取和发送邮件;检查新/未读邮件、获取内容、搜索邮箱、标记已读/未读、发送带附件的邮件。支持...
★ 113 📥 52,404