deep-email-validator
Version:
Validates emails based on regex, common typos, disposable email blacklists, DNS records and SMTP server response.
93 lines (72 loc) • 2.39 kB
Markdown
Validates email addresses based on regex, common typos, disposable email blacklists, DNS records and SMTP server response.
- Validates email format (contains `@`, `.` in domain, no spaces or control characters).
- Validates common typos e.g. `example@gmaill.com` using [mailcheck](https://github.com/mailcheck/mailcheck).
- Validates email was not generated by a disposable email service using [disposable-email-domains](https://github.com/ivolo/disposable-email-domains).
- Validates MX records are present on DNS.
- Validates SMTP server is running.
- Validates mailbox exists on SMTP server.
- Sanitizes inputs to prevent SMTP command injection.
- Native TypeScript support.
## Getting Started
Requires Node.js >= 20. Server-side only (not browser compatible).
### Installation
```
npm install deep-email-validator
```
### Usage
```typescript
import { validate } from 'deep-email-validator'
const res = await validate('asdf@gmail.com')
// {
// "valid": false,
// "reason": "smtp",
// "validators": {
// "regex": {
// "valid": true
// },
// "typo": {
// "valid": true
// },
// "disposable": {
// "valid": true
// },
// "mx": {
// "valid": true
// },
// "smtp": {
// "valid": false,
// "reason": "Mailbox not found.",
// }
// }
// }
```
You can also pass an options object to control which validations run:
```typescript
await validate({
email: 'name@example.org',
sender: 'name@example.org',
validateRegex: true,
validateMx: true,
validateTypo: true,
validateDisposable: true,
validateSMTP: true,
})
```
[Default options can be found here.](https://github.com/mfbx9da4/deep-email-validator/blob/main/src/options/options.ts#L1)
### Custom TLDs
If you want to validate domains with TLDs that are not supported by default, use the `additionalTopLevelDomains` option:
```typescript
await validate({
email: 'name@example.ir',
additionalTopLevelDomains: ['ir'],
})
```
For a list of TLDs supported by default, see [mailcheck defaults](https://github.com/mailcheck/mailcheck/blob/afca031b4ce1cdc6e3ecbe88198f41b4835f81e3/src/mailcheck.js#L31).
<details>
<summary>CommonJS usage</summary>
```javascript
const { validate } = require('deep-email-validator')
const res = await validate('asdf@gmail.com')
```
</details>