Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to avoid sending emails when email address ends with ".old"

I have a requirement to override ActionMailer's mail method. This method is protected so in the subclass, I also define the mail method as protected:

protected

def mail(headers={}, &block)
  #add a check to avoid sending emails that end with ".old"
  unless headers[:to] =~ /\.old$/
    super(headers, &block)
  end
end

this way, when an outgoing email address ends with .old, the method should return nil and not send an email.

However, in my unit test, it seems that an email is still going to ActionMailer::Base.deliveries

here is my unit test:

describe 'BaseNotifier' do

  class TestNotifier < BaseNotifier
    def mailer(to, from, subject)
      mail(:to => to,
          :from => from,
          :subject => subject)
    end
  end

  before(:each) do
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries.clear
  end

  it "should not send emails to a user with an email that ends in '.old'" do
    TestNotifier.mailer("[email protected]", "[email protected]", "test email").deliver
    puts "what do we have here " + ActionMailer::Base.deliveries.first.to_s
    ActionMailer::Base.deliveries.length.should == 0
  end
end

I create a test class and subclass the class where mail is overridden (this is how it will be used in the system). I then make an email and call the .deliver method.

UPDATE: i think the deliver method is not the issue. When I do this: puts TestNotifier.mailer("[email protected]", "[email protected]", "test email") I get the same email message as below. I also tried to override the mail method in this way:

def mail(headers={}, &block)
  #add a check to avoid sending emails that end with ".old"
  unless headers[:to] =~ /\.old$/
    super(headers, &block)
  else
    super({:to=>nil, :from=>nil, :boundary =>nil, :date => nil, :message =>nil, :subject =>nil, :mime_version => nil,
           :content_type => nil, :charset =>nil, :content_transfer_encoding => nil}, &block)
  end

end

which gives me a failure with undefined method 'ascii_only?' for nil:NilClass on this line: TestNotifier.mailer("[email protected]", "[email protected]", "test email").deliver

UPDATE: I've also tried doing this with the overridden mail method:

#create a class that overrides the deliver method to do nothing. class NonSendingEmail; def deliver; end; end

def mail(headers={}, &block) #add a check to avoid sending emails that end with ".old" unless headers[:to] =~ /.old$/ super(headers, &block) else NonSendingEmail.new end end

but i'm still having the same result.

It seems like the deliver method is generating an empty email? What I'd like to do is have nothing generated at all. Here is the email from the deliveries array:

Date: Thu, 19 Jan 2012 00:00:00 +0000
Message-ID: <[email protected]>
Mime-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
like image 859
Ramy Avatar asked Jan 19 '12 15:01

Ramy


1 Answers

you are editing the wrong place. changing the mail method to return nil won't help as the mailer then uses an empty message. you can instead patch the mail class (working with rails 3):

module Mail
  class Message
    alias :old_deliver :deliver
    def deliver
      old_deliver unless to.first =~ /\.old$/
    end
  end
end
like image 152
phoet Avatar answered Oct 23 '22 16:10

phoet