# RobustThread, Postfix, TMail, & Rails app small question

**URL:** <https://www.ruby-forum.com/t/robustthread-postfix-tmail-rails-app-small-question/172853>\
**Category:** Rails\
**Created:** [July 28, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/robustthread-postfix-tmail-rails-app-small-question/172853 "2009-07-28T23:57:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nateleavitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@nateleavitt](https://www.ruby-forum.com/u/nateleavitt)\
**Post date:** [July 28, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/robustthread-postfix-tmail-rails-app-small-question/172853/1 "2009-07-28T23:57:31Z")

</div>

I’m using Postfix to receive email then having it dump the emails to a  
Maildir. From there I have a ruby daemon using RobustThread which runs  
every 30 seconds to process the emails using Tmail. Everything is  
working great up to this point. With TMail I’m able to get the parts of  
the email that I need.

The only question I have is how would be the best way to get the emails,  
from my ruby daemon, into my rails app as a Model and save to database?

---

<div class="post-metadata">

**Author:** ![nateleavitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@nateleavitt](https://www.ruby-forum.com/u/nateleavitt)\
**Post date:** [July 29, 2009, 10:06pm UTC](https://www.ruby-forum.com/t/robustthread-postfix-tmail-rails-app-small-question/172853/2 "2009-07-29T22:06:02Z")

</div>

> from my ruby daemon, into my rails app as a Model and save to  
> database?

You can use Active Record outside of Rails… so you should be able to  
just load it up and use the Model directly (assuming you aren’t  
relying on something within your model that is Rails specific).

---

<div class="post-metadata">

**Author:** ![nateleavitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@nateleavitt](https://www.ruby-forum.com/u/nateleavitt)\
**Post date:** [July 30, 2009, 1:58am UTC](https://www.ruby-forum.com/t/robustthread-postfix-tmail-rails-app-small-question/172853/3 "2009-07-30T01:58:37Z")

</div>

Philip H. wrote:

> > from my ruby daemon, into my rails app as a Model and save to  
> > database?

# Thanks for the reply! I guess I just want to make sure that this works and is a good way to do it. Here is what I have in my daemon below:

require ‘rubygems’  
require ‘robustthread’  
require ‘tmail’  
require ‘activerecord’

conf = YAML::load(File.open(File.dirname( **FILE** ) +  
‘/var/www/rails/somedomain/current/config/database.yml’))  
ActiveRecord::Base.establish\_connection(conf[environment])

RobustThread.logger = Logger.new( ‘somedomain\_mail.log’ )  
RobustThread.exception\_handler do | exception |  
email\_me\_the\_exception( exception )  
end

pid = fork do  
RobustThread.loop( :seconds =\> 30, :label =\> “Processing emails…” )  
do  
#check for emails in the maildir  
Dir["/home/nate/Maildir/new/ **/**"].each do | thisfile |

```
mail = TMail::Mail.load( thisfile )
@subject = mail.subject
@wr_token = mail.to.first.gsub(/[services\removed_email_address@domain.invalid]/, '')
@body = mail.body.gsub(/\/\/ Add your reply above here.*$/m, '')

@user = User.find_by_email(mail.from, :select => :id)
@work_request = WorkRequest.find_by_access_token(@wr_token, :select 

```

=\> :id)  
@log\_item = LogItem.new(  
:user\_id =\> @user.id,  
:work\_request\_id =\> @work\_request.id,  
:comment\_textile =\> @body  
)  
if @log\_item.save  
bla…  
else  
bla…  
end

end  
end  
sleep  
end  
Process.detach pid

class LogItem \< ActiveRecord::Base  
end

class User \< ActiveRecord::Base  
end

class WorkRequest \< ActiveRecord::Base  
end

---

<div class="post-metadata">

**Author:** ![nateleavitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@nateleavitt](https://www.ruby-forum.com/u/nateleavitt)\
**Post date:** [July 30, 2009, 2:09am UTC](https://www.ruby-forum.com/t/robustthread-postfix-tmail-rails-app-small-question/172853/4 "2009-07-30T02:09:22Z")

</div>

Philip H. wrote:

> On Jul 29, 2009, at 4:58 PM, Nate L. wrote:
> 
> > Philip H. wrote:
> > 
> > > > from my ruby daemon, into my rails app as a Model and save to  
> > > > database?
> > 
> > Thanks for the reply! I guess I just want to make sure that this  
> > works  
> > and is a good way to do it. Here is what I have in my daemon below:
> 
> That should work. You could also require the actual models in your  
> app/models directly which would get you any validations and other  
> helper methods…

Thanks Philip. I’ll test and respond with my results for anyone else  
out there.

---

<div class="post-metadata">

**Author:** ![nateleavitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@nateleavitt](https://www.ruby-forum.com/u/nateleavitt)\
**Post date:** [July 30, 2009, 2:05am UTC](https://www.ruby-forum.com/t/robustthread-postfix-tmail-rails-app-small-question/172853/5 "2009-07-30T02:05:28Z")

</div>

On Jul 29, 2009, at 4:58 PM, Nate L. wrote:

> Philip H. wrote:
> 
> > > from my ruby daemon, into my rails app as a Model and save to  
> > > database?
> 
> Thanks for the reply! I guess I just want to make sure that this  
> works  
> and is a good way to do it. Here is what I have in my daemon below:

That should work. You could also require the actual models in your  
app/models directly which would get you any validations and other  
helper methods…
