From 1143e16354a4fdb68c4b2d537626f2580dd588a6 Mon Sep 17 00:00:00 2001 From: Priit Tark Date: Tue, 21 Jul 2015 16:34:43 +0300 Subject: [PATCH] Rescue contact registrant change email delivery #2745 --- app/mailers/application_mailer.rb | 21 ++++++++++++++++++++ app/mailers/contact_mailer.rb | 33 ++++++++++++++++++++----------- app/mailers/domain_mailer.rb | 18 ++++++----------- app/mailers/invoice_mailer.rb | 2 +- 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 62afa28b8..40266edb4 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,25 @@ class ApplicationMailer < ActionMailer::Base default from: 'noreply@internet.ee' layout 'mailer' + + def whitelist_blocked?(emails) + return false if Rails.env.production? || Rails.env.test? + + emails = [emails] unless emails.is_a?(Array) + emails = emails.flatten + emails.each do |email| + next unless TEST_EMAILS.include?(email) + logger.warn "EMAIL SENDING WAS BLOCKED BY WHITELIST: #{email}" + return true + end + false + end + + # turn on delivery on specific (epp) request only, thus rake tasks does not deliver anything + def delivery_off?(model) + return false if model.deliver_emails == true + logger.warn "EMAIL SENDING WAS NOT ACTIVATED " \ + "BY MODEL OBJECT: id ##{model.try(:id)} deliver_emails returned false" + true + end end diff --git a/app/mailers/contact_mailer.rb b/app/mailers/contact_mailer.rb index 800544b2e..859f73088 100644 --- a/app/mailers/contact_mailer.rb +++ b/app/mailers/contact_mailer.rb @@ -1,23 +1,34 @@ class ContactMailer < ApplicationMailer - # rubocop: disable Metrics/CyclomaticComplexity + # rubocop:disable Metrics/MethodLength def email_updated(contact) - unless Rails.env.production? - return unless TEST_EMAILS.include?(contact.email) || TEST_EMAILS.include?(contact.email_was) - end - - # turn on delivery on specific request only, thus rake tasks does not deliver anything - return if contact.deliver_emails != true + 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| - mail(to: email, subject: "#{I18n.t(:contact_email_update_subject)} [#{@contact.code}]") + 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 end end - # rubocop: enable Metrics/CyclomaticComplexity + # rubocop:enable Metrics/MethodLength end diff --git a/app/mailers/domain_mailer.rb b/app/mailers/domain_mailer.rb index 599658936..936a4559c 100644 --- a/app/mailers/domain_mailer.rb +++ b/app/mailers/domain_mailer.rb @@ -1,10 +1,7 @@ class DomainMailer < ApplicationMailer def registrant_pending_updated(domain) @domain = domain - return if Rails.env.production? ? false : !TEST_EMAILS.include?(@domain.registrant_email) - - # turn on delivery on specific EPP request only, thus rake tasks does not deliver anything - return if @domain.deliver_emails != true + return if delivery_off?(@domain) if @domain.registrant_verification_token.blank? logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}" @@ -21,27 +18,23 @@ class DomainMailer < ApplicationMailer confirm_path = "#{ENV['registrant_url']}/registrant/domain_update_confirms" @verification_url = "#{confirm_path}/#{@domain.id}?token=#{@domain.registrant_verification_token}" + return if whitelist_blocked?(@old_registrant.email) mail(to: @old_registrant.email, subject: "#{I18n.t(:domain_registrant_pending_updated_subject, name: @domain.name)} [#{@domain.name}]") end def registrant_updated(domain) @domain = domain - return if Rails.env.production? ? false : !TEST_EMAILS.include?(@domain.registrant_email) - - # turn on delivery on specific EPP request only, thus rake tasks does not deliver anything - return if @domain.deliver_emails != true + return if delivery_off?(@domain) + return if whitelist_blocked?(@domain.registrant_email) mail(to: @domain.registrant_email, subject: "#{I18n.t(:domain_registrant_updated, name: @domain.name)} [#{@domain.name}]") end def pending_deleted(domain) @domain = domain - return if Rails.env.production? ? false : !TEST_EMAILS.include?(@domain.registrant_email) - - # turn on delivery on specific EPP request only, thus rake tasks does not deliver anything - return if @domain.deliver_emails != true + return if delivery_off?(@domain) if @domain.registrant_verification_token.blank? logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}" @@ -58,6 +51,7 @@ class DomainMailer < ApplicationMailer confirm_path = "#{ENV['registrant_url']}/registrant/domain_delete_confirms" @verification_url = "#{confirm_path}/#{@domain.id}?token=#{@domain.registrant_verification_token}" + return if whitelist_blocked?(@old_registrant.email) mail(to: @old_registrant.email, subject: "#{I18n.t(:domain_pending_deleted_subject, name: @domain.name)} [#{@domain.name}]") end diff --git a/app/mailers/invoice_mailer.rb b/app/mailers/invoice_mailer.rb index eb6168d30..e93269e8e 100644 --- a/app/mailers/invoice_mailer.rb +++ b/app/mailers/invoice_mailer.rb @@ -1,6 +1,6 @@ class InvoiceMailer < ApplicationMailer def invoice_email(invoice, pdf) - return if Rails.env.production? ? false : !TEST_EMAILS.include?(invoice.billing_email) + return if whitelist_blocked?(invoice.billing_email) @invoice = invoice attachments[invoice.pdf_name] = pdf