Nate Leavitt
2009-Jul-28 21:57 UTC
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? -- Posted via http://www.ruby-forum.com/.
Philip Hallstrom
2009-Jul-29 20:04 UTC
Re: 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?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).
Nate Leavitt
2009-Jul-29 23:58 UTC
Re: RobustThread, Postfix, TMail, & Rails app small question
Philip Hallstrom 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\+@somedomain.com]/, '''') @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 -- Posted via http://www.ruby-forum.com/.
Philip Hallstrom
2009-Jul-30 00:04 UTC
Re: RobustThread, Postfix, TMail, & Rails app small question
On Jul 29, 2009, at 4:58 PM, Nate Leavitt wrote:> > Philip Hallstrom 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...> =================> > > 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\+@somedomain.com]/, '''') > @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 > -- > Posted via http://www.ruby-forum.com/. > > >
Nate Leavitt
2009-Jul-30 00:09 UTC
Re: RobustThread, Postfix, TMail, & Rails app small question
Philip Hallstrom wrote:> On Jul 29, 2009, at 4:58 PM, Nate Leavitt wrote: > >> >> Philip Hallstrom 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. -- Posted via http://www.ruby-forum.com/.