Client/server Meteor package to normalize email addresses to their canonical form:
- remove dots in GMail or email addresses hosted by similar services (Google Apps for Work, FastMail)
- remove address tags starting with '+', or '-' for Yahoo!, or '=' as well if desired
- converts alias domains to the canonical one, e.g. googlemail.com to gmail
This is useful for a variety of reasons:
- Get the real Gravatar of a user who registers for your service with theiremail+yourservice@gmail.com. We use this at iDoRecall.
- Detect duplicate emails for fraud prevention.
gmail.com
,googlemail.com
andgoogle.com
- the only domains Google is known to use for incoming email- Any domain hosted with Google Apps for Work
- FastMail and domains hosted with FastMail
- Microsoft's outlook.com, hotmail.com, live.com
- Yahoo! domains
Pull requests to factor out the Meteor-independent code to use as a Node package are welcome.
The most common and general invocation is asynchronous:
let normalizedEmail = Email.normalize('a.b.c+tag@domain.com', {detectProvider: true}, function callback(error, result) {
// result will be abc@domain.com if they use Google Apps, or unchanged otherwise
});
The general syntax is:
var normalizedEmail = Email.normalize(emailAddress, options, [callback]); // abc@gmail.com
options
is an object with the following keys, all optional:
forceRemovePeriods
- defaultfalse
forceRemoveTags
- defaultfalse
; if true, will remove anything between the first '+', '-' or '=' and the '@' signdetectProvider
- defaultfalse
because it makes a DNS lookup request for the MX record of the domain to see if it might be a Google Apps for Business or a Fastmail domain, in which case appropriate rules will apply. IfdetectProvider
is true, a callback is required on the client and optional on the server.callback
- only required ifdetectProvider
is true, and the code runs on the client
The email provider detection is done on the client by using the Enclout DNS API. If you run into any rate limits, please file an issue to add support for authentication.
If you're using this package to canonicalize email addresses for Gravatar, make sure to search (i.e. compute the hash for) the original email address first, since the user might legitimately always use something like first.last@gmail.com
. Also, the original email address should always have priority (consider a user who has Gravatars for both user+games@example.com
and user+professional@example.com
).
When sending email use the original as well - it's respectful to your users to email them at the address they have supplied, since custom filter rules might depend on it.
- normalize-email npm package - doesn't support Yahoo! or FastMail. We started contributing by fixing the naive removal of dots from Microsoft accounts (which was an error), but it quickly became apparent that a more comprehensive approach was needed.
- djanho-canonical-email - written in Python, doesn't support anything but Gmail
- soundcloud/normailize - Ruby, not maintained since March 2013
Maintainer: Dan Dascalescu (@dandv)
Copyright (C) 2015 iDoRecall, Inc.
The MIT License (MIT)