Fix domain expiration mailer email validation

This commit is contained in:
Alex Sherman 2021-03-23 12:50:25 +05:00
parent b5cbb33e1c
commit ab248a641e
7 changed files with 64 additions and 42 deletions

View file

@ -11,7 +11,11 @@ module Domains
saved = domain.save(validate: false)
DomainExpireEmailJob.enqueue(domain.id, run_at: send_time) if saved
return unless saved
recipients.each do |recipient|
DomainExpireEmailJob.enqueue(domain.id, recipient, run_at: send_time)
end
end
def set_graceful_expired
@ -23,6 +27,23 @@ module Domains
def send_time
domain.valid_to + Setting.expiration_reminder_mail.to_i.days
end
def recipients
filter_invalid_emails(domain.expired_domain_contact_emails)
end
def filter_invalid_emails(emails)
emails.select do |email|
valid = Truemail.valid?(email)
unless valid
logger.info('Unable to send DomainExpireMailer#expired email for'\
"domain #{domain.name} (##{domain.id}) to invalid recipient #{email}")
end
valid
end
end
end
end
end

View file

@ -1,13 +1,19 @@
class DomainExpireEmailJob < Que::Job
def run(domain_id)
def run(domain_id, email)
domain = Domain.find(domain_id)
return if domain.registered?
attrs = {
domain: domain,
registrar: domain.registrar,
email: email,
}
if domain.force_delete_scheduled?
DomainExpireMailer.expired_soft(domain: domain, registrar: domain.registrar).deliver_now
DomainExpireMailer.expired_soft(attrs).deliver_now
else
DomainExpireMailer.expired(domain: domain, registrar: domain.registrar).deliver_now
DomainExpireMailer.expired(attrs).deliver_now
end
end
end

View file

@ -1,23 +1,23 @@
class DomainExpireMailer < ApplicationMailer
attr_accessor :domain, :registrar
attr_accessor :domain, :registrar, :email
def expired(domain:, registrar:)
process_mail(domain: domain, registrar: registrar, method_name: __method__.to_s)
def expired(domain:, registrar:, email:)
process_mail(domain: domain, registrar: registrar, email: email, method_name: __method__.to_s)
end
def expired_soft(domain:, registrar:)
process_mail(domain: domain, registrar: registrar, method_name: __method__.to_s)
def expired_soft(domain:, registrar:, email:)
process_mail(domain: domain, registrar: registrar, email: email, method_name: __method__.to_s)
end
private
def process_mail(domain:, registrar:, method_name:)
def process_mail(domain:, registrar:, email:, method_name:)
init(domain, registrar)
logger.info("Send DomainExpireMailer##{method_name} email for #{domain.name} (##{domain.id})" \
" to #{recipient(domain).join(', ')}")
" to #{email}")
mail(to: recipient(domain), subject: subject(method_name))
mail(to: email, subject: subject(method_name))
end
def init(domain, registrar)
@ -25,10 +25,6 @@ class DomainExpireMailer < ApplicationMailer
@registrar = registrar_presenter(registrar: registrar)
end
def recipient(domain)
filter_invalid_emails(emails: domain.primary_contact_emails, domain: @domain)
end
def subject(method_name)
I18n.t("domain_expire_mailer.#{method_name}.subject", domain_name: @domain.name)
end
@ -40,23 +36,4 @@ class DomainExpireMailer < ApplicationMailer
def registrar_presenter(registrar:)
RegistrarPresenter.new(registrar: registrar, view: view_context)
end
# Needed because there are invalid emails in the database, which have been imported from legacy app
def filter_invalid_emails(emails:, domain:)
old_validation_type = Truemail.configure.default_validation_type
Truemail.configure.default_validation_type = :regex
results = emails.select do |email|
valid = Truemail.valid?(email)
unless valid
logger.info("Unable to send DomainExpireMailer#expired email for domain #{domain.name} (##{domain.id})" \
" to invalid recipient #{email}")
end
valid
end
Truemail.configure.default_validation_type = old_validation_type
results
end
end

View file

@ -632,6 +632,12 @@ class Domain < ApplicationRecord
(admin_contacts.emails + [registrant.email]).uniq
end
def expired_domain_contact_emails
force_delete_contact_emails.reject do |email|
BouncedMailAddress.where(email: email).count.positive?
end
end
def force_delete_contact_emails
(primary_contact_emails + tech_contacts.pluck(:email) +
["info@#{name}", "#{prepared_domain_name}@#{name}"]).uniq