Skip to content

About

Cloudflare worker to pull emails with IMAP and notify in discord channel

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

mail-puller

a cloudflare worker that checks your email and yells at discord about it!! built with pure vibes and raw tcp sockets 🎶


what does it do

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 !!


features

  • 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.PEEK so 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

setup

uses pnpm. npm/yarn will work too, but the lockfile is pnpm-lock.yaml.

1. install

pnpm install

2. create KV namespace

pnpm wrangler kv namespace create MAIL_STATE

paste the returned id into wrangler.jsonc under kv_namespaces.

then regenerate types:

pnpm cf-typegen

3. configure secrets

copy 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"]
    }
  ]
}

account fields

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.

choosing mailboxes

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

pushing the config

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 secrets

or push individually, without secrets.json:

pnpm wrangler secret put DISCORD_WEBHOOK
pnpm wrangler secret put ACCOUNTS_JSON

changing config later

state 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.

4. deploy

pnpm deploy

local dev

generate a local .dev.vars from the same secrets.json:

pnpm dev-vars

then:

pnpm dev

trigger the scheduled handler manually:

curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"

.dev.vars is gitignored, as is secrets.json. re-run pnpm dev-vars after every edit to secrets.json — the worker reads .dev.vars, not the json.


tests

pnpm test              # watch mode, with coverage
pnpm vitest run        # one shot

covers 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.


cron schedule

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.


tech stack

About

Cloudflare worker to pull emails with IMAP and notify in discord channel

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages