RobustThread, Postfix, TMail, & Rails app small question

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?

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

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\[email protected]]/, '')
@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

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.

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…