AgenticEmailService
emailmcp
Self-hosted SendGrid — powered by AI.
Your own email infrastructure. One MCP command provisions a cloud VPS, WireGuard tunnel, DKIM/SPF/DMARC, Let's Encrypt TLS, and authoritative DNS. No third-party email provider needed.
Or use it as a universal email client with 9 providers. Your choice.
Works with
Autonomous Mode • Provider Mode • Rules Engine • Install • API Docs
Two Modes
Self-Hosted (Autonomous Mode) — your own SendGrid
You: "Set up my email server"
AI: Creating OVH VPS in GRA11... done. IP: 51.38.x.x
WireGuard config saved. Run: sudo wg-quick up ~/.emailmcp/wg-email.conf
You: [connects WireGuard]
You: "Resume setup"
AI: Tunnel verified. Add these 2 DNS records at your registrar:
NS emailrelay.xyz → ns1.emailrelay.xyz
A ns1.emailrelay.xyz → 51.38.x.x
You: [adds DNS records]
You: "Resume setup"
AI: DNS propagated. TLS cert obtained. SMTP verified.
Your email server is live.
You: "Add company.com as a sending domain"
AI: DKIM keys generated. Add these 4 records to company.com:
MX company.com → mail.company-com.emailrelay.xyz
TXT company.com → v=spf1 include:company-com.emailrelay.xyz -all
CNAME emailmcp._domainkey.company.com → emailmcp._domainkey.company-com.emailrelay.xyz
TXT _dmarc.company.com → v=DMARC1; p=reject; adkim=s; aspf=s
You: "Send hello@company.com a welcome email"
AI: Sent. DKIM ✅ SPF ✅ DMARC ✅
What you get:
- Dedicated clean IP on a $3/mo VPS (OVH, Vultr, Hetzner, DigitalOcean)
- WireGuard tunnel — email flows through your IP, not a shared pool
- DKIM signing with your own 2048-bit RSA keys
- SPF hard fail (
-all) — only your IP authorized - DMARC strict alignment (
adkim=s; aspf=s) - Authoritative DNS on the VPS (CoreDNS) — you are your own nameserver
- Let's Encrypt TLS for SMTP STARTTLS
- PTR / reverse DNS set automatically
- Local mailbox storage — AI reads inbox via MCP, no IMAP needed
- Multi-tenant: add unlimited sending domains, each with its own DKIM keys
- Health monitoring with auto-restart
No SendGrid. No Mailgun. No monthly email bills. Your infrastructure, your keys, your data.
Provider Mode — 9 providers, zero lock-in
Same tool, different backend. Use any email provider:
Switch providers by changing one env var. Read inbox via IMAP with real-time IDLE push notifications.
Rules Engine
Email rules with MCP-triggered actions. Like server-side rules, but your AI controls them:
You: "Create a rule: when emails from *@vip-client.com arrive, flag as VIP and forward to ceo@company.com"
AI: Rule created: VIP Client Alert (priority 10)
Match: from=*@vip-client.com
Actions: flag(vip), forward(ceo@company.com)
8 action types: forward, auto_reply, webhook, flag, move, mcp_tool, log, reject
Enterprise patterns tested:
- Multi-department routing (sales/support/billing with different auto-replies)
- Cascading escalation (L1 → L2 → L3 with
stopflags) - Compliance (legal hold, GDPR data subject request detection, SOX flagging)
- DLP (flag emails containing sensitive patterns)
- Phishing protection (reject known bad domains)
- Audit trail (webhook every email to your audit system)
- Batch processing (50 concurrent emails, zero errors)
Rules fire on both autonomous inbound SMTP and IMAP IDLE real-time notifications.
IMAP IDLE — Real-Time Push
No polling. Instant notification when new email arrives:
You: "Watch my work and personal email for new messages"
AI: Watching work (me@company.com) and personal (me@gmail.com)
New emails trigger rule evaluation automatically.
[New email arrives]
AI: New email in work: "Server Down Alert" from monitoring@infra.com
→ Rule "L2 Infrastructure" matched → forwarded to infra-team@company.com
Works with any IMAP provider. Multi-account. Auto-reconnect on disconnect.
Install
Option A: npx (zero setup)
{
"mcpServers": {
"email": {
"command": "npx",
"args": ["-y", "emailmcp"],
"env": {
"DEFAULT_PROVIDER": "autonomous",
"EMAIL_DOMAIN": "company.com"
}
}
}
}
Option B: Clone for autonomous mode
git clone https://github.com/nfodor/emailmcp.git
cd emailmcp && npm install
cp .env.example .env
# Set DEFAULT_PROVIDER=autonomous and EMAIL_DOMAIN
Then ask your AI: "Set up my email server"
Provider mode (any of 9 providers)
{
"mcpServers": {
"email": {
"command": "npx",
"args": ["-y", "emailmcp"],
"env": {
"DEFAULT_PROVIDER": "mailgun",
"MAILGUN_API_KEY": "key-your-api-key",
"MAILGUN_DOMAIN": "mg.yourdomain.com",
"FROM_EMAIL": "noreply@yourdomain.com",
"IMAP_HOST": "imap.gmail.com",
"IMAP_EMAIL": "you@gmail.com",
"IMAP_PASSWORD": "your-app-password"
}
}
}
}
See .env.example for all provider configs (Mailgun, SendGrid, SES, Gmail, Resend, SMTP, MailHog, Autonomous).
MCP Tools
| Tool | Description |
|---|---|
send_email | Send with text/HTML, attachments, CC, BCC. Provider override per-call. |
send_template_email | Handlebars templates (built-in: welcome, notification, test) |
validate_email | Format + MX record verification |
read_inbox | IMAP or local autonomous mailbox with filters |
get_mailbox_status | Total / unread / recent counts |
Rules
| Tool | Description |
|---|---|
create_email_rule | Match conditions + actions (forward, auto_reply, webhook, mcp_tool, etc.) |
list_email_rules | All rules with hit counts |
update_email_rule | Enable/disable, change priority/match/actions |
delete_email_rule | Remove a rule |
Real-Time
| Tool | Description |
|---|---|
start_idle_watch | IMAP IDLE push on one or all accounts |
stop_idle_watch | Stop watching |
get_idle_status | Connection status, new email counts |
Autonomous Server
| Tool | Description |
|---|---|
provision_start | Create cloud VPS + WireGuard + DKIM keys |
provision_resume | Resume from where you left off (phased wizard) |
provision_status | Which phases complete, what's next |
add_sending_domain | Add customer domain, get 4 DNS records |
list_sending_domains | All domains with verification status |
create_mailbox | Create local mailbox (sales@, support@, etc.) |
setup_autonomous_security | Generate DKIM/SPF/DMARC |
get_dns_records | All required DNS records for a domain |
verify_autonomous_setup | Test DNS, DKIM, SMTP end-to-end |
start_health_monitor | Tunnel + SMTP + DNS + IP reputation checks |
Infrastructure
| Tool | Description |
|---|---|
list_cloud_providers | Pricing: OVH $3/mo, Vultr $2.50/mo, Hetzner $3.79/mo, DO $4/mo |
regenerate_zone_file | Full CoreDNS zone with all customer subdomains |
request_email_ip | WireGuard IP provisioning queue |
Architecture
┌─────────────────────────────┐
│ Cloud VPS ($3/mo) │
│ │
Internet ──► port 25 ──► WireGuard ──────────────────┼──► Your Machine
│ ▲ │ │
│ │ CoreDNS (port 53) │ ▼
│ │ Let's Encrypt TLS │ EmailMCP
│ │ iptables forwarding │ ├── SMTP Receiver
└─────┼─────────────────────────┘ ├── Mailbox Store
│ ├── Rules Engine
│ ├── DKIM Signer
WireGuard ├── MCP Server
Tunnel └── Local API :41924
│
┌─────┼───────┐
│ Your Machine│
│ │
│ Claude/ │
│ Cursor/ │◄──── MCP Protocol
│ Codex │
└──────────────┘
Inbound: Internet → VPS port 25 → WireGuard → your machine → mailbox store → AI reads via MCP
Outbound: AI sends via MCP → DKIM signed → WireGuard → VPS clean IP → recipient MX
DNS: VPS runs CoreDNS as authoritative nameserver for your service domain
Provisioning Flow (Phased Wizard)
No chicken-and-egg. Each phase waits for its prerequisites:
| Phase | What happens | User action |
|---|---|---|
| 1 | Create VPS, get IP, generate WireGuard config | None (automatic) |
| 2 | Verify WireGuard tunnel | sudo wg-quick up ~/.emailmcp/wg-email.conf |
| 3 | Show DNS records (now we have the real IP) | Add 2 records at registrar |
| 4 | Verify DNS, obtain Let's Encrypt cert | None (automatic after propagation) |
| 5 | Full verification: tunnel + SMTP + DNS | None (automatic) |
State saved to ~/.emailmcp/provision-state.json. Run provision_resume after each action.
API Docs
When the local API is running (autonomous mode), Swagger UI is at:
http://127.0.0.1:41924/api-docs
Raw OpenAPI spec: http://127.0.0.1:41924/api-docs/swagger.json
Covers both the REST API and all MCP tools.
Development
git clone https://github.com/nfodor/emailmcp.git
cd emailmcp && npm install
npm test # 1266 tests, 103 sections
npm run test:watch # Auto-rerun on changes
Test categories
| Category | Tests |
|---|---|
| Core (Config, Logger, RateLimiter, UsageTracker) | 96 |
| Provider Factory & Base | 107 |
| IMAP Reader | 73 |
| Email Auth (DKIM/SPF/DMARC) | 15 |
| Threading & SMTP Forwarding | 137 |
| Templates | 25 |
| SMTP Provider | 22 |
| Multi-Account IMAP | 14 |
| Autonomous Provider | 24 |
| Mailbox Store | 36 |
| DNS Records | 29 |
| Rules Engine | 57 |
| IMAP IDLE | 14 |
| Cloud Provisioning | 30 |
| Sending Domain Manager | 34 |
| DKIM Sign/Verify (real SMTP) | 32 |
| SPF Validation (simulated) | 15 |
| DMARC Alignment (simulated) | 24 |
| Full Chain (DKIM→SPF→DMARC) | 18 |
| E2E: Real SMTP → Store → MCP | 80 |
| E2E: Rules (enterprise patterns) | 71 |
| Total | 1266 |
E2E tests run real SMTP servers, send real emails via nodemailer, fire real rules, and verify the full pipeline. No mocks.
See CONTRIBUTING.md for how to write tests and add features.
Cloud Provider Pricing
| Provider | Plan | Price | PTR | DNS |
|---|---|---|---|---|
| OVH | d2-2 | €3/mo | native | native (subdomain delegation) |
| Vultr | vc2-1c-1gb | $2.50/mo | native | — |
| Hetzner | cx22 | €3.79/mo | native | — |
| DigitalOcean | s-1vcpu-512mb | $4/mo | via name | via DO DNS |
All you need is WireGuard on the VPS. The cheapest VM handles it.
Troubleshooting
| Problem | Fix |
|---|---|
| AI doesn't see email tools | Restart client after config change |
| WireGuard won't connect | Check VPS firewall allows UDP 51820 |
| DNS not propagating | Wait up to 60 min. Run provision_resume to re-check. |
| DKIM failing | Run setup_autonomous_security to regenerate keys |
| Emails going to spam | Check IP reputation: get_health_status shows blocklist checks |
| Rate limit hit | Adjust RATE_LIMIT_HOURLY / RATE_LIMIT_DAILY in .env |
| IMAP connection fails | Use app-specific password. Check port 993. |
Backstory: Why I built this
I wrote my first Windows email client in 1992, when internet email was barely a thing. Three decades later, email is still the only truly decentralized communication protocol that survived.
But email got captured. SendGrid, Mailgun, SES — they're great products, but you're sending through someone else's infrastructure, on their shared IPs, under their rules. Your deliverability depends on their reputation.
AgenticEmailService gives you the SendGrid experience — DKIM, SPF, DMARC, dedicated IP, domain authentication — but self-hosted. One cheap VPS, one WireGuard tunnel, full control. Your AI provisions it, manages it, and sends through it.
Email deserves to be sovereign again. This is one step toward that.