ActionMailer & controller

Matt Stone wrote:


I have an actionmailer script that happily processes an inbox. It is
called via script & reads emails & grabs the attachments for further

I want to be able to access a method [process_attachment] in
app/controllers/application.rb, but the mailman routine does not appear
to be able to access it. I've tried both including environment.rb &
app/controllers/application.rb but still get the error - undefined
method 'process_attachment'

Is there anyway to do this, or do I have to move the logic somewhere


require File.dirname(__FILE__) + '/../../config/environment.rb'
require File.dirname(__FILE__) + '/../../app/controllers/application.rb'

class Inbound < ActiveRecord::Base

class Mailman < ActionMailer::Base

  def receive(email)
      email.attachments.each do |attachment|
        inbound =
        inbound.attachment = attachment


I'm not sure if this is the best method, its probably not even a good
method :] but it's something you could try in the meantime :slight_smile:

extract the method into a separate file into your /lib directory, aka.

Then require this file in either your environment.rb, or (less
blasphemously) in your mailer class.
Doesn't feel very elegant, I know, but I'm not sure if something like
helper_method exists for mailers...

Hope this helps...
Gustav Paul

Hi Matt,

Dumb question:
Where is the methode process_attachment currently defined?

Regards, Simon

Ah ok. Now I understand.

Your problem is, that ApplicationController is the parent class of all
Controllers in a Ruby on Rails Application.
But you extend ActionMailer and ActionMailer doesn't extends

You could do the following

class Mailman < ActionMailer::Base

  def receive(email)
      application =
      email.attachments.each do |attachment|
        inbound =
        inbound.attachment = attachment

I'm not sure if this is an elegant solution but it should work.
Otherwise put process_attachement in any class and you can instantiate
this instead of ApplicationController.

Cheers, Simon