This project is archived and is in readonly mode.

#2340 ✓resolved
Friedrich Göpel

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

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

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. :)

Comments and changes to this ticket

  • Joren Six

    Joren Six March 29th, 2009 @ 10:36 PM

    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!

  • Alexander Lang

    Alexander Lang April 4th, 2009 @ 08:51 PM

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

  • korch

    korch April 7th, 2009 @ 07:00 PM

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

  • Friedrich Göpel

    Friedrich Göpel April 22nd, 2009 @ 04:29 PM

    • 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
  • Michael Koziarski

    Michael Koziarski June 9th, 2009 @ 09:24 AM

    • State changed from “new” to “resolved”
  • Tom Stuart

    Tom Stuart July 13th, 2009 @ 09:16 AM

    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.

  • Michael Koziarski

    Michael Koziarski July 13th, 2009 @ 10:10 AM

    • 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.

  • Joey A

    Joey A July 28th, 2009 @ 10:49 PM

    Here's a patch that fixes the pre-1.9 issue for me. I've included the tests provided by Josh Nichols at https://rails.lighthouseapp.com/projects/8994/tickets/2945.

  • Joey A
  • Michael Koziarski

    Michael Koziarski July 29th, 2009 @ 02:49 AM

    • Assigned user set to “Michael Koziarski”
  • Michael Siebert

    Michael Siebert July 31st, 2009 @ 10:50 PM

    +1 for Joey A's patch!

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

  • Michael Koziarski

    Michael Koziarski August 1st, 2009 @ 09:15 AM

    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

  • Levin Alexander
  • Josh Nichols

    Josh Nichols August 8th, 2009 @ 04:50 PM

    +1, as I was running into it on #2945 :) 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
    
  • Peter Boling

    Peter Boling August 14th, 2009 @ 07:14 PM

    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

  • Dmitry Polushkin

    Dmitry Polushkin August 14th, 2009 @ 09:01 PM

    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.

  • Mark G

    Mark G September 3rd, 2009 @ 08:35 AM

    Thanks Dmitry. The initialiser was very helpful.

  • Larry Sprock

    Larry Sprock September 4th, 2009 @ 06:41 PM

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

  • Dmitry Polushkin

    Dmitry Polushkin September 6th, 2009 @ 11:05 AM

    Ouch, still not patched? Why not in 2.3.4?

  • Joshua Siler

    Joshua Siler September 9th, 2009 @ 06:29 PM

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

  • Curtis Hawthorne

    Curtis Hawthorne September 10th, 2009 @ 03:04 AM

    +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.

  • robduncan

    robduncan September 11th, 2009 @ 10:37 PM

    1. I am also experiencing this issue. Dmitry Polushkin's workaround works for me.
  • Jeremy Kemper

    Jeremy Kemper September 11th, 2009 @ 11:04 PM

    • Milestone changed from 2.3.4 to 2.3.6

    [milestone:id#50064 bulk edit command]

  • Josh Nichols

    Josh Nichols September 14th, 2009 @ 09:08 PM

    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.

  • Levin Alexander

    Levin Alexander September 15th, 2009 @ 11:34 AM

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

    +1

  • Levin Alexander

    Levin Alexander September 15th, 2009 @ 01:00 PM

    #3078 is a duplicate of this ticket

  • Joe Van Dyk

    Joe Van Dyk September 17th, 2009 @ 04:28 AM

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

  • Zachery Hostens

    Zachery Hostens September 17th, 2009 @ 09:36 AM

    in response to Josh Nichol's Post:

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

  • Zachery Hostens

    Zachery Hostens September 17th, 2009 @ 09:44 AM

    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.

  • Paul Campbell

    Paul Campbell September 18th, 2009 @ 02:09 PM

    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?

  • Gabe da Silveira

    Gabe da Silveira October 5th, 2009 @ 08:21 PM

    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.

  • Christian Seiler

    Christian Seiler October 5th, 2009 @ 08:54 PM

    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.

  • Scott Johnson

    Scott Johnson October 28th, 2009 @ 07:22 PM

    +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.

  • Aaron
  • Christian Seiler

    Christian Seiler October 29th, 2009 @ 08:54 PM

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

  • Dmitry Polushkin

    Dmitry Polushkin October 29th, 2009 @ 09:14 PM

    My another +1 for this ticket.

  • thenobot

    thenobot November 4th, 2009 @ 05:38 AM

    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...

  • thenobot

    thenobot November 4th, 2009 @ 05:41 AM

    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!"
    
  • Chris Lee

    Chris Lee November 4th, 2009 @ 07:16 AM

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

  • Nathaniel Bibler

    Nathaniel Bibler November 5th, 2009 @ 07:40 PM

    +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.

  • owain

    owain November 22nd, 2009 @ 06:47 PM

    • 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

  • owain

    owain November 22nd, 2009 @ 07:01 PM

    • 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...

  • Rob Bean

    Rob Bean November 27th, 2009 @ 03:36 PM

    • 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

  • Michael Koziarski

    Michael Koziarski November 28th, 2009 @ 12:35 AM

    • 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

  • bhauff

    bhauff January 27th, 2010 @ 09:33 PM

    The initializer from Dmitry Polushkin fixed my broken apps.

  • Chris Kampmeier

    Chris Kampmeier March 28th, 2010 @ 08:57 AM

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages