Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mail forgets to escape when passing arguments as hash #1056

Closed
gucki opened this issue Dec 7, 2016 · 6 comments
Closed

mail forgets to escape when passing arguments as hash #1056

gucki opened this issue Dec 7, 2016 · 6 comments

Comments

@gucki
Copy link

gucki commented Dec 7, 2016

It seems the mail gem doesn't encode properly when the arguments are passed as a hash to the constructor.

It works when using the block format

irb(main):034:0>     mail = Mail.new do
irb(main):035:1*       from    "Jörg <test@example.com>"
irb(main):036:1>       to      "Hans Müller <test@example.com>"
irb(main):037:1>       subject "This is a test email"
irb(main):038:1>       body    "Too bad it fails to send... :-("
irb(main):039:1>     end
=> #<Mail::Message:47071882270480, Multipart: false, Headers: <From: Jörg <test@example.com>>, <To: Hans Müller <test@example.com>>, <Subject: This is a test email>>
irb(main):041:0>     mail.deliver
=> #<Mail::Message:47071882270480, Multipart: false, Headers: <Date: Wed, 07 Dec 2016 08:17:34 +0100>, <From: Jörg <test@example.com>>, <To: Hans Müller <test@example.com>>, <Message-ID: <5847b78ebc50b_4ffe2acfc395111485439@GT680R.mail>>, <Subject: This is a test email>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>

It fails when passing the argmuents as a hash to the constructor:

irb(main):044:0*     mail = Mail.new(
irb(main):045:1*       from: "Jörg <test@example.com>",
irb(main):046:1*       to: "Hans Müller <test@example.com>",
irb(main):047:1*       subject: "This is a test email",
irb(main):048:1*       body: "Too bad it fails to send... :-(",
irb(main):049:1*     )
=> #<Mail::Message:47071882540340, Multipart: false, Headers: <From: Jörg <test@example.com>>, <To: Hans Müller <test@example.com>>, <Subject: This is a test email>>
irb(main):051:0>     mail.deliver
Net::SMTPSyntaxError: 501 <Jörg: "@" or "." expected after "Jörg"

  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/smtp.rb:965:in `check_response'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/smtp.rb:934:in `getok'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/smtp.rb:842:in `mailfrom'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/smtp.rb:663:in `send_message'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mail-2.6.4/lib/mail/network/delivery_methods/smtp.rb:114:in `block in deliver!'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/smtp.rb:522:in `start'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mail-2.6.4/lib/mail/network/delivery_methods/smtp.rb:113:in `deliver!'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mail-2.6.4/lib/mail/message.rb:2149:in `do_delivery'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mail-2.6.4/lib/mail/message.rb:239:in `deliver'
  from (irb):51
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
  from /home/gucki/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
  from bin/rails:6:in `require'
  from bin/rails:6:in `<main>'
@jeremy
Copy link
Collaborator

jeremy commented Dec 7, 2016

This is because we don't support unencoded UTF-8 yet:

>> Mail.new(from: 'Jorg <test@example.com>').smtp_envelope_from
=> "test@example.com"
>> Mail.new(from: 'Jörg <test@example.com>').smtp_envelope_from
=> "Jörg <test@example.com>"

The second case is wrong. That's because we couldn't parse the From address, per RFC 5322.

We need to encode these fields before using them. Here's a demo showing that this works (since it encodes the UTF-8 chars before parsing!):

>> Mail.new(from: Mail::FromField.new('Jörg <test@example.com>').encoded).smtp_envelope_from
=> "test@example.com"

We have quite a few open issues regarding UTF-8 support, going back to #39.

@jeremy jeremy closed this as completed Dec 7, 2016
@gucki
Copy link
Author

gucki commented Dec 7, 2016

@jeremy Thanks for your fast reply. But the block form does work. It also works when using the setters, for example mail.from = 'Jörg <test@example.com>'. It only does not work when passing the arguments as a hash. The server is using exim, if that's of interest.

@jeremy
Copy link
Collaborator

jeremy commented Dec 7, 2016

What does mail.smtp_envelope_from report for you in each case?

@gucki
Copy link
Author

gucki commented Dec 8, 2016

Same as for you

irb(main):073:0> Mail.new(from: 'Jorg <test@example.com>').smtp_envelope_from
=> "test@example.com"

irb(main):074:0> Mail.new(from: 'Jörg <test@example.com>').smtp_envelope_from
=> "Jörg <test@example.com>"

But when I look at the headers of the email in my mail client, I can see it's properly encoded:

From: =?UTF-8?B?bcO8bGxlcg==?= <test@example.com>
To: =?UTF-8?B?asO2cmc=?= <test@example.com>

<From: müller <test@example.com>>, <To: jörg <test@example.com>>

@jeremy
Copy link
Collaborator

jeremy commented Dec 8, 2016

Note that's the message From address. The SMTP envelope from address is what's used in the SMTP MAIL FROM: command. The SMTP envelope address is derived by default from the message From address.

@jeremy
Copy link
Collaborator

jeremy commented May 15, 2017

The original issue is fixed by #1103:

>> Mail.new(from: 'Jörg <test@example.com>').smtp_envelope_from
=> "test@example.com"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants