├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── src └── index.js └── tests └── mail-test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | 5 | branches: 6 | only: 7 | - master 8 | 9 | before_install: 10 | - npm i -g npm 11 | # Workaround for a permissions issue with Travis virtual machine images 12 | script: 13 | - npm test -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 David Dias 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | mailgun-validate-email 2 | ================= 3 | 4 | Super tiny wrapper of email validation API from [Mailgun](http://www.mailgun.com/), 5 | useful in form validation. This can be most useful in form validation to avoid those pesky spam emails. 6 | 7 | ### Disclaimer 8 | This module uses a third party service from Mailgun to verify the validity of the email, 9 | you can read all the info in their [API docs](http://documentation.mailgun.com/api-email-validation.html) 10 | Emails are *securely transmitted* using Public Key Cryptography 11 | 12 | # Badgers 13 | [![NPM](https://nodei.co/npm/mailgun-validate-email.png?downloads=true&stars=true)](https://nodei.co/npm/mailgun-validate-email/) 14 | 15 | [![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/diasdavid/mailgun-validate-email?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) 16 | [![Dependency Status](https://david-dm.org/diasdavid/mailgun-validate-email.svg)](https://david-dm.org/diasdavid/mailgun-validate-email) 17 | [![Build Status](https://travis-ci.org/diasdavid/mailgun-validate-email.svg)](https://travis-ci.org/diasdavid/mailgun-validate-email) 18 | 19 | ## Usage 20 | 21 | ```sh 22 | npm install mailgun-validate-email --save 23 | ``` 24 | 25 | ```javascript 26 | var validator = require('mailgun-validate-email')('INSERT-YOUR-MAILGUN-PUBKEY-HERE') 27 | validator("banana@papaia.com", function (err, result){ 28 | if(err) { 29 | // email was not valid 30 | } else { 31 | console.log(result); 32 | // register the person for your service etc. 33 | } 34 | }) 35 | ``` 36 | 37 | Output will be something like 38 | 39 | ```javascript 40 | { 41 | is_valid: true, 42 | parts: { 43 | local_part: banana, 44 | domain: papaia.com, 45 | display_name: null 46 | }, 47 | address: 'banana@papaia.com', 48 | did_you_mean: null 49 | } 50 | ``` 51 | 52 | 53 | ### *Why* use Third-Party Email Validation? 54 | 55 | There are *easier* ways of checking if an email conforms to the correct *format* 56 | e.g: using [**Joi**](https://github.com/hapijs/joi#example) `Joi.string().email()` 57 | But a validation library only checks that the address "*looks*" valid, 58 | the Mailgun API actually checks if the domain has a valid [**DNS mx record**](http://en.wikipedia.org/wiki/MX_record) 59 | (checking if the domain *accepts* emails). 60 | 61 | This means you don't waste time (or money) sending emails to **valid@foo.bar** 62 | (*valid* email address which will *fail* to deliver and thus 63 | clog up your inbox with failure reports!) 64 | 65 | **Note**: this will *not* prevent people from registering with your 66 | service/app using a *real* email they *don't control*. 67 | e.g: **barack@whitehouse.gov** ... 68 | so you should still get people to *confirm* their email address by sending them 69 | an email with a unique token. 70 | (this will prevent people registering as someone else) 71 | 72 | 73 | ## License 74 | 75 | (The MIT License) 76 | 77 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 78 | 79 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 80 | 81 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 82 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mailgun-validate-email", 3 | "version": "2.1.0", 4 | "description": "validate email addresses with mailgun API", 5 | "main": "src/index.js", 6 | "scripts": { 7 | "lint": "standard", 8 | "test": "node ./node_modules/.bin/lab -r tap tests/*-test.js | ./node_modules/.bin/tap-spec", 9 | "test-cov": "node ./node_modules/.bin/lab -t 75 tests/*-test.js", 10 | "test-cov-html": "node ./node_modules/.bin/lab -r html -o coverage.html tests/*-test.js" 11 | }, 12 | "pre-commit": [ 13 | "lint", 14 | "test", 15 | "test-cov" 16 | ], 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/diasdavid/mailgun-validate-email" 20 | }, 21 | "keywords": [ 22 | "mailgun", 23 | "email", 24 | "validate", 25 | "address", 26 | "spam", 27 | "scam", 28 | "valid" 29 | ], 30 | "author": "David Dias", 31 | "license": "MIT", 32 | "bugs": { 33 | "url": "https://github.com/diasdavid/mailgun-validate-email" 34 | }, 35 | "dependencies": { 36 | "request": "^2.25.0" 37 | }, 38 | "devDependencies": { 39 | "code": "^1.2.1", 40 | "jscs": "^1.7.3", 41 | "jshint": "^2.5.10", 42 | "lab": "^5.0.3", 43 | "pre-commit": "^1.1.2", 44 | "tap-spec": "^2.1.0" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | var request = require('request') 2 | 3 | module.exports = function (apiKey) { 4 | return function validator (email, cb) { 5 | var options = { 6 | url: 'https://api.mailgun.net/v3/address/validate', 7 | method: 'GET', 8 | timeout: 1500, 9 | qs: { 10 | address: email || '' 11 | }, 12 | auth: { 13 | username: 'api', 14 | password: apiKey || '' 15 | } 16 | } 17 | request(options, function (err, res, body) { 18 | if (err) { 19 | return cb(err) 20 | } 21 | if (body.length === 0 || res.statusCode !== 200) { 22 | return cb(new Error('mailgun replied with empty body')) 23 | } 24 | try { 25 | return cb(null, JSON.parse(body)) 26 | } catch (_err) { 27 | return cb(_err) 28 | } 29 | }) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/mail-test.js: -------------------------------------------------------------------------------- 1 | var Lab = require('lab') 2 | var Code = require('code') 3 | var lab = exports.lab = Lab.script() 4 | 5 | var experiment = lab.experiment 6 | var test = lab.test 7 | var before = lab.before 8 | var after = lab.after 9 | var expect = Code.expect 10 | var apiKey = 'pubkey-5ogiflzbnjrljiky49qxsiozqef5jxp7' 11 | var validator = require('./../src/index.js')(apiKey) 12 | 13 | experiment(': ', function () { 14 | before(function (done) { 15 | done() 16 | }) 17 | 18 | after(function (done) { 19 | done() 20 | }) 21 | 22 | test('valid email', function (done) { 23 | validator('banana@papaia.com', function (err, result) { 24 | expect(err).to.not.exist 25 | expect(result.is_valid).to.equal(true) 26 | done() 27 | }) 28 | }) 29 | 30 | test('non valid email', function (done) { 31 | validator('baasanana@psadasdasapaia.comasdasdas', function (err, result) { 32 | expect(err).to.not.exist 33 | expect(result.is_valid).to.equal(false) 34 | done() 35 | }) 36 | }) 37 | }) 38 | --------------------------------------------------------------------------------