hunk ./lib/email_address_validator/email_address_validator.rb 125 + debug "Validating #{address} with regex" hunk ./lib/email_address_validator/email_address_validator.rb 130 + debug "Validating #{address} with DNS" hunk ./Rakefile 54 - s.name = 'email_address_validator' + s.name = 'email-address-validator' hunk ./Rakefile 57 - s.homepage = "http://theshed.hezmatt.org/email_address_validator" + s.homepage = "http://theshed.hezmatt.org/email-address-validator" hunk ./lib/email_address_validator/regexp.rb 9 - QUOTED_STRING = /"(?:\s*#{QCONTENT})\s*"/ + QUOTED_STRING = /"(?:\s*#{QCONTENT})*\s*"/ hunk ./lib/email_address_validator/regexp.rb 18 - ADDR_SPEC = /(#{LOCAL_PART})@(#{DOMAIN})/ + ADDR_SPEC = /^(#{LOCAL_PART})@(#{DOMAIN})$/ hunk ./test/regex_test.rb 13 + + def test_more_interesting_addresses + ['"Abc\\@def"@example.com', + '"Fred Bloggs"@example.com', + '"Joe\\\\Blow"@example.com', + '"Abc@def"@example.com', + 'customer/department=shipping@example.com', + '$A12345@example.com', + '!def!xyz%abc@example.com', + '_somename@example.com', + '"test\\\\blah"@example.com', + '"test\\blah"@example.com', + '"test\\\\\\rblah"@example.com', + '"test\\"blah"@example.com', + 'customer/department@example.com', + '$A12345@example.com', + '!def!xyz%abc@example.com', + '_Yosemite.Sam@example.com', + '~@example.com', + '"Austin@Powers"@example.com', + 'Ima.Fool@example.com', + '"Ima.Fool"@example.com', + '"Ima Fool"@example.com' + ].each do |addr| + assert EmailAddressValidator.validate(addr) + end + end + + def test_invalid_addresses + ['NotAnEmail', + '@NotAnEmail', + '\\"test\\rblah\\"@example.com', + '"test"blah"@example.com', + '.wooly@example.com', + 'wo..oly@example.com', + 'pootietang.@example.com', + '.@example.com', + 'Ima Fool@example.com' + ].each do |addr| + assert !EmailAddressValidator.validate(addr) + end + end hunk ./test/regex_test.rb 56 - addfile ./README hunk ./README 1 +Email Address Validator +======================= + +Like any data, an e-mail address that you accept from a user cannot just +be accepted at face value, it needs to be validated. It's non-trivial to +do this, though -- not only do you have to make sure that it is formatted +correctly (and this is harder than it first appears, as the rules for what +constitute a valid e-mail address aren't simple), you also have to make +sure that the e-mail address will actually receive mail. + +The EmailAddressValidator class tries, as much as possible without actually +delivering an e-mail, to validate that an e-mail address is valid and will +accept e-mail. It does this in three stages: + +- Ensure that the e-mail address is correctly formatted, using a regular + expression derived from the ABNF specified in RFC2822 for maximum + correctness. This takes very little resources. + +- Lookup MX records or A records for the domain part of the e-mail + address, to make sure that there's someone who might be willing to + receive mail. This lookup requires network access and take a little bit + of time, but can catch a lot of simple typos. + +- Make an attempt to deliver an e-mail to the address, stopping just + before sending the message body. This is the most accurate way to + discover whether an e-mail will be delivered, although it is still not + 100% accurate (for example, Exchange servers will accept all mail at + SMTP time and then generate a bounce, so an SMTP-time test won't tell + you anything useful there). However, most e-mail services will do the + right thing, and it is the only way to detect a typo'd local part. It + does take a little while to perform this lookup, though. + +See the rdoc/ri documentation for full information on how to use this class, +or just look in lib/email_address_validator/email_address_validator.rb. hunk ./lib/email_address_validator/email_address_validator.rb 1 +# email_address_validator.rb -- class to validate RFC2822 e-mail addresses +# Copyright (C) 2008 Matt Palmer +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + hunk ./lib/email_address_validator/regexp.rb 1 +# regexp.rb -- regular expression elements to validate RFC2822 e-mail addresses +# Copyright (C) 2008 Matt Palmer +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + hunk ./Rakefile 13 -PROJECT_VERSION = '0.0.1' +PROJECT_VERSION = '0.0.2'