SixArm.com » Ruby »
Email address validation using RFC 822

Introduction

Email address regular expression to validate an email address using RFC 822.

The original PHP code is by Cal Henderson, see iamcal.com/publish/articles/php/parsing_email/

Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb, see tfletcher.com/lib/rfc822.rb

For docs go to sixarm.com/sixarm_ruby_email_address_validation/doc

Want to help? We're happy to get pull requests.

Related links:

Install quickstart

Install:

gem install sixarm_ruby_email_address_validation

Bundler:

gem "sixarm_ruby_email_address_validation", ">=2.0.1", "<2"

Require:

require "sixarm_ruby_email_address_validation"

Install with security (optional)

To enable high security for all our gems:

wget http://sixarm.com/sixarm.pem
gem cert --add sixarm.pem
gem sources --add http://sixarm.com

To install with high security:

gem install sixarm_ruby_email_address_validation --trust-policy HighSecurity

Example

if EmailAddressValidation::Pattern=~"foo@bar.com"
   ...
end

Pattern Match

To find an email address in a string, do the pattern match then use the result, which is the match's string position:

Example of match position:

EmailAddressValidation::Pattern=~'foo@bar.com'
=> 0

EmailAddressValidation::Pattern=~'... foo@bar.com ...'
=> 4

EmailAddressValidation::Pattern=~'... hello world ...'
=> nil

Pattern Match Exact

To do a pattern match to the entire string, use the EmailAddressValidation::PatternExact.

The entire string must be one email address.

Example of pattern match:

if EmailAddressValidation::PatternExact=~'foo@bar.com'
#=> truthy

if EmailAddressValidation::PatternExact=~'... foo@bar.com ...'
#=> falsey

This pattern is especialy useful to validate an email address.

Example to validate an email address:

def valid?(email_address)
  EmailAddressValidation::PatternExact=~email_address ? true : false
end

Rails Validation (optional)

To add email address validation to a typical Ruby On Rails model:

class User
  include EmailAddressValidation
  validates :email_address, :format => { :with => EmailAddressValidation::PatternExact }
end

Frequently Asked Questions

Q. Does this handle unusual email addresses, like foo+bar@my.com?

A. Yes. It handles all RFC email addresses. If you find an RFC email address that fails, please let us know.

Q. Why use this in a Rails app?

A. We use it to detect a potential typo during user registration, so we can prompt the user to correct it before we send a typical welcome email with a registration link. If we didn't validate the email address format, then we would have sent the welcome email to the wrong user, or into the void.

Q. Why use this to get more than one email address?

A. We use it to find typos in large databases of email addresses where it's not a business option to send the user an email. For example, proofing an existing report of 10 million users in a CSV file. We need to contact anyone with a malformed email address, so we actually call them if we have their phone number. We can't send these people an email, because the address is invalid.