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 What I'd like to do is have nothing generated at all. Here is the email from the deliveries array:deliver
method is generating an empty email?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With