A code snippet that will save you from misspelled emails

You're scrolling through your email list and you notice something. It's an email address that is almost correct. It reads "john@gmali.com". Well, that's unfortunate. Because after a close examination you notice that 1 percent of the entire list is misspelled. Your entire marketing efforts could be 1 percent better if you could fix it!

 

A first idea might pop into your mind. Just go through your entire list and automatically replace misspelled emails (@gmaill.com, @gmal, @gmel, @gmial). Although it might be a solid technical solution, it poses an ethical and regulatory question. Are you allowed to use these "new" emails for your marketing campaigns? Because the user submitted the incorrect email, you were never given permission to use the correct email.

 

Solving problems before they occur

What if I told you that you could prevent yourself from having this problem in the first place? Yes, with the help of your developers and a little code snippet to get them moving you could dodge the bullet.

 

The idea is simple. If we notice a misspelled email and offer them a fixed version, I'll assure you that most of them will accept their email without a typo.

notion image
 

Since most of the people use Gmail and Yahoo as their main email outside of work, we will run a spell checker for only those domains (gmail.com and yahoo.com).

 

A magical function

All it does is finds if an email is misspelled and offers a fix.

var stringSimilarity = require("string-similarity");

function isMisspelled(email) {
  //you can always change this threshold to change sensitivity
  const threshhold = 0.625;
  let providedDomain = email.split("@")[1];
  console.log(providedDomain);

  var matches = stringSimilarity.findBestMatch(providedDomain, [
    "yahoo.com",
    "gmail.com",
    "theatre",
  ]);

  let bestMatch = matches.ratings.sort((a, b) =>
    a.rating < b.rating ? 1 : -1
  )[0];

  if (bestMatch.rating >= threshhold && bestMatch.rating < 1) {
    return { result: true, fix: `${email.split("@")[0]}@${bestMatch.target}` };
  } else {
    return { result: false };
  }
}

console.log(isMisspelled("john@gmial.com"));
//{ result: true, fix: 'john@gmail.com' }

console.log(isMisspelled("john@gmail.com"));
//{ result: false }
 

Pass this to your developers to help them implement this change. This solution will fix the misspelled issue and your email list will grow.


Do you want to read more stories like that?

Leave your info on the bottom to subscribe 👇