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

View file

@ -42,7 +42,14 @@ class Contact < ActiveRecord::Base
before_update :manage_emails before_update :manage_emails
def manage_emails def manage_emails
return nil unless email_changed? 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 end
before_save :manage_statuses before_save :manage_statuses

View file

@ -13,4 +13,8 @@ TEST_EMAILS = %w(
info@gitlab.eu info@gitlab.eu
test@example.com test@example.com
test@example.org test@example.org
old@example.org
new@example.org
old@example.com
new@example.com
) )

View file

@ -4,8 +4,7 @@ describe ContactMailer do
describe 'email changed notification when delivery turned off' do describe 'email changed notification when delivery turned off' do
before :all do before :all do
@contact = Fabricate(:contact, email: 'test@example.ee') @contact = Fabricate(:contact, email: 'test@example.ee')
@contact.email = 'test@example.com' # new email @mail = ContactMailer.email_updated('test@example.com', @contact)
@mail = ContactMailer.email_updated(@contact)
end end
it 'should not render email subject' do it 'should not render email subject' do
@ -31,8 +30,7 @@ describe ContactMailer do
@contact = @domain.registrant @contact = @domain.registrant
@contact.reload # until figured out why registrant_domains not loaded @contact.reload # until figured out why registrant_domains not loaded
@contact.deliver_emails = true @contact.deliver_emails = true
@contact.email = 'test@example.org' # new email @mail = ContactMailer.email_updated('info@example.org', @contact)
@mail = ContactMailer.email_updated(@contact)
end end
it 'should render email subject' do it 'should render email subject' do
@ -43,9 +41,8 @@ describe ContactMailer do
@mail.from.should == ["noreply@internet.ee"] @mail.from.should == ["noreply@internet.ee"]
end end
it 'should have both old and new receiver email' do it 'should send to info email' do
@mail.to.size.should == 2 @mail.to.should == ['info@example.org']
@mail.to.include? "test@example.org"
end end
it 'should render body' do it 'should render body' do