How I Learned to Stop Hating and Love Action Mailer

My biggest gripe with ActionMailer is how difficult it is to generate URL’s. It’s common enough when sending an email that it includes a link. But ActionMailer, by default, gives you no access to url_for and named routes. Ugh!

Even if you’re clever enough to do something like:

class ActionMailer::Base
include ActionController::UrlWriter
end

You’re still screwed as you need to know the host, port, protocol, etc. to generate links. These data can be set globally, but by far the easiest and most flexible way is to get them is from the request object.

Passing around the host, port, etc.

The request object, is (of course) only available to Controllers. So the data need to be passed from the Controller to the mailer, like:

Of course, if you’ve read my previous post you know I HATE polluting my Controllers with business logic like this. I strongly prefer pushing this “triggered action” into the model:

class User
after_create :send_email
end

The cost of this is that I now need to past the host, etc. down into my model so it can pass it on to the mailer! The Java Programmers over here just laugh at me for not having a real Dependency Injection framework, which they say would solve this handily. Some stupid Java framework solving this problem better than Rails?! This makes me MAD AS HELL!

Enter the Global Variable

Screw Dependency Injection. I’m going to use a Global Variable like every other God Fearing Rails programmer. His Excellency DHH said let there be cattr_accessor and it was Good.

Step one, set the around-filter in your ApplicationController:

around_filter :retardase_inhibitor

Step two,

THERE IS NO STEP TWO

You don’t need to pass around host. You can generate URL’s in ActionMailer no problem. Let’s look at how this is done:

Well, get over it. How do you think with_scope works? How do you think you can call a Finder like User.find or User.new wherever you feel like? It’s called Global Variables, man. Embrace it. I call this liberation theology.

21 Comments

Nathan Sobo says:

What rhetorical flair. It fills me with delight.

December 12, 2007 at 11:50 pm

Cody Caughlan says:

If you are calling your mailer from a controller and using something like a hash to pass into your deliver method then you can just assign a hash key/value pair of “controller” / self and you then have transparent access to url_for and friends. E.g.

Cody thanks for the suggestion. The disadvantage of this approach is the need to pass around stuff. I want to hide from the controller the fact that a model has a triggered action. In the worst case a model creates another as an after create, that creates another, etc. Finally the last model in the chain sends an email. Crap — now you have to pass around the controller n frames down the call stack. Not only that, but to unit test the last model you have to create a mock controller.

ActionMailer “already includes the UrlWriter”:http://dev.rubyonrails.org/browser/tags/rel1-2-3/actionmailer/lib/actionmailer/base.rb#L222, it just needs the host and port to generate the URLs. I would rather use defaulturloptions (already built-in):
class Notifier < ActionMailer::Base
defaulturloptions[:host] = DEFAULT_HOST
defaulturloptions[:port] = DEFAULT_PORT

And then define the host and port in the environment. The benefit of taking the info from the request in the controller (like you’re doing) is that you don’t have to do this configuration, you can have multiple domains and still generate the corresponding URLs, etc. But if you’re sending e-mail from outside the context of a request (such as some async process), you still need to specify a host, and this is the easiest way to do that.

To work much better. I was having problems with the default port getting into my production url string and I was occasionally having problems with the port getting set to nil. This fixed both issues.

December 12, 2007 at 11:50 pm

Alex C says:

Brilliant! I’m perfectly happy using a hack to fix a broken framework, and the name (“retardase inhibitor”) makes it crystal clear there are no heroes in this story.

One way to make this palatable as a patch to core would be to put the settings-saving and -restoring methods on UrlWriter, not ActionController. Call them “push_settings” and “pop_settings” perhaps. Then your filter would be

You should do it all in the controller, because you cannot expect the MyMailer-model to always run in a context, where the routes are available. Like ActiveRecord-models, the ActionMailer-models could also be used from a shell script or a cron job.