ankane/ahoy_email

Setting an email not to send via ActionMailer::Base#perform_deliveries still records an ahoy_messages record

cannikin opened this issue · 5 comments

In our Rails app we've got a Mailer Interceptor https://wearestac.com/blog/action-mailer-interceptors that prevents certain emails from being sent (like when their subscription expires, they should receive certain notifications any more). The mailers will be invoked as usual in the app, but the interceptor will cancel the delivery at the last moment if certain conditions are met.

Expected behavior: the email isn't sent and an ahoy_messages entry is not created in the database

Actual behavior: The email isn't being sent, but an ahoy_messages entry is created in the database.

Hey @cannikin, if your custom interceptor intercepts before it reaches the Ahoy interceptor, you can use the sent_at column to tell if the message was actually sent.

@ankane Hmm...in our database I'm still seeing a sent_at for all of these emails. Our interceptor is created in config/initializers and it looks like it's added AFTER the ahoy_email interceptor (since the gems are included first). As far as I can tell there's no way to prepend an interceptor. I went down the path of adding that functionality but it's going to involve changes to both the Mail and ActionMailer gem and that seems like a tall order!

I wonder if I did a :require => false on ahoy_email and then a require 'ahoy_email' in my initializer, after registering my own interceptor? Hmm...

Nope, doesn't like that... complains about Ahoy::Message not existing...something about the order things are initialized doesn't work so well with Engines maybe?

Hey @cannikin, looks like you can unregister interceptors. rails/rails#18446

I'd try doing that and re-registering after your own interceptor.

Cleaning up stale issues