2012-01-05 55 views
1

將我的Rails 2.3應用程序從MRI Ruby 1.8.7切換到JRuby 1.6.5後,該應用程序不再能夠發送郵件。我使用的ActionMailer這樣的:Rails應用不會在JRuby下發送郵件(通過sendmail)

class MessageMailer < ActionMailer::Base 
    def message(msg, recipient, reply_to_email=nil) 
    template = (msg.message_type.nil?) ? "default" : msg.message_type.name.downcase.gsub(' ', '_') 

    recipients recipient 
    subject msg.subject 
    from (msg.sender.nil? or msg.sender.email.blank?) ? "\"no-reply\" <#{SYSTEM_EMAIL_ADDRESS}>" : msg.sender.email 
    content_type "text/html" 
    body render_message(template, :message => msg) 
    reply_to reply_to_email || ((msg.sender.nil? or msg.sender.email.blank?) ? "\"no-reply\" <#{SYSTEM_EMAIL_ADDRESS}>" : msg.sender.email) 
    end 

    ... 
end 

MessageMailer.deliver_message(...) 

這可能是無關緊要的,因爲這在MRI的Ruby 1.8.7的所有作品。

Rails應用程序被配置在配置/環境/ production.rb到使用sendmail:

config.action_mailer.delivery_method = :sendmail 

更有趣的是sendmail的日誌(/var/log/mail.log):

# Sending mail under MRI Ruby 1.8.7 
Jan 5 09:38:49 my sendmail[24755]: q05EcnCr024755: from=edwarda, size=310, class=0, nrcpts=1, msgid=<[email protected]>, [email protected] 
Jan 5 09:38:49 my sm-mta[24757]: q05Ecn02024757: from=<[email protected]>, size=516, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1] 
Jan 5 09:38:49 my sendmail[24755]: q05EcnCr024755: [email protected], ctladdr=edwarda (1011/1012), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30310, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (q05Ecn02024757 Message accepted for delivery) 
Jan 5 09:38:49 my sm-mta[24759]: STARTTLS=client, relay=aspmx.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=RC4-SHA, bits=128/128 
Jan 5 09:38:49 my sm-mta[24759]: q05Ecn02024757: to=<[email protected]>, ctladdr=<[email protected]> (1011/1012), delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=120516, relay=aspmx.l.google.com. [74.125.45.27], dsn=2.0.0, stat=Sent (OK 1325774329 o43si18661797yhk.140) 

# Sending mail under JRuby 1.6.5 
Jan 5 11:10:26 my sendmail[7623]: q05GAQkH007623: from=edwarda, size=199, class=0, nrcpts=0, [email protected] 

請注意,當我運行JRuby時,nrcpts(收件人數量)爲0,運行1.8.7時爲1。

我使用完全相同的代碼和寶石,但我用這些寶石除了JRuby的:

gem 'activerecord-jdbc-adapter', '<= 1.2.0', :require => false 
gem 'activerecord-jdbcpostgresql-adapter', '<= 1.2.0', :require => 'jdbc_adapter' 
gem 'ffi-ncurses' 
gem 'jruby-openssl' 
gem 'torquebox', '2.0.0.beta1', :platforms => 'jruby' 
gem 'torquebox-rake-support', :platforms => 'jruby' 
gem 'torquebox-capistrano-support', '2.0.0.beta1' 

在情況下,它是非常有用的,這是我的Gemfile.lock

在我的Rails日誌中沒有有趣或不尋常的輸出;只有通常的成功消息。

編輯:我無法在我的開發(OSX)機器上重現此問題。

想知道爲什麼收件人可能會迷路或者我可能會如何解決此問題?

+0

是jruby的在1.9模式下運行?什麼版本的導軌? – 2012-01-05 19:34:54

+0

JRuby以1.8模式運行; Rails 2.3.14 – 2012-01-08 01:05:49

回答

3

這是關係到一個JRuby的錯誤:http://jira.codehaus.org/browse/JRUBY-6162

顯然IO.popen過早關閉,當它試圖執行sendmail命令。直到這個上游固定的,這猴補丁可以包括在初始值設定來解決此問題:

軌道2:

module ActionMailer 
    class Base 
    def perform_delivery_sendmail(mail) 
     sendmail_args = sendmail_settings[:arguments] 
     sendmail_args += " -f \"#{mail['return-path']}\"" if mail['return-path'] 

     IO.popen("#{sendmail_settings[:location]} #{sendmail_args}", "r+") do |f| #r+ geht, w+ geht nicht richtig, bzw. nur manchmal 
     f.puts mail.encoded.gsub(/\r/, '') 
     f.close_write 
     sleep 1 # <---- added this line in order to give sendmail some time to process 
     end 
    end 
    end 
end 

導軌3:

module Mail 
    class Sendmail 

    def initialize(values) 
     self.settings = { :location  => '/usr/sbin/sendmail', 
         :arguments  => '-i -t' }.merge(values) 
    end 

    attr_accessor :settings 

    def deliver!(mail) 
     envelope_from = mail.return_path || mail.sender || mail.from_addrs.first 
     return_path = "-f \"#{envelope_from.to_s.shellescape}\"" if envelope_from 

     arguments = [settings[:arguments], return_path].compact.join(" ") 

     Sendmail.call(settings[:location], arguments, mail.destinations.collect(&:shellescape).join(" "), mail) 
    end 

    def Sendmail.call(path, arguments, destinations, mail) 
     IO.popen("#{path} #{arguments} #{destinations}", "r+") do |io| 
     io.puts mail.encoded.to_lf 
     io.close_write # <------ changed this from flush to close_write 
     sleep 1 # <-------- added this line 
     end 
    end 
    end 
end 
0

很可能舊版本將消息傳遞給不包含任何收件人標題的「sendmail -t」。 -t告訴sendmail使用這些頭文件來確定郵件的發送位置,如果沒有它們,你會在from =行的日誌中得到nrcpts = 0,並且不會記錄該郵件的行。

+0

舊版本的是什麼? – 2012-01-05 18:14:27

+0

您用來發送郵件的模塊的較舊版本。 – 2012-01-05 18:21:49

+0

這不是ActionMailer嗎? – 2012-01-10 19:35:25

相關問題