home / skills / openclaw / skills / imap-smtp-email

imap-smtp-email skill

/skills/gzlicanyi/imap-smtp-email

This skill helps you read, search, and send emails via IMAP/SMTP, manage mailboxes, and attach files across major providers.

npx playbooks add skill openclaw/skills --skill imap-smtp-email

Review the files below or copy the command above to add this skill to your agents.

Files (7)
SKILL.md
5.5 KB
---
name: imap-smtp-email
description: Read and send email via IMAP/SMTP. Check for new/unread messages, fetch content, search mailboxes, mark as read/unread, and send emails with attachments. Works with any IMAP/SMTP server including Gmail, Outlook, 163.com, vip.163.com, 126.com, vip.126.com, 188.com, and vip.188.com.
---

# IMAP/SMTP Email Tool

Read, search, and manage email via IMAP protocol. Send email via SMTP. Supports Gmail, Outlook, 163.com, vip.163.com, 126.com, vip.126.com, 188.com, vip.188.com, and any standard IMAP/SMTP server.

## Configuration

Create `.env` in the skill folder or set environment variables:

```bash
# IMAP Configuration (receiving email)
IMAP_HOST=imap.gmail.com          # Server hostname
IMAP_PORT=993                     # Server port
[email protected]
IMAP_PASS=your_password
IMAP_TLS=true                     # Use TLS/SSL connection
IMAP_REJECT_UNAUTHORIZED=true     # Set to false for self-signed certs
IMAP_MAILBOX=INBOX                # Default mailbox

# SMTP Configuration (sending email)
SMTP_HOST=smtp.gmail.com          # SMTP server hostname
SMTP_PORT=587                     # SMTP port (587 for STARTTLS, 465 for SSL)
SMTP_SECURE=false                 # true for SSL (465), false for STARTTLS (587)
[email protected]          # Your email address
SMTP_PASS=your_password           # Your password or app password
[email protected]          # Default sender email (optional)
SMTP_REJECT_UNAUTHORIZED=true     # Set to false for self-signed certs
```

## Common Email Servers

| Provider | IMAP Host | IMAP Port | SMTP Host | SMTP Port |
|----------|-----------|-----------|-----------|-----------|
| 163.com | imap.163.com | 993 | smtp.163.com | 465 |
| vip.163.com | imap.vip.163.com | 993 | smtp.vip.163.com | 465 |
| 126.com | imap.126.com | 993 | smtp.126.com | 465 |
| vip.126.com | imap.vip.126.com | 993 | smtp.vip.126.com | 465 |
| 188.com | imap.188.com | 993 | smtp.188.com | 465 |
| vip.188.com | imap.vip.188.com | 993 | smtp.vip.188.com | 465 |
| yeah.net | imap.yeah.net | 993 | smtp.yeah.net | 465 |
| Gmail | imap.gmail.com | 993 | smtp.gmail.com | 587 |
| Outlook | outlook.office365.com | 993 | smtp.office365.com | 587 |
| QQ Mail | imap.qq.com | 993 | smtp.qq.com | 587 |

**Important for 163.com:**
- Use **authorization code** (授权码), not account password
- Enable IMAP/SMTP in web settings first

## IMAP Commands (Receiving Email)

### check
Check for new/unread emails.

```bash
node scripts/imap.js check [--limit 10] [--mailbox INBOX] [--recent 2h]
```

Options:
- `--limit <n>`: Max results (default: 10)
- `--mailbox <name>`: Mailbox to check (default: INBOX)
- `--recent <time>`: Only show emails from last X time (e.g., 30m, 2h, 7d)

### fetch
Fetch full email content by UID.

```bash
node scripts/imap.js fetch <uid> [--mailbox INBOX]
```

### search
Search emails with filters.

```bash
node scripts/imap.js search [options]

Options:
  --unseen           Only unread messages
  --seen             Only read messages
  --from <email>     From address contains
  --subject <text>   Subject contains
  --recent <time>    From last X time (e.g., 30m, 2h, 7d)
  --since <date>     After date (YYYY-MM-DD)
  --before <date>    Before date (YYYY-MM-DD)
  --limit <n>        Max results (default: 20)
  --mailbox <name>   Mailbox to search (default: INBOX)
```

### mark-read / mark-unread
Mark message(s) as read or unread.

```bash
node scripts/imap.js mark-read <uid> [uid2 uid3...]
node scripts/imap.js mark-unread <uid> [uid2 uid3...]
```

### list-mailboxes
List all available mailboxes/folders.

```bash
node scripts/imap.js list-mailboxes
```

## SMTP Commands (Sending Email)

### send
Send email via SMTP.

```bash
node scripts/smtp.js send --to <email> --subject <text> [options]
```

**Required:**
- `--to <email>`: Recipient (comma-separated for multiple)
- `--subject <text>`: Email subject, or `--subject-file <file>`

**Optional:**
- `--body <text>`: Plain text body
- `--html`: Send body as HTML
- `--body-file <file>`: Read body from file
- `--html-file <file>`: Read HTML from file
- `--cc <email>`: CC recipients
- `--bcc <email>`: BCC recipients
- `--attach <file>`: Attachments (comma-separated)
- `--from <email>`: Override default sender

**Examples:**
```bash
# Simple text email
node scripts/smtp.js send --to [email protected] --subject "Hello" --body "World"

# HTML email
node scripts/smtp.js send --to [email protected] --subject "Newsletter" --html --body "<h1>Welcome</h1>"

# Email with attachment
node scripts/smtp.js send --to [email protected] --subject "Report" --body "Please find attached" --attach report.pdf

# Multiple recipients
node scripts/smtp.js send --to "[email protected],[email protected]" --cc "[email protected]" --subject "Update" --body "Team update"
```

### test
Test SMTP connection by sending a test email to yourself.

```bash
node scripts/smtp.js test
```

## Dependencies

```bash
npm install
```

## Security Notes

- Store credentials in `.env` (add to `.gitignore`)
- For Gmail: use App Password if 2FA is enabled
- For 163.com: use authorization code (授权码), not account password

## Troubleshooting

**Connection timeout:**
- Verify server is running and accessible
- Check host/port configuration

**Authentication failed:**
- Verify username (usually full email address)
- Check password is correct
- For 163.com: use authorization code, not account password
- For Gmail: use App Password if 2FA enabled

**TLS/SSL errors:**
- Match `IMAP_TLS`/`SMTP_SECURE` setting to server requirements
- For self-signed certs: set `IMAP_REJECT_UNAUTHORIZED=false` or `SMTP_REJECT_UNAUTHORIZED=false`

Overview

This skill lets you read, search, manage, and send email using standard IMAP and SMTP protocols. It supports any IMAP/SMTP server (examples: Gmail, Outlook, 163.com family, 126.com, 188.com) and handles fetching message content, marking read/unread, mailbox listing, and sending messages with attachments. Configuration is environment-variable driven for easy deployment.

How this skill works

The skill connects to an IMAP server to check mailboxes, search messages with flexible filters, fetch full message content by UID, and toggle read flags. It uses SMTP to compose and send emails, including HTML bodies and file attachments. Connection settings (host, port, TLS/SSL, credentials, mailbox) are provided via environment variables so it works with any standard mail provider.

When to use it

  • Automate checking for new or unread messages in one or more mailboxes
  • Fetch complete email content for processing or archival
  • Search messages by sender, subject, date range, or seen/unseen status
  • Mark messages read/unread after automated processing
  • Send notifications, reports, or emails with attachments from scripts or cron jobs
  • Test SMTP connectivity and delivery from a server environment

Best practices

  • Store credentials in environment variables and keep .env out of version control
  • Use provider-specific app passwords or authorization codes when available (e.g., Gmail 2FA, 163.com auth code)
  • Match TLS/SSL and port settings to the provider: STARTTLS vs SSL vs plain
  • Limit fetch operations (use --limit, --recent) to avoid large downloads during searches
  • For self-signed certs, set reject-unauthorized flags carefully and only in trusted environments

Example use cases

  • A backup job that searches INBOX for unseen messages from the last 24 hours and saves full content to storage
  • A monitoring script that checks mailboxes and triggers alerts when high-priority emails arrive
  • A reporting tool that emails daily reports with attached PDFs to multiple recipients via SMTP
  • An automation that marks processed emails as read to avoid duplicate handling
  • A developer test that sends a test message to validate SMTP credentials and network access

FAQ

Do I need provider-specific settings to use this with Gmail or Outlook?

You only need the correct IMAP/SMTP host, port, and TLS/SSL settings plus valid credentials. For Gmail and Outlook, prefer app passwords or tenant app credentials when two-factor authentication is enabled.

How do I handle large mailboxes or many messages?

Use the --limit and --recent options when checking or searching to narrow results. Fetch individual messages by UID instead of bulk downloading full mailbox content to save bandwidth and time.