Guest User

Untitled

a guest
Aug 7th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. Rails app won't send mail (via sendmail) under JRuby
  2. class MessageMailer < ActionMailer::Base
  3. def message(msg, recipient, reply_to_email=nil)
  4. template = (msg.message_type.nil?) ? "default" : msg.message_type.name.downcase.gsub(' ', '_')
  5.  
  6. recipients recipient
  7. subject msg.subject
  8. from (msg.sender.nil? or msg.sender.email.blank?) ? ""no-reply" <#{SYSTEM_EMAIL_ADDRESS}>" : msg.sender.email
  9. content_type "text/html"
  10. body render_message(template, :message => msg)
  11. reply_to reply_to_email || ((msg.sender.nil? or msg.sender.email.blank?) ? ""no-reply" <#{SYSTEM_EMAIL_ADDRESS}>" : msg.sender.email)
  12. end
  13.  
  14. ...
  15. end
  16.  
  17. MessageMailer.deliver_message(...)
  18.  
  19. config.action_mailer.delivery_method = :sendmail
  20.  
  21. # Sending mail under MRI Ruby 1.8.7
  22. Jan 5 09:38:49 my sendmail[24755]: q05EcnCr024755: from=edwarda, size=310, class=0, nrcpts=1, msgid=<201201051438.q05EcnCr024755@my.example.org>, relay=edwarda@localhost
  23. Jan 5 09:38:49 my sm-mta[24757]: q05Ecn02024757: from=<edwarda@my.example.org>, size=516, class=0, nrcpts=1, msgid=<201201051438.q05EcnCr024755@my.example.org>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
  24. Jan 5 09:38:49 my sendmail[24755]: q05EcnCr024755: to=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 Message accepted for delivery)
  25. 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
  26. Jan 5 09:38:49 my sm-mta[24759]: q05Ecn02024757: to=<me@example.com>, ctladdr=<edwarda@my.example.org> (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)
  27.  
  28. # Sending mail under JRuby 1.6.5
  29. Jan 5 11:10:26 my sendmail[7623]: q05GAQkH007623: from=edwarda, size=199, class=0, nrcpts=0, relay=edwarda@localhost
  30.  
  31. gem 'activerecord-jdbc-adapter', '<= 1.2.0', :require => false
  32. gem 'activerecord-jdbcpostgresql-adapter', '<= 1.2.0', :require => 'jdbc_adapter'
  33. gem 'ffi-ncurses'
  34. gem 'jruby-openssl'
  35. gem 'torquebox', '2.0.0.beta1', :platforms => 'jruby'
  36. gem 'torquebox-rake-support', :platforms => 'jruby'
  37. gem 'torquebox-capistrano-support', '2.0.0.beta1'
  38.  
  39. module ActionMailer
  40. class Base
  41. def perform_delivery_sendmail(mail)
  42. sendmail_args = sendmail_settings[:arguments]
  43. sendmail_args += " -f "#{mail['return-path']}"" if mail['return-path']
  44.  
  45. IO.popen("#{sendmail_settings[:location]} #{sendmail_args}", "r+") do |f| #r+ geht, w+ geht nicht richtig, bzw. nur manchmal
  46. f.puts mail.encoded.gsub(/r/, '')
  47. f.close_write
  48. sleep 1 # <---- added this line in order to give sendmail some time to process
  49. end
  50. end
  51. end
  52. end
  53.  
  54. module Mail
  55. class Sendmail
  56.  
  57. def initialize(values)
  58. self.settings = { :location => '/usr/sbin/sendmail',
  59. :arguments => '-i -t' }.merge(values)
  60. end
  61.  
  62. attr_accessor :settings
  63.  
  64. def deliver!(mail)
  65. envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
  66. return_path = "-f "#{envelope_from.to_s.shellescape}"" if envelope_from
  67.  
  68. arguments = [settings[:arguments], return_path].compact.join(" ")
  69.  
  70. Sendmail.call(settings[:location], arguments, mail.destinations.collect(&:shellescape).join(" "), mail)
  71. end
  72.  
  73. def Sendmail.call(path, arguments, destinations, mail)
  74. IO.popen("#{path} #{arguments} #{destinations}", "r+") do |io|
  75. io.puts mail.encoded.to_lf
  76. io.close_write # <------ changed this from flush to close_write
  77. sleep 1 # <-------- added this line
  78. end
  79. end
  80. end
  81. end
Add Comment
Please, Sign In to add comment