module CogiEmail
Constants
- VERSION
Public Class Methods
Normalize email address.
You need to validate before normalize an email address.
@param [String] email Email address
@return [String] Email address in lowercase, without special characters.
@raise [CogiEmail::NormalizationError] If can not normalize the email address.
@example
CogiEmail.normalize('(Peter Brown)<peter_brown@example.com>') # => peter_brown@example.com CogiEmail.normalize('peter_brown@example.com') # => peter_brown@example.com CogiEmail.normalize('Peter_Brown@example.com') # => peter_brown@example.com CogiEmail.normalize('peter brown@example.com') # => peter_brown@example.com
# File lib/cogi_email.rb 47 def self.normalize(email) 48 begin 49 m = Mail::Address.new(email) 50 m.address.downcase 51 rescue 52 raise CogiEmail::NormalizationError 53 end 54 end
Check if an email address is real or not.
An email address is real if:
Valid Has MX DNS record Can send test email
@param [String] email Email address
@return [Boolean] True if email address is real, otherwise False
@example
CogiEmail.real_email?('nobi.younet@gmail.com') # => true CogiEmail.real_email?('nobi.younet@example.com') # => false
# File lib/cogi_email.rb 93 def self.real_email?(email) 94 return false unless self.validate?(email) # not a valid email address 95 result = true 96 97 begin 98 v = CogiEmail::Checker.new(email) 99 v.connect 100 v.verify 101 rescue 102 result = false 103 end 104 105 result 106 end
Check if email domain is valid by making a DNS lookup.
An email domain is valid if it has an MX DNS record is set up to receive mail.
Reference: www.safaribooksonline.com/library/view/ruby-cookbook/0596523696/ch01s19.html
@param [String] email Email address
@return [Boolean] True if email domain have valid MX DNS record, otherwise False
# File lib/cogi_email.rb 65 def self.valid_email_domain?(email) 66 valid = true 67 68 begin 69 m = Mail::Address.new(email) 70 hostname = m.domain 71 Resolv::DNS.new.getresource(hostname, Resolv::DNS::Resource::IN::MX) 72 rescue Resolv::ResolvError 73 valid = false 74 end 75 76 valid 77 end
Check if a string is a valid email address.
This library is so strict to ensure that all email is clean and deliverable.
References:
https://www.jochentopf.com/email/ https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php
@param [String] email Email address
@return [Boolean] True if it is in a valid email, otherwise False
@example
CogiEmail.validate?('peter_brown@example.com') # => true CogiEmail.validate?('peter-brown@example.com') # => true CogiEmail.validate?('peter.brown@example.com') # => true CogiEmail.validate?('peter brown@example.com') # => false CogiEmail.validate?('peter_brown@@example.com') # => false
# File lib/cogi_email.rb 27 def self.validate?(email) 28 pattern = /\A\s*([-\p{L}\d+._]{1,64})@((?:[-\p{L}\d]+\.)+\p{L}{2,})\s*\z/i 29 !!(email =~ pattern) 30 end