Add support of punycode domains in emails

This commit is contained in:
Alex Sherman 2020-06-15 15:57:44 +05:00
parent 56ac816dd1
commit b4369bdcd0
6 changed files with 77 additions and 21 deletions

View file

@ -3,21 +3,54 @@ module Concerns
extend ActiveSupport::Concern
def email_verification
EmailAddressVerification.find_or_create_by(email: email.downcase,
domain: domain(email.downcase))
EmailAddressVerification.find_or_create_by(email: self.class.punycode_to_unicode(email),
domain: domain(email))
end
def billing_email_verification
return unless attribute_names.include?('billing_email')
EmailAddressVerification.find_or_create_by(email: billing_email.downcase,
domain: domain(email.downcase))
EmailAddressVerification.find_or_create_by(email: self.class
.punycode_to_unicode(billing_email),
domain: domain(billing_email))
end
class_methods do
def domain(email)
Mail::Address.new(email).domain&.downcase || 'not_found'
rescue Mail::Field::IncompleteParseError
'not_found'
end
def local(email)
Mail::Address.new(email).local&.downcase || email
rescue Mail::Field::IncompleteParseError
email
end
def punycode_to_unicode(email)
return email if domain(email) == 'not_found'
local = local(email)
domain = SimpleIDN.to_unicode(domain(email))
"#{local}@#{domain}"&.downcase
end
def unicode_to_punycode(email)
return email if domain(email) == 'not_found'
local = local(email)
domain = SimpleIDN.to_ascii(domain(email))
"#{local}@#{domain}"&.downcase
end
end
def domain(email)
Mail::Address.new(email).domain || 'not_found'
rescue Mail::Field::IncompleteParseError
'not_found'
SimpleIDN.to_unicode(self.class.domain(email))
end
def punycode_to_unicode(email)
self.class.punycode_to_unicode(email)
end
def verify_email_mx_smtp(field:, email:)

View file

@ -39,8 +39,7 @@ class EmailAddressVerification < ApplicationRecord
end
def verify
# media = success ? :mx : :smtp
media = :regex
media = :mx
validation_request = Truemail.validate(email, with: media)
if validation_request.result.success