# ActionMailer & controller

**URL:** <https://www.ruby-forum.com/t/actionmailer-controller/76023>\
**Category:** Rails\
**Created:** [November 15, 2006, 10:57am UTC](https://www.ruby-forum.com/t/actionmailer-controller/76023 "2006-11-15T10:57:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [November 15, 2006, 10:57am UTC](https://www.ruby-forum.com/t/actionmailer-controller/76023/1 "2006-11-15T10:57:20Z")

</div>

Hi,

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

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  
else…??

* * *

## mailman.rb

require File.dirname( **FILE** ) + ‘/…/…/config/environment.rb’  
require File.dirname( **FILE** ) + ‘/…/…/app/controllers/application.rb’

class Inbound \< ActiveRecord::Base  
end

class Mailman \< ActionMailer::Base

def receive(email)  
email.attachments.each do |attachment|  
inbound = Inbound.new  
inbound.attachment = attachment  
process\_attachment(inbound)  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [November 15, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/actionmailer-controller/76023/2 "2006-11-15T12:15:37Z")

</div>

Matt S. wrote:

> method ‘process\_attachment’
> 
> ```
> end
> end
> 
> ```
> 
> end

Hey

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 🙂

extract the method into a separate file into your /lib directory, aka.  
/lib/my\_truly\_global\_methods.rb

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…  
Cheery-o  
Gustav P.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [November 15, 2006, 3:30pm UTC](https://www.ruby-forum.com/t/actionmailer-controller/76023/3 "2006-11-15T15:30:14Z")

</div>

Hi Matt,

Dumb question:  
Where is the methode process\_attachment currently defined?

Regards, Simon

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [November 15, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/actionmailer-controller/76023/4 "2006-11-15T19:22:38Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi Matt,
> 
> Dumb question:  
> Where is the methode process\_attachment currently defined?
> 
> Regards, Simon

Simon,

Thanks for your response, I really appreciate it.

process\_attachement is defined in app/controller/application.rb. It also  
processes uploaded files.

I want to have the same logic process the files uploaded via a browser,  
& files sent as attachments.

I’m really confused as to how is the best way to achieve this.

rgds,

- matt.

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [November 16, 2006, 1:04pm UTC](https://www.ruby-forum.com/t/actionmailer-controller/76023/5 "2006-11-16T13:04:16Z")

</div>

Simon,

Thanks for that - works perfectly. The explanation was great, I know  
understand why it works perfectly.

rgds,

- matt.

removed\_email\_address@domain.invalid wrote:

> 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  
> ApplicationController.
> 
> You could do the following
> 
> class Mailman \< ActionMailer::Base
> 
> def receive(email)  
> application = ApplicationController.new  
> email.attachments.each do |attachment|  
> inbound = Inbound.new  
> inbound.attachment = attachment  
> application.process\_attachment(inbound)  
> end  
> end  
> end
> 
> 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

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [November 15, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/actionmailer-controller/76023/6 "2006-11-15T20:23:24Z")

</div>

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  
ApplicationController.

You could do the following

class Mailman \< ActionMailer::Base

def receive(email)  
application = ApplicationController.new  
email.attachments.each do |attachment|  
inbound = Inbound.new  
inbound.attachment = attachment  
application.process\_attachment(inbound)  
end  
end  
end

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
