Refactor emails #2786

This commit is contained in:
Priit Tark 2015-07-28 20:28:26 +03:00
parent 1c7febf47a
commit e876bf59a5
4 changed files with 34 additions and 32 deletions

View file

@ -1,33 +1,27 @@
class ContactMailer < ApplicationMailer
# rubocop:disable Metrics/MethodLength
def email_updated(contact)
def email_updated(email, contact)
return if delivery_off?(contact)
@contact = contact
emails = []
emails << [@contact.email, @contact.email_was] if @contact.registrant_domains.present?
emails << @contact.domains.map(&:registrant_email) if @contact.domains.present?
emails = emails.uniq
return if whitelist_blocked?(emails)
emails.each do |email|
begin
mail(to: email, subject: "#{I18n.t(:contact_email_update_subject)} [#{@contact.code}]")
rescue EOFError,
IOError,
TimeoutError,
Errno::ECONNRESET,
Errno::ECONNABORTED,
Errno::EPIPE,
Errno::ETIMEDOUT,
Net::SMTPAuthenticationError,
Net::SMTPServerBusy,
Net::SMTPFatalError,
Net::SMTPSyntaxError,
Net::SMTPUnknownError,
OpenSSL::SSL::SSLError => e
logger.warn "EMAIL SENDING FAILED: #{email}: #{e}"
end
return if whitelist_blocked?(email)
begin
mail(to: email, subject: "#{I18n.t(:contact_email_update_subject)} [#{@contact.code}]")
rescue EOFError,
IOError,
TimeoutError,
Errno::ECONNRESET,
Errno::ECONNABORTED,
Errno::EPIPE,
Errno::ETIMEDOUT,
Net::SMTPAuthenticationError,
Net::SMTPServerBusy,
Net::SMTPFatalError,
Net::SMTPSyntaxError,
Net::SMTPUnknownError,
OpenSSL::SSL::SSLError => e
logger.info "EMAIL SENDING FAILED: #{email}: #{e}"
end
end
# rubocop:enable Metrics/MethodLength

View file

@ -42,7 +42,14 @@ class Contact < ActiveRecord::Base
before_update :manage_emails
def manage_emails
return nil unless email_changed?
ContactMailer.email_updated(self).deliver_now
return nil unless deliver_emails == true
emails = []
emails << [email, email_was]
emails << domains.map(&:registrant_email) if domains.present?
emails = emails.flatten.uniq
emails.each do |e|
ContactMailer.email_updated(e, contact).deliver_now
end
end
before_save :manage_statuses