Ruby on rails Rails应用程序赢得';t在JRuby下发送邮件(通过sendmail)
将我的Rails 2.3应用程序从MRI Ruby 1.8.7切换到JRuby 1.6.5后,该应用程序不再能够发送邮件。我使用ActionMailer的方式如下:Ruby on rails Rails应用程序赢得';t在JRuby下发送邮件(通过sendmail),ruby-on-rails,jruby,sendmail,actionmailer,jrubyonrails,Ruby On Rails,Jruby,Sendmail,Actionmailer,Jrubyonrails,将我的Rails 2.3应用程序从MRI Ruby 1.8.7切换到JRuby 1.6.5后,该应用程序不再能够发送邮件。我使用ActionMailer的方式如下: class MessageMailer < ActionMailer::Base def message(msg, recipient, reply_to_email=nil) template = (msg.message_type.nil?) ? "default" : msg.message_type.nam
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(...)
更有趣的是sendmail日志(/var/log/mail.log):
#在MRI Ruby 1.8.7下发送邮件
1月5日09:38:49我的sendmail[24755]:q05EcnCr024755:from=edwarda,size=310,class=0,nrcpts=1,msgid=,中继=edwarda@localhost
一月5日09:38:49我的sm mta[24757]:q05Ecn02024757:from=,size=516,class=0,nrcpts=1,msgid=,proto=ESMTP,daemon=mta-v4,relay=localhost[127.0.0.1]
1月5日09:38:49我的发送邮件[24755]:q05EcnCr024755:收件人=me@example.com,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消息接受传递)
1月5日09:38:49我的sm mta[24759]:STARTTLS=client,relay=aspmx.l.google.com,version=TLSv1/SSLv3,verify=FAIL,cipher=RC4-SHA,bits=128/128
一月五日09:38:49我的sm mta[24759]:q05Ecn02024757:to=,ctladdr=(1011/1012),延迟=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(正常1325774329 o43si18661797yhk.140)
#根据JRuby 1.6.5发送邮件
1月5日11:10:26我的sendmail[7623]:q05GAQkH007623:from=edwarda,size=199,class=0,nrcpts=0,中继=edwarda@localhost
请注意,当我运行JRuby时,nrcpts(收件人数量)为0,当运行1.8.7时,nrcpts(收件人数量)为1
我使用完全相同的代码和gems,除了在JRuby中使用这些gems之外:
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'
gem'activerecord jdbc adapter',“最有可能是旧版本向“sendmail-t”传递了一条不包含任何收件人标题的消息-t告诉sendmail使用这些标题来确定邮件的去向,因此如果没有这些标题,您将在from=行的日志中得到nrcpts=0,并且不会为邮件记录to=行。这与JRuby错误有关:
显然,IO.popen
在尝试执行sendmail命令时过早关闭。在上游修复之前,此monkey补丁可以包含在初始值设定项中,以解决问题:
轨道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
模块ActionMailer
阶级基础
def执行发送邮件(邮件)
sendmail_args=sendmail_设置[:参数]
sendmail_args+=“-f\”{mail['return-path']}\”如果mail['return-path']
IO.popen(“{sendmail_settings[:location]}{sendmail_args}”,“r+”)do | f | | r+geht,w+geht nicht richtig,bzw。努尔·曼奇马尔
f、 放置mail.encoded.gsub(/\r/,'')
f、 合写
睡眠1#'/usr/sbin/sendmail',
:arguments=>'-i-t'}.merge(值)
结束
属性存取器:设置
def交付!(邮件)
信封|from=mail.return|path|mail.sender|mail.from|addrs.first
返回_path=“-f\”#{envelope_from.to_s.shellescape}\”如果envelope_from
arguments=[settings[:arguments],返回_路径].compact.join(“”)
Sendmail.call(设置[:位置]、参数、mail.destinations.collect(&:shellescape).join(“”,mail)
结束
调用(路径、参数、目的地、邮件)
popen(“#{path}#{arguments}#{destinations}”,“r+”)do|IO|
io.puts mail.encoded.to\u lf
io.close_write#您用来发送邮件的模块的旧版本。这不是ActionMailer吗?jruby是否在1.9模式下运行?什么版本的rails?JRuby在1.8模式下运行;轨道2.3.14
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'
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
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