2009-08-26 41 views
0

根據Rails API(下面的代碼片段),接收郵件的最佳方式是在守護進程中創建單個Rails實例,每當新郵件到達時由MTA調用該守護進程。在Rails中將數據傳遞給郵件程序守護進程?

我的問題是:當新郵件到達時,如何將數據傳遞給守護進程?

========================

Rails的API片段

To receive emails, you need to implement a public instance method called receive that takes a tmail object as its single parameter. The Action Mailer framework has a corresponding class method, which is also called receive, that accepts a raw, unprocessed email as a string, which it then turns into the tmail object and calls the receive instance method. 

Example: 

    class Mailman < ActionMailer::Base 
    def receive(email) 
     page = Page.find_by_address(email.to.first) 
     page.emails.create(
     :subject => email.subject, :body => email.body 
    ) 

     if email.has_attachments? 
     for attachment in email.attachments 
      page.attachments.create({ 
      :file => attachment, :description => email.subject 
      }) 
     end 
     end 
    end 
    end 

This Mailman can be the target for Postfix or other MTAs. In Rails, you would use the runner in the trivial case like this: 

    ./script/runner 'Mailman.receive(STDIN.read)' 

However, invoking Rails in the runner for each mail to be received is very resource intensive. A single instance of Rails should be run within a daemon if it is going to be utilized to process more than just a limited number of email. 

回答

0

在你提供的例子,有沒有守護進程運行來處理電子郵件。該文檔說您可以設置您的郵件程序守護程序,在這種情況下,Postfix在收到郵件時調用命令。當您從您的郵件調用命令:

RAILS_ROOT /腳本/運行「Mailman.receive(STDIN.read)」

電子郵件的內容被傳遞到接收方法。處理傳入電子郵件的更好方法是創建一個接收電子郵件的實際郵箱。然後,您可以編寫批量檢查郵箱以處理電子郵件的Ruby腳本。您可以通過cron調用該腳本並鎖定它,以確保只有一個進程執行此任務。