Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

action mailer can't deliver mail via smtp on ruby 1.9.1 or 1.8.7

#2340

Ruby: ruby 1.9.1p0 (2009-03-04 revision 22762) [i386-darwin9.6.0] Rails: edge rails revision: 6ed42ebdff05f9d28a60e91093d8f9afad03a958

Delivering e-mail via smtp on ruby 1.9.1 fails. Turning on debugging in postfix reveals the cause:


MAIL FROM:<["friedrich.goepel@gmail.com"]>

Of course the square brackets are illegal here and make it impossible for the mail server to deliver the mail.

A little digging into action mailer reveals: - in perform_delivery_smtp the sender is passed to Net::SMTP as an Array here:


 smtp.sendmail(mail.encoded, sender, destinations)
  • it works when changing the line to:

 smtp.sendmail(mail.encoded, sender.first, destinations)
  • Apparently a Array is passed in ruby 1.8 as well, where it works as is.
  • Passing a String works in 1.8 as well.

Now when diffing ruby 1.8 and 1.9.1 net/smtp.rb one finds this:


-    def mailfrom( fromaddr )
-      getok('MAIL FROM:<%s>', fromaddr)
+    def mailfrom(from_addr)
+      getok("MAIL FROM:<#{from_addr}>")

So it seems this worked before just by sheer accident, multiple senders aren't possible anyway, so passing an Array here makes little sense.

Further investigation leads to the discovery that the Array wrapping happens in TMail, and switching the mail.from to mail['from'] fixes the problem as well and more cleanly.

So here's a oneliner patch to do just that.

Enjoy. :)

Reported by Friedrich Göpel · March 25th, 2009 @ 05:02 PM

State: resolved
Milestone: 2.3.6
Assigned to: nobody
Importance: none

Activity

  1. Joren Six
    Joren Six

    I can confirm this bug. In postfix it gives this:

    warning: Illegal address syntax from unknown[xx.xx.xx.xx] in MAIL command: ["user@example.com"]

    The exception in rails itself:

    Net::SMTPSyntaxError 501 5.1.7 Bad sender address syntax

    Let's hope that this comment makes it easier to google this problem.

    Anyway thanks for the fix!

    March 29th, 2009 @ 10:36 PM

  2. Alexander Lang
    Alexander Lang

    +1 from me. i get the same error with postfix.

    April 4th, 2009 @ 08:51 PM

  3. korch
    korch

    +1 from me too, getting the exact same error right now with postfix

    April 7th, 2009 @ 07:00 PM

  4. Friedrich Göpel
    Friedrich Göpel
    • Tag changed from 2.3.2, actionmailer, bug, edge, email, ruby1.9, ruby19 to 2.3, 2.3.2, 2.3.x, actionmailer, bug, edge, email, patch, ruby1.9, ruby19, smtp

    April 22nd, 2009 @ 04:29 PM

  5. Michael Koziarski
    Michael Koziarski
    • State changed from new to resolved

    June 9th, 2009 @ 09:24 AM

  6. Tom Stuart
    Tom Stuart

    This commit broke SMTP sends in my Rails project (on Ruby 1.8).

    When the TMail object is instantiated with a From header that includes a recipient name and email address (e.g. "John Doe john@doe.com"), mail.from returns just the email address (e.g. "john@doe.com"), which is the correct syntax for SMTP's MAIL FROM command. However, mail['from'] returns the verbatim From string (e.g. "John Doe john@doe.com") which is invalid syntax and causes the SMTP server to barf.

    Breaking support for named senders in exchange for Ruby 1.9 compatibility isn't an acceptable tradeoff. If you don't want an array, the sender.first solution is the right one.

    July 13th, 2009 @ 09:16 AM

  7. Michael Koziarski
    Michael Koziarski
    • State changed from resolved to open
    • Milestone changed from 2.x to 2.3.4

    Tom,

    Can you provide a patch (complete with test case) which demonstrates this problem?

    I'm frozen to 2-3-stable and stuff's working ok for me, so I'm obviously doing something wrong.

    July 13th, 2009 @ 10:10 AM

  8. Joey A
    Joey A

    Here's a patch that fixes the pre-1.9 issue for me. I've included the tests provided by Josh Nichols at #2945 ActionMailer generates invalid SMTP commands when from contains name.

    July 28th, 2009 @ 10:49 PM

  9. Joey A
  10. Michael Koziarski
    Michael Koziarski
    • Assigned user set to Michael Koziarski

    July 29th, 2009 @ 02:49 AM

  11. Michael Siebert
    Michael Siebert

    +1 for Joey A's patch!

    just applied it to our production app, it saved our night
    this bug is a real showstopper...

    July 31st, 2009 @ 10:50 PM

  12. Michael Koziarski
    Michael Koziarski

    Joey,

    Could you attach it as a proper git format-patch patch, that way
    you'll get the right credit.

    Happy to apply to 2-3-stable and master, will be fixed in 2.3.4

    August 1st, 2009 @ 09:15 AM

  13. Levin Alexander
  14. Josh Nichols
    Josh Nichols

    +1, as I was running into it on #2945 ActionMailer generates invalid SMTP commands when from contains name :) I verified the patch addresses the problem on 1.8 and 1.9.

    Given the mailer:

    class Blah < ActionMailer::Base
    
      def blah
        from "Tester McTesty <test@test.com>"
        recipients "Josh Nichols <josh@technicalpickles.com>"
        subject "blah"
      end
      
    end
    

    Output before the patch is incorrect for 1.8 and 1.9, according to mailtrap:

    * Message begins
    From: <Tester McTesty <test@test.com>>
    To: <josh@technicalpickles.com>
    Body:
    Date: Sat, 8 Aug 2009 11:43:40 -0400
    From: Tester McTesty <test@test.com>
    To: Josh Nichols <josh@technicalpickles.com>
    Message-Id: <4a7d9d2ce0ff_540558b421df@technicalpickles.local.tmail>
    Subject: blah
    Mime-Version: 1.0
    Content-Type: text/plain; charset=utf-8
    
    * Message ends
    

    After applying the patch, it is corrected:

    * Message begins
    From: <test@test.com>
    To: <josh@technicalpickles.com>
    Body:
    Date: Sat, 8 Aug 2009 11:42:06 -0400
    From: Tester McTesty <test@test.com>
    To: Josh Nichols <josh@technicalpickles.com>
    Message-Id: <4a7d9cce149d7_53d158b421c1@technicalpickles.local.tmail>
    Subject: blah
    Mime-Version: 1.0
    Content-Type: text/plain; charset=utf-8
    
    * Message ends
    

    August 8th, 2009 @ 04:50 PM

  15. Peter Boling
    Peter Boling

    Bug is NOT just on Ruby 1.9.1. I have the problem on Ruby 1.8.7. I assume anything that runs rails 2.3.3 will be unable to send email with a "Name" format.

    This is a show stopper. Please release a new rails with this patch! The

    August 14th, 2009 @ 07:14 PM

  16. Dmitry Polushkin
    Dmitry Polushkin

    I've added a fix in initializer for Rails 2.3.3 (fix_mailer_on_rails_2.3.3.rb):

    module ActionMailer
      class Base
        def perform_delivery_smtp(mail)
          destinations = mail.destinations
          mail.ready_to_send
          sender = (mail['return-path'] && mail['return-path'].spec) || Array(mail.from).first
    
          smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
          smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
          smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
                     smtp_settings[:authentication]) do |smtp|
            smtp.sendmail(mail.encoded, sender, destinations)
          end
        end
      end
    end
    

    Lets wait till the end of the month, when 2.3.4 will be released.

    August 14th, 2009 @ 09:01 PM

  17. Mark G
    Mark G

    Thanks Dmitry. The initialiser was very helpful.

    September 3rd, 2009 @ 08:35 AM

  18. Larry Sprock
    Larry Sprock

    Why did this not get applied to rails 2.3.4? Seems like a no brainer. Thanks to Dmitry for a work around.

    September 4th, 2009 @ 06:41 PM

  19. Dmitry Polushkin
    Dmitry Polushkin

    Ouch, still not patched? Why not in 2.3.4?

    September 6th, 2009 @ 11:05 AM

  20. Joshua Siler
    Joshua Siler

    Just plowed head long into this issue using 2.3.4 and ruby 1.8. Is the patch in the queue for next release?

    September 9th, 2009 @ 06:29 PM

  21. Curtis Hawthorne
    Curtis Hawthorne

    +1 from me. I just ran into this issue after upgrading from 2.2. I'm running ruby 1.8. For now, I'm using Dmitry's workaround, but it would be great to get this in the release.

    September 10th, 2009 @ 03:04 AM

  22. robduncan
    robduncan
    1. I am also experiencing this issue. Dmitry Polushkin's workaround works for me.

    September 11th, 2009 @ 10:37 PM

  23. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.4 to 2.3.6

    [milestone:id#50064 bulk edit command]

    September 11th, 2009 @ 11:04 PM

  24. Josh Nichols
    Josh Nichols

    Verified this is a still problem on 2.3.4.

    Are there any issues with the attached patches? Would really like to see this fixed so I don't have to monkey patch ActionMailer in an initializer.

    September 14th, 2009 @ 09:08 PM

  25. Levin Alexander
    Levin Alexander

    We're using the Patch by Joey A. successfully in production.

    +1

    September 15th, 2009 @ 11:34 AM

  26. Levin Alexander
  27. Joe Van Dyk
    Joe Van Dyk

    So this problem doesn't just happen with ruby 1.9? If so, the ticket title should be updated.

    September 17th, 2009 @ 04:28 AM

  28. Zachery Hostens
    Zachery Hostens

    in response to Josh Nichol's Post:

    Why on earth are emails being sent with multiple From/To lines?

    September 17th, 2009 @ 09:36 AM

  29. Zachery Hostens
    Zachery Hostens

    nm. i imagine the paste is slightly different than actual output and one is MAIL FROM/RCPT TO, and other is just From/To.

    Apologize.

    September 17th, 2009 @ 09:44 AM

  30. Paul Campbell
    Paul Campbell

    I got this today on 1.8.6 on Engine Yard, on Rails 2.3.3 and 2.3.4 ....

    It's a shame this didn't make it in to 2.3.4 ... seems like it's a pretty serious bug, and I can't imagine too many people using mailers that send with just an email address. It would essentially break any app that relied on named mailers that upgraded to 2.3.3 or 2.3.4.

    Is 2.3.5 on the cards soon? Would something like this not warrant a small point release?

    September 18th, 2009 @ 02:09 PM

  31. Gabe da Silveira
    Gabe da Silveira

    I agree with Paul. This bug is nasty. Our site was not sending emails for an hour after deploying. To make matters worse, exception notifier was broken too so we were not notified of the problem. This should be pushed out as a point release immediately. This kind of thing can do real damage to production apps.

    October 5th, 2009 @ 08:21 PM

  32. Christian Seiler
    Christian Seiler

    Gabe, I had to go through the exact same. No registration emails, no exception notifications. This is by far the most critical issue I had with Rails so far. And it's a shame that nothing has happened such a long time.

    October 5th, 2009 @ 08:54 PM

  33. Scott Johnson
    Scott Johnson

    +1, this is worth a point release by itself. This is a show stopper and it has been broken for months now. A real shame.

    October 28th, 2009 @ 07:22 PM

  34. Aaron
  35. Christian Seiler
    Christian Seiler

    We should start a tweet blast or something to get some awareness

    October 29th, 2009 @ 08:54 PM

  36. Dmitry Polushkin
    Dmitry Polushkin

    My another +1 for this ticket.

    October 29th, 2009 @ 09:14 PM

  37. thenobot
    thenobot

    I had the same problem with ActionMailer 2.2.2 and Ruby 1.8.7-174 with Postfix.

    A workaround is to add the following line to your notification methods:

    from       "Me <noreply@mydomain.com>"
    

    Not sure if that works for the versions of ActionMailer and Ruby in question...

    November 4th, 2009 @ 05:38 AM

  38. thenobot
    thenobot

    Oh barf, my first post and I mess up the formatting. The workaround looks like:

        from       "Symantec Health <noreply@symantechealth.com>"
    +   headers    "return-path" => 'noreply@symantechealth.com'
        subject    "New Symantec Health Sales Lead!"
    

    November 4th, 2009 @ 05:41 AM

  39. Chris Lee
    Chris Lee

    +1 for this ticket and for Dmitry's code.

    November 4th, 2009 @ 07:16 AM

  40. Nathaniel Bibler
    Nathaniel Bibler

    +1 for this issue being seen in production and successful use of Dmitry's initializer code in Rails 2.3.4 with MRI Ruby 1.8.7.

    November 5th, 2009 @ 07:40 PM

  41. owain
    owain
    • Tag changed from 2.3, 2.3.2, 2.3.x, actionmailer, bug, edge, email, patch, ruby1.9, ruby19, smtp to 1.8.7, 2.3, 2.3.2, 2.3.x, actionmailer, bug, edge, email, patch, ruby, ruby1.9, ruby19, smtp
    • Title changed from action mailer can't deliver mail via smtp on ruby 1.9.1 to action mailer can't deliver mail via smtp on ruby 1.9.1 or 1.8.7

    It took an absolute age (4 hours) to find this post. Perhaps a better title such as Net::SMTPFatalError (553) Invalid mail address might make it somewhat easier to search for in future. At least that's what I was getting.

    Certainly on ruby 1.8.7

    November 22nd, 2009 @ 06:47 PM

  42. owain
    owain
    • Tag cleared.

    Posted a stub on the Ruby on Rails talk site to help people pick this up.
    http://groups.google.com/group/rubyonrails-talk/browse_thread/threa...

    November 22nd, 2009 @ 07:01 PM

  43. Rob Bean
    Rob Bean
    • Assigned user cleared.

    +1 to thenobot's solution.

    Using the name and email in "From" and adding the "return-path" header resolved the issue for me.

    Ruby 1.8.7
    Rails 2.3.4

    November 27th, 2009 @ 03:36 PM

  44. Michael Koziarski
    Michael Koziarski
    • State changed from open to resolved

    Missed 2.3.5 for this one, sorry.

    It's resolved in 2-3-stable and master so will be fixed in 3.x and 2.3.6

    November 28th, 2009 @ 12:35 AM

  45. bhauff
    bhauff

    The initializer from Dmitry Polushkin fixed my broken apps.

    January 27th, 2010 @ 09:33 PM

  46. Chris Kampmeier
    Chris Kampmeier

    In case anybody's looking for them, the commits that fix this are da61a6c9 on 2-3-stable and 7e0aa35c on master.

    March 28th, 2010 @ 08:57 AM