mailsure 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ First stable release. mailsure checks email addresses at signup, with no dependencies.
6
+
7
+ - `check(email, options)`: syntax (a practical subset of RFC 5321/5322, with quoted local parts, Unicode and optional IP domains), a typo suggestion, and disposable, role and free flags. Returns a reason when an address is invalid. Never throws.
8
+ - `suggest(email)`: "Did you mean …?" fixes for about 45 popular mailbox providers worldwide and in India (`gmial.com` → `gmail.com`, `yahoo.co.on` → `yahoo.co.in`) and for TLDs that don't exist (`.con`, `.cmo`, `.ogr`). Unknown domains are left alone.
9
+ - `isEmail`, `isDisposable` (about 230 well-known disposable services, subdomains included), `isRole`, `isFree`.
10
+ - `normalize(email, { canonical: true })`: provider-aware forms for finding duplicate sign-ups (Gmail dots and `+tags`, `googlemail.com`, Outlook, iCloud, Proton, Fastmail and Yahoo tags).
11
+ - Options: `allowDisposable`, `allowRole`, `allowIp`, `allowUnicode`, extra `domains` and `disposableDomains`.
12
+ - `mailsure/dns` (Node.js): `hasMx` with A/AAAA fallback and null MX support, and `checkDeliverable`. Returns `null` instead of guessing when DNS fails or times out.
13
+ - ESM and CommonJS builds with TypeScript types. Works in browsers and edge runtimes (except `mailsure/dns`).
14
+
15
+ ## 0.0.1
16
+
17
+ - Name reservation on npm. No code.
package/README.md CHANGED
@@ -1,9 +1,262 @@
1
- # mailsure
1
+ <div align="center">
2
2
 
3
- Check email addresses at signup: syntax, typo suggestions (gmial.com → gmail.com), disposable and role addresses. Zero dependencies.
3
+ <h1>mailsure</h1>
4
4
 
5
- > Early test version (0.0.1). The full library is coming soon.
5
+ <h3>Catch bad email addresses at signup, before they cost you a user.</h3>
6
+
7
+ <p>
8
+ <a href="https://www.npmjs.com/package/mailsure"><img src="https://img.shields.io/npm/v/mailsure?color=blue" alt="npm version"></a>
9
+ <a href="https://github.com/ckgiri/mailsure/actions/workflows/ci.yml"><img src="https://github.com/ckgiri/mailsure/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
10
+ <a href="https://www.npmjs.com/package/mailsure?activeTab=dependencies"><img src="https://img.shields.io/badge/dependencies-0-brightgreen" alt="zero dependencies"></a>
11
+ <a href="https://www.npmjs.com/package/mailsure"><img src="https://img.shields.io/npm/types/mailsure" alt="TypeScript types included"></a>
12
+ <a href="LICENSE"><img src="https://img.shields.io/npm/l/mailsure" alt="MIT license"></a>
13
+ <a href="https://github.com/ckgiri/mailsure/issues"><img src="https://img.shields.io/badge/contributions-welcome-brightgreen" alt="contributions welcome"></a>
14
+ </p>
15
+
16
+ <p>
17
+ <a href="#installation">Installation</a> ·
18
+ <a href="#quick-start">Quick start</a> ·
19
+ <a href="#api">API</a> ·
20
+ <a href="#options">Options</a> ·
21
+ <a href="#dns-checks">DNS checks</a> ·
22
+ <a href="#contributing">Contributing</a>
23
+ </p>
24
+
25
+ </div>
26
+
27
+ **mailsure** is a lightweight, zero-dependency library that checks email addresses at signup. It validates the syntax, suggests fixes for typos like `gmial.com`, and flags disposable, role and free addresses. It runs in Node.js, browsers and edge runtimes.
28
+
29
+ ```js
30
+ check("ana@gmial.com").suggestion
31
+ // → "ana@gmail.com"
32
+ ```
33
+
34
+ ## ✨ Features
35
+
36
+ - ✅ **Practical syntax checks**: the parts of RFC 5321/5322 that real mail servers accept, with a clear reason when an address fails
37
+ - 🔤 **Typo suggestions**: `gmial.com` → `gmail.com`, `gmail.con` → `gmail.com`, `yahoo.co.on` → `yahoo.co.in`
38
+ - 🗑️ **Disposable addresses**: about 230 well-known throwaway email services, subdomains included
39
+ - 👥 **Role addresses**: `admin@`, `support@`, `noreply@` and other shared inboxes
40
+ - 🆓 **Free providers**: tells Gmail, Outlook, Yahoo and Rediffmail users apart from work addresses
41
+ - 🔁 **Duplicate detection**: canonical forms for Gmail dots, `+tags` and `googlemail.com`
42
+ - 🌍 **Unicode**: international addresses such as `user@例え.jp`
43
+ - 📡 **Optional MX check**: ask DNS whether the domain can receive mail (Node.js)
44
+ - ⚡ **Fast and safe**: linear time, never throws, tested against inputs built to slow it down
45
+ - 📦 **Zero dependencies**: about 7 KB gzipped, with TypeScript types
46
+
47
+ ## Installation
48
+
49
+ ```bash
50
+ npm install mailsure
51
+ ```
52
+
53
+ ## Quick start
54
+
55
+ ```js
56
+ import { check } from "mailsure";
57
+
58
+ check("Ana@Gmial.com");
59
+ // {
60
+ // valid: true,
61
+ // email: "Ana@gmial.com",
62
+ // local: "Ana",
63
+ // domain: "gmial.com",
64
+ // disposable: false,
65
+ // role: false,
66
+ // free: false,
67
+ // suggestion: "Ana@gmail.com"
68
+ // }
69
+
70
+ check("a..b@example.com");
71
+ // { valid: false, reason: "syntax", ... }
72
+
73
+ check("temp@mailinator.com", { allowDisposable: false });
74
+ // { valid: false, reason: "disposable", disposable: true, ... }
75
+ ```
76
+
77
+ A typo suggestion does not make an address invalid. `gmial.com` could be a real domain, so ask the user instead of changing it for them.
78
+
79
+ **"Did you mean …?" in a signup form (React)**
80
+
81
+ ```jsx
82
+ import { useState } from "react";
83
+ import { check } from "mailsure";
84
+
85
+ function EmailField() {
86
+ const [email, setEmail] = useState("");
87
+ const [hint, setHint] = useState();
88
+
89
+ return (
90
+ <label>
91
+ Email
92
+ <input
93
+ type="email"
94
+ value={email}
95
+ onChange={(e) => setEmail(e.target.value)}
96
+ onBlur={() => setHint(check(email).suggestion)}
97
+ />
98
+ {hint && (
99
+ <button type="button" onClick={() => { setEmail(hint); setHint(undefined); }}>
100
+ Did you mean {hint}?
101
+ </button>
102
+ )}
103
+ </label>
104
+ );
105
+ }
106
+ ```
107
+
108
+ **Plain JavaScript**
109
+
110
+ ```js
111
+ import { suggest } from "mailsure";
112
+
113
+ input.addEventListener("blur", () => {
114
+ const fix = suggest(input.value);
115
+ hint.textContent = fix ? `Did you mean ${fix}?` : "";
116
+ });
117
+ ```
118
+
119
+ **On the server**
120
+
121
+ Always check again on the server. Browser checks are only a convenience.
122
+
123
+ ```js
124
+ import { check, normalize } from "mailsure";
125
+
126
+ app.post("/signup", async (req, res) => {
127
+ const result = check(req.body.email, { allowDisposable: false });
128
+ if (!result.valid) {
129
+ return res.status(400).json({ error: `Invalid email (${result.reason})` });
130
+ }
131
+
132
+ // One account per person: "John.Doe+promo@googlemail.com" and "johndoe@gmail.com" match.
133
+ const key = normalize(result.email, { canonical: true });
134
+ if (await db.users.exists({ emailKey: key })) {
135
+ return res.status(409).json({ error: "This email is already registered" });
136
+ }
137
+
138
+ await db.users.create({ email: result.email, emailKey: key });
139
+ await sendConfirmationEmail(result.email);
140
+ res.status(201).end();
141
+ });
142
+ ```
143
+
144
+ ## API
145
+
146
+ | Function | Returns | What it does |
147
+ |---|---|---|
148
+ | `check(email, options?)` | `CheckResult` | Everything below in one call |
149
+ | `isEmail(email, options?)` | `boolean` | True when `check(email, options).valid` is true |
150
+ | `suggest(email, options?)` | `string \| undefined` | The corrected address when the domain looks like a typo |
151
+ | `isDisposable(emailOrDomain, options?)` | `boolean` | True for disposable email services, including their subdomains |
152
+ | `isRole(email)` | `boolean` | True for role addresses such as `admin@` or `support+x@` |
153
+ | `isFree(emailOrDomain)` | `boolean` | True for free mailbox providers |
154
+ | `normalize(email, options?)` | `string` | Trims and lowercases the domain; `{ canonical: true }` for duplicate checks |
155
+
156
+ None of them throw, whatever you pass in.
157
+
158
+ `check` returns:
159
+
160
+ | Field | Type | Meaning |
161
+ |---|---|---|
162
+ | `valid` | `boolean` | The address passed every check you asked for |
163
+ | `email` | `string` | The address, trimmed, with the domain lowercased |
164
+ | `local` | `string` | The part before the `@` |
165
+ | `domain` | `string` | The part after the `@`, lowercased |
166
+ | `reason` | `string` | Only when `valid` is false: `"empty"`, `"syntax"`, `"too-long"`, `"domain"`, `"tld"`, `"disposable"` or `"role"` (and `"mx"` from `checkDeliverable`) |
167
+ | `suggestion` | `string` | The full corrected address, when the domain looks like a typo |
168
+ | `disposable` | `boolean` | The domain is a disposable email service |
169
+ | `role` | `boolean` | The address belongs to a role or team, not a person |
170
+ | `free` | `boolean` | The domain is a free mailbox provider |
171
+
172
+ The built-in lists are exported too: `DISPOSABLE_DOMAINS`, `FREE_DOMAINS`, `POPULAR_DOMAINS` and `ROLE_LOCAL_PARTS`.
173
+
174
+ ### What counts as valid
175
+
176
+ - At most 254 characters in total and 64 before the `@`.
177
+ - The local part uses letters, digits and ``!#$%&'*+/=?^_`{|}~-``, with dots between them (no leading, trailing or double dots). Quoted local parts such as `"john doe"@example.com` are accepted.
178
+ - The domain has at least two labels. Each label is 1–63 letters, digits or hyphens, and does not start or end with a hyphen.
179
+ - The top-level domain is at least two letters, or punycode (`xn--`).
180
+ - Spaces, a missing `@` and extra `@` signs (outside quotes) are rejected.
181
+
182
+ ### Canonical forms
183
+
184
+ `normalize(email, { canonical: true })` lowercases the address and applies the rules of known providers:
185
+
186
+ | Provider | Rule | Example |
187
+ |---|---|---|
188
+ | Gmail, Googlemail | Removes dots and `+tag`, maps to `gmail.com` | `John.Doe+news@googlemail.com` → `johndoe@gmail.com` |
189
+ | Outlook, Hotmail, Live, MSN | Removes `+tag` | `ana+shop@outlook.com` → `ana@outlook.com` |
190
+ | iCloud, me.com, mac.com | Removes `+tag` | `ana+x@icloud.com` → `ana@icloud.com` |
191
+ | Proton, Fastmail | Removes `+tag` | `ana+x@proton.me` → `ana@proton.me` |
192
+ | Yahoo | Removes `-keyword` | `ana-shop@yahoo.com` → `ana@yahoo.com` |
193
+ | Others | Lowercases only | `Ana+x@Example.com` → `ana+x@example.com` |
194
+
195
+ Use the canonical form to find duplicate accounts. Store it next to the address the user typed, and always send mail to the address they typed.
196
+
197
+ ## Options
198
+
199
+ | Option | Default | What it does |
200
+ |---|---|---|
201
+ | `allowDisposable` | `true` | When `false`, disposable addresses are invalid with reason `"disposable"`. They are always flagged in `disposable` |
202
+ | `allowRole` | `true` | When `false`, role addresses are invalid with reason `"role"`. They are always flagged in `role` |
203
+ | `allowIp` | `false` | Accept IP address domains such as `user@[192.0.2.1]` and `user@[IPv6:2001:db8::1]` |
204
+ | `allowUnicode` | `true` | Accept Unicode in the local part and domain. When `false`, only ASCII (punycode domains still pass) |
205
+ | `domains` | `[]` | Extra domains to suggest, such as your company's domain or your users' common providers |
206
+ | `disposableDomains` | `[]` | Extra disposable domains. Subdomains match too |
207
+
208
+ `suggest` accepts `domains` and `disposableDomains`. `isDisposable` accepts `disposableDomains`.
209
+
210
+ ## DNS checks
211
+
212
+ `mailsure/dns` asks DNS whether a domain can receive mail. It uses `node:dns`, so it works in Node.js (and in runtimes that provide `node:dns`), not in browsers.
213
+
214
+ ```js
215
+ import { hasMx, checkDeliverable } from "mailsure/dns";
216
+
217
+ await hasMx("gmail.com"); // true
218
+ await hasMx("ana@example.com"); // false: example.com publishes a "null MX"
219
+ await hasMx("no-such-domain.invalid"); // false
220
+
221
+ const result = await checkDeliverable("ana@example.com", { timeout: 3000 });
222
+ // { valid: false, reason: "mx", mx: false, ... }
223
+ ```
224
+
225
+ | Function | Returns | What it does |
226
+ |---|---|---|
227
+ | `hasMx(domainOrEmail, options?)` | `Promise<boolean \| null>` | `true` when the domain has MX records, or an A/AAAA record (the implicit MX in RFC 5321). `false` when the domain does not exist, has no records, or publishes a null MX (RFC 7505). `null` when DNS failed or timed out, so the answer is unknown |
228
+ | `checkDeliverable(email, options?)` | `Promise<CheckResult & { mx }>` | Runs `check`, then `hasMx` for valid addresses. A `false` makes the address invalid with reason `"mx"`; `null` keeps the result of `check` |
229
+
230
+ Options: `timeout` in milliseconds (default `5000`) and `resolver`, any object with `resolveMx`, `resolve4` and `resolve6` methods, for a custom DNS server or for tests. `checkDeliverable` also takes all `check` options. Neither function throws.
231
+
232
+ Treat `null` as "let them through": a slow DNS server should not block a real user.
233
+
234
+ ## Notes
235
+
236
+ - No library can prove a mailbox exists. Syntax checks and lists catch mistakes and throwaway addresses, and an MX record shows the domain accepts mail, but only a confirmation email shows that a person reads the inbox.
237
+ - Typo suggestions are hints. They cover popular mailbox providers and TLDs that don't exist (`.con`, `.cmo`), and they leave unknown domains alone, but a real domain can look like a typo of a popular one. Ask; don't auto-correct.
238
+ - The disposable list holds well-known services only. New throwaway domains appear every day, so add your own with `disposableDomains`.
239
+ - Role and free addresses belong to real people too. Flag them, or use them for scoring, rather than blocking them by default.
240
+
241
+ ## Contributing
242
+
243
+ **mailsure is open source and contributions are welcome.** Good places to start:
244
+
245
+ - **Disposable domains**: open a pull request adding a domain to `src/data.ts`, with a link that shows it is a throwaway email service.
246
+ - **Typos and providers**: a common typo that isn't caught, or a correct domain that gets a wrong suggestion. Popular providers in your country are especially welcome.
247
+ - **Bugs**: an address that is accepted or rejected when it shouldn't be, reported in [Issues](https://github.com/ckgiri/mailsure/issues).
248
+
249
+ ```bash
250
+ git clone https://github.com/ckgiri/mailsure.git
251
+ cd mailsure
252
+ npm install
253
+ npm test
254
+ ```
255
+
256
+ ## Changelog
257
+
258
+ See [CHANGELOG.md](CHANGELOG.md).
6
259
 
7
260
  ## License
8
261
 
9
- MIT © CK Giri
262
+ [MIT](LICENSE) © CK Giri