Skip to content
プロジェクト

概要

email for agents. Built for AI agents that need to send, receive, and understand emails programmatically

仕事効率化・自動化

この数値の測り方

計測したスター増加

24時間
0
7日間
+2
15日間
+1
1か月
履歴を収集中

期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。

README.ja.md

オリジナル

mails

AIエージェント向けのメールインフラ。プログラムでメールの送受信ができます。

npm license

English | 日本語 | 中文

仕組み

                        送信                                        受信

  Agent                                               外部送信者
    |                                                   |
    |  mails send --to [email protected]                 |  [email protected] にメール送信
    |                                                   |
    v                                                   v
+--------+                                    +-------------------+
|  CLI   |                                    | Cloudflare Email  |
|  /SDK  |                                    |     Routing       |
+--------+                                    +-------------------+
    |                                                   |
    |  /v1/send                                         |  email() handler
    |  /api/send                                        |
    v                                                   v
+-----------------------------------------------------------+
|                       Worker                              |
|                                                           |
|  provider チェーン:Cloudflare Email Service → Resend     |
|                                                           |
|  mails.dev(ホスティング)    or   自前デプロイ              |
|  +-----------------------------------------+              |
|  |  +----------+    +----------------+     |  +---------+ |
|  |  |  db9.ai  |    |  fs9(ファイル) |     |  |   D1    | |
|  |  | 全文検索  |    | (添付保存)    |     |  | (保存) | |
|  |  | 高度検索  |    +----------------+     |  +---------+ |
|  |  +----------+                           |              |
|  +-----------------------------------------+              |
+-----------------------------------------------------------+
          |                           |
   CLI/SDKで問い合わせ          mails sync
   (remote provider)      (ローカルに取得)
          |                           |
          v                     +-----+------+
       Agent                    |            |
                          +---------+  +-----------+
                          | SQLite  |  |  db9.ai   |
                          |(ローカル)|  |(クラウド) |
                          +---------+  +-----------+
                          オフライン    全文検索
                          バックアップ  高度なフィルター

特徴

  • メール送信 — プロバイダチェーン:Cloudflare Email Service(ネイティブ env.EMAIL.send() バインディング、パブリックベータ)と Resend のフォールバック。デフォルト順序 cloudflare,resend、EMAIL_PROVIDERS で上書き可能
  • メール受信 — Cloudflare Email Routing Worker経由
  • 受信箱検索 — キーワードで件名、本文、送信者、認証コードを検索
  • 認証コード自動抽出 — メールから認証コードを自動検出(英/中/日/韓対応)
  • 添付ファイル — CLIの --attach またはSDKで送信、MIME添付ファイルの受信・解析
  • ストレージプロバイダー — ローカルSQLite、db9.aiクラウドPostgreSQL、またはリモートWorker API
  • プロバイダ可視化 — /api/send レスポンスと /api/inbox の outbound 行に provider フィールド(cloudflare/resend)が含まれ、実際にどちらが配信したか確認できる
  • ゼロランタイム依存 — プロバイダは全て Workers バインディングまたは fetch() を直接使用、SDK なし
  • ホスティングサービス — mails claim で無料 @mails.dev メールアドレス取得
  • セルフホスト — 独自Workerデプロイ、メールボックス単位の token 認証

インストール

npm install -g mails
# or
bun install -g mails
# or use directly
npx mails

CLI は最大 24 時間に 1 回 npm を確認し、新しいバージョンがある場合は stderr に更新通知を表示します。MAILS_NO_UPDATE_CHECK=1 で無効化できます(NO_UPDATE_NOTIFIER=1 または CI=1 が設定されている場合もスキップされます)。

クイックスタート

ホスティングモード (mails.dev)

mails claim myagent                  # [email protected] を無料で取得
mails send --to [email protected] --subject "Hello" --body "World"  # 月100通無料
mails inbox                          # 受信箱を確認
mails inbox --query "password"       # メール検索
mails code --to [email protected]    # 認証コードを待機

Resendキー不要 — ホスティングユーザーは月100通無料。無制限送信は自分のキーを設定:mails config set resend_api_key re_YOUR_KEY

セルフホストモード

cd worker && wrangler deploy             # 独自Workerをデプロイ
# 送信プロバイダを最低1つ設定:
wrangler secret put RESEND_API_KEY       # オプションA:Resend
#   オプションB:Cloudflare Email Service(パブリックベータ)— wrangler.toml に追加:
#   [[send_email]]
#   name = "EMAIL"
# 両方設定時、デフォルトの優先順位は cloudflare → resend。
# 順序の強制 / 片方無効化:
#   wrangler secret put EMAIL_PROVIDERS   # 例: "resend" または "cloudflare,resend"
#
# 単一メールボックス:
#   [email protected]
#   AUTH_TOKEN=YOUR_MAILBOX_TOKEN
# 複数メールボックス:
#   AUTH_TOKENS_JSON={"[email protected]":"token1","[email protected]":"token2"}
mails config set worker_url https://your-worker.example.com
mails config set worker_token YOUR_MAILBOX_TOKEN
mails config set mailbox [email protected]
mails send --to [email protected] --subject "Hello" --body "Hi"  # Worker経由で送信
mails inbox                              # Worker APIに問い合わせ
mails sync                               # メールをローカルSQLiteにダウンロード

CLIリファレンス

claim

mails claim <name>                   # [email protected] を取得(ユーザーあたり最大10個)

send

mails send --to <email> --subject <subject> --body <text>
mails send --to <email> --subject <subject> --html "<h1>Hello</h1>"
mails send --from "Name <email>" --to <email> --subject <subject> --body <text>
mails send --to <email> --subject <subject> --body <text> --reply-to <email>
mails send --to <email> --subject "Report" --body "See attached" --attach report.pdf

inbox

mails inbox                                  # 最近のメール一覧
mails inbox --full-id                        # 完全なメール ID で一覧表示(デフォルトは先頭 8 文字)
mails inbox --mailbox [email protected]         # 特定のメールボックス
mails inbox --query "password reset"         # 全文検索(関連性順)
mails inbox --query "invoice" --direction inbound --limit 10
mails inbox <id>                             # メール詳細 + 添付ファイル
mails inbox <id> --save                      # 添付ファイルをカレントディレクトリにダウンロード
mails inbox <id> --save ./downloads          # 添付ファイルを指定ディレクトリにダウンロード

高度なクエリ機能(添付 / 送信者 / 期間 / ヘッダーのフィルターと送信者統計)は mails.dev ホスティングの HTTP API でのみ提供されており、mails CLI や SDK には まだ接続されていません。詳細は docs/advanced-query-api.md を参照。

code

mails code --to [email protected]              # 認証コード待機(デフォルト30秒)
mails code --to [email protected] --timeout 60 # タイムアウト指定

認証コードはstdoutに出力されるため、パイプで簡単に連携できます:CODE=$(mails code --to [email protected])

config

mails config                    # 設定をすべて表示
mails config set <key> <value>  # 値を設定
mails config get <key>          # 値を取得
mails config path               # 設定ファイルのパスを表示

sync

mails sync                              # Workerからローカルストレージにメールを同期
mails sync --since 2026-03-01           # 指定日からの同期
mails sync --from-scratch               # フル再同期

Worker(ホスティングまたはセルフホスト)からローカルSQLiteにメールをプル。オフラインアクセスやローカルバックアップに便利。

SDK

import { send, getInbox, searchInbox, waitForCode } from 'mails'

// 送信
const result = await send({
  to: '[email protected]',
  subject: 'Hello',
  text: 'World',
})

// 添付ファイル付き送信
await send({
  to: '[email protected]',
  subject: 'Report',
  text: 'See attached',
  attachments: [{ path: './report.pdf' }],
})

// 受信箱一覧
const emails = await getInbox('[email protected]', { limit: 10 })

// 受信箱検索(全文検索、関連性順)
const results = await searchInbox('[email protected]', {
  query: 'password reset',
  direction: 'inbound',
})

// 認証コード待機
const code = await waitForCode('[email protected]', { timeout: 30 })
if (code) console.log(code.code) // "123456"

Email Worker

worker/ ディレクトリにはメール受信用のCloudflare Email Routing Workerが含まれています。

セットアップ

cd worker
bun install
wrangler d1 create mails
# wrangler.toml を編集 — D1データベースIDを設定
wrangler d1 execute mails --file=schema.sql
wrangler deploy

デプロイ後、Cloudflare Email Routingでこのworkerにメールを転送するよう設定してください。

Workerの認証設定

# 単一メールボックス:
#   [email protected]
#   AUTH_TOKEN=YOUR_MAILBOX_TOKEN
# 複数メールボックス:
#   AUTH_TOKENS_JSON={"[email protected]":"token1","[email protected]":"token2"}

すべての /api/* エンドポイントに Authorization: Bearer <mailbox-token> が必要です。この token は ?to=、取得対象メール、または送信時の from と一致するメールボックス用である必要があります。/health は常に公開されます。mailbox token が未設定のまま /api/* にアクセスすると 503 を返します。

Worker API

エンドポイント 説明
GET /api/inbox?to=<addr>&limit=20 メール一覧
GET /api/inbox?to=<addr>&query=<text> メール検索
GET /api/code?to=<addr>&timeout=30 認証コードのロングポーリング
GET /api/email?id=<id> メール詳細(添付ファイル含む)
POST /api/send プロバイダチェーン経由でメール送信(Cloudflare Email Service、Resend)
GET /api/sync?to=<addr>&since=<iso> 増分メール同期(添付ファイル含む)
GET /health ヘルスチェック(常に公開)

ストレージプロバイダー

CLIはストレージプロバイダーを自動検出します:

  • 設定に api_key がある場合 → リモート(mails.devホスティング)
  • 設定に worker_url がある場合 → リモート(セルフホストWorker)
  • それ以外 → ローカルSQLite

SQLite(デフォルト)

ローカルデータベース:~/.mails/mails.db。設定不要。

db9.ai

AIエージェント向けクラウドPostgreSQL。全文検索と関連性ランキング、添付ファイル内容の検索、高度なフィルタリングに対応。

mails config set storage_provider db9
mails config set db9_token YOUR_TOKEN
mails config set db9_database_id YOUR_DB_ID

db9を使うと以下の機能が利用可能:

  • 重み付きFTS — 件名(最高優先)> 送信者 > 本文 > 添付ファイルテキスト
  • 添付ファイルフィルター — 種類別、名前別、添付の有無
  • 送信者・期間フィルター — 送信者アドレスと受信日時の範囲で絞り込み
  • ヘッダー検索 — JSONB形式のメールヘッダーを検索
  • 送信者統計 — 全送信者の頻度ランキング

これらの高度な機能は mails.dev ホスティングの HTTP API 経由で提供されます (docs/advanced-query-api.md 参照)。mails CLI/SDK は 現在 query、direction、limit のみを転送します。

リモート(Worker API)

Worker HTTP APIに直接問い合わせます。api_key または worker_url が設定されている場合、自動的に有効になります。

設定キー

キー デフォルト 説明
mailbox 受信メールアドレス
api_key mails.devホスティングサービスAPIキー
worker_url セルフホストWorker URL
worker_token セルフホストWorker用の mailbox token
resend_api_key Resend APIキー
default_from デフォルト送信者アドレス
storage_provider auto sqlite、db9、remote
db9_token db9.ai クラウドストレージ token
db9_database_id db9.ai データベース ID
last_sync 最後に保存したメールから更新する増分同期カーソル(自動管理)

テスト

bun test              # ユニット + E2Eテスト(外部依存なし)
bun test:coverage     # カバレッジレポート付き
bun test:live         # ライブE2Eテスト(.env設定が必要)
bun test:all          # すべてのテスト(ライブE2E含む)

ユニットテスト198件 + E2Eテスト27件 = 225件のテスト、全プロバイダーをカバー。

プロバイダー別E2Eカバレッジ

SQLite db9 Remote (OSS) Remote (Hosted)
Save inbound email ✅ ✅ N/A N/A
Save email + attachments ✅ ✅ N/A N/A
Send → receive (real) ✅ — ✅ ✅
Inbox list ✅ ✅ ✅ ✅
has_attachments flag ✅ ✅ ✅ ✅
Direction filter ✅ ✅ ✅ ✅
Pagination ✅ ✅ ✅ ✅
Email detail ✅ ✅ ✅ ✅
Attachment metadata ✅ ✅ ✅ ✅
Search ✅ ✅ ✅ ✅
Attachment content search ✅ ✅ — —
Verification code ✅ ✅ ✅ ✅
Download attachment ✅ ✅ — ✅
Save to disk (--save) ✅ — — ✅
Mailbox isolation ✅ ✅ — —
Outbound recording ✅ — ✅ N/A
Send via Worker (/api/send) — — ✅ —
Sync to local — — ✅ —

ライセンス

MIT

関連プロジェクトを探す

読み込み中…