a cloudflare worker that checks your email and yells at discord about it!! built with pure vibes and raw tcp sockets 🎶
every minute this little worker wakes up, sneaks into your IMAP mailboxes over a raw TLS socket, grabs any new emails since last time, and fires them off as Discord embeds. no email client needed. no polling service. just a cron job living on the edge (literally, cloudflare edge).
it tracks the last-seen UID per account per mailbox in KV storage so it never sends you the same email twice, even across cold starts !!
- raw IMAP over TLS using
cloudflare:sockets— no npm imap library, built from scratch - custom s-expression parser for IMAP FETCH responses (yes really)
- multi-account support — add as many inboxes as you want
- multi-mailbox per account — poll spam/junk alongside INBOX
- RFC 2047 subject decoding — no more
=?utf-8?B?...?=in your notifications - body previews, decoded from base64 / quoted-printable, HTML stripped
- uses
BODY.PEEKso polling never marks your mail as read - discord webhook notifications with pretty embeds
- first-run baseline: won't flood you with old mail on initial deploy
- runs serverless on cloudflare workers — zero infra
uses pnpm.
npm/yarnwill work too, but the lockfile ispnpm-lock.yaml.
pnpm installpnpm wrangler kv namespace create MAIL_STATEpaste the returned id into wrangler.jsonc under kv_namespaces.
then regenerate types:
pnpm cf-typegencopy secrets.example.json and fill it in:
{
"DISCORD_WEBHOOK": "https://discord.com/api/webhooks/YOUR_ID/YOUR_TOKEN",
"ACCOUNTS_JSON": [
{
"name": "My Account",
"host": "imap.gmail.com",
"port": 993,
"user": "you@gmail.com",
"password": "your-app-password",
"mailboxes": ["INBOX", "[Gmail]/Spam"]
}
]
}| field | required | what it is |
|---|---|---|
name |
yes | label for logs, discord titles, and KV keys. renaming it re-baselines that account — see below |
host |
yes | IMAP server hostname, e.g. imap.gmail.com |
port |
yes | almost always 993 (IMAP over TLS). the client always uses TLS |
user |
yes | usually your full email address |
password |
yes | app-specific password where the provider offers one |
mailboxes |
no | which mailboxes to poll. defaults to ["INBOX"] |
for gmail use an App Password, not your real password !! most providers with 2FA require the same — mail.ru, yandex, fastmail all issue per-app passwords.
don't know your host? it's usually imap.<provider> — or search your provider's docs for "IMAP settings". the port is 993 unless they say otherwise.
mailboxes is a replacement, not an addition. if you set it, include INBOX explicitly or you'll stop polling it.
names are provider-specific and not guessable:
| provider | inbox | spam |
|---|---|---|
| gmail | INBOX |
[Gmail]/Spam |
| localised servers | INBOX |
translated, e.g. Correo no deseado |
| most others | INBOX |
Junk |
write them decoded. non-ASCII names travel as modified UTF-7 on the wire (Wichtig stays as-is, but Gelöscht becomes Gel&APY-scht), and the client handles that conversion — put the readable name in config.
easiest way to get this right: deploy with the default ["INBOX"], run once, and read the log. every run lists what the server actually has:
[My Account] 10 mailbox(es): INBOX, Junk, Sent, Drafts, Trash, ...
copy the names you want from that line. a configured name the server doesn't have logs a warning rather than failing silently:
[My Account] configured but not on server: Spam
secrets.json is not read by the worker. it feeds two separate targets, and editing it alone changes nothing:
| command | target | use for |
|---|---|---|
pnpm secrets |
cloudflare (deployed) | production |
pnpm dev-vars |
local .dev.vars |
pnpm dev |
re-run the relevant one after every edit to secrets.json.
pnpm secretsor push individually, without secrets.json:
pnpm wrangler secret put DISCORD_WEBHOOK
pnpm wrangler secret put ACCOUNTS_JSONstate is keyed lastUID:<account>:<mailbox> in KV, so renaming an account or a mailbox orphans its watermark. the new key baselines on the next run, meaning anything that arrived in the gap is skipped rather than reported. harmless, but don't expect a backfill.
adding a new mailbox behaves the same way: it baselines first, then notifies from the run after.
pnpm deploygenerate a local .dev.vars from the same secrets.json:
pnpm dev-varsthen:
pnpm devtrigger the scheduled handler manually:
curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"
.dev.varsis gitignored, as issecrets.json. re-runpnpm dev-varsafter every edit tosecrets.json— the worker reads.dev.vars, not the json.
pnpm test # watch mode, with coverage
pnpm vitest run # one shotcovers the parsers, the IMAP client against an in-process fake server, and the cron handler end to end.
tests never touch your real account: vitest.config.mts overrides the KV namespace to local storage and blanks the credentials loaded from .dev.vars.
currently set to * * * * * (every minute). change it in wrangler.jsonc under triggers.crons.
heads up: every-minute polling means 1440 IMAP logins per account per day. some providers rate-limit or lock accounts for that.
*/5 * * * *is gentler if you hit trouble.
- Cloudflare Workers
- Cloudflare KV — state persistence
cloudflare:sockets— raw TLS connections- TypeScript