3

I am using mail gem in rails 2.3.18

class English < ActionMailer::Base
  def test
    subject       "testing attachments"
    recipients    "test@test1.com"
    from          "test@gmail.com"
    sent_on       Time.now
    content_type  "multipart/mixed"
    part :content_type => "multipart/alternative" do |alt|
      alt.part "text/plain" do |plain|
        plain.body "hello"
      end
    end
    attachment :body => "test", :filename => '추적_.pdf'
  end
end

when i am sending the email the filename gets corrupt if it is not in english i works well when it is non english characters

mail objects looks something like this:

 #<Mail::Message:70182593794020, Multipart: true, Headers: <Date: Sat, 11 Jan 2014 11:30:51 +0530>, <From: test@gmail.com>, <To:
 test@test1.com>, <Message-ID:
 <52d0de1454a_b9c93fd4a18349dc100290@sumans-MacBook-Pro-2.local.mail>>,
 <Subject: testing attachments>, <Mime-Version: 1.0>, <Content-Type:
 multipart/mixed;
 boundary=--==_mimepart_52d0de13eaf0a_b9c93fd4a18349dc9982>,
 <Content-Transfer-Encoding: 7bit>>

mail.parts

 [#<Mail::Message:70182595011140, Multipart: true, Headers: <Date: Sat,
 11 Jan 2014 11:30:51 +0530>, <Message-ID:
 <52d0de13f1aea_b9c93fd4a18349dc100029@sumans-MacBook-Pro-2.local.mail>>,
 <Mime-Version: 1.0>, <Content-Type: multipart/alternative;
 boundary=--==_mimepart_52d0de13e9676_b9c93fd4a18349dc997cb>,
 <Content-Transfer-Encoding: 7bit>>, #<Mail::Message:70182595276760,
 Multipart: true, Headers: <Date: Sat, 11 Jan 2014 11:30:51 +0530>,
 <Message-ID:
 <52d0de13f35ca_b9c93fd4a18349dc1001bc@sumans-MacBook-Pro-2.local.mail>>,
 <Mime-Version: 1.0>, <Content-Type: multipart/mixed; name=추적_.pdf>,
 <Content-Transfer-Encoding: base64>, <content-disposition: attachment;
 filename=추적_.pdf>>]

here the filename is proper but when i am sending the email using

smtp.sendmail(mail.encoded, sender, destinations) the filename is corrupt

mail.encoded

> "Date: Sat, 11 Jan 2014 11:30:51 +0530\r\nFrom: test@gmail.com\r\nTo:
> test@test1.com\r\nMessage-ID:
> <52d0de1454a_b9c93fd4a18349dc100290@sumans-MacBook-Pro-2.local.mail>\r\nSubject:
> testing attachments\r\nMime-Version: 1.0\r\nContent-Type:
> multipart/mixed;\r\n
> boundary=\"--==_mimepart_52d0de13eaf0a_b9c93fd4a18349dc9982\"\r\nContent-Transfer-Encoding:
> 7bit\r\n\r\n\r\n----==_mimepart_52d0de13eaf0a_b9c93fd4a18349dc9982\r\nDate:
> Sat, 11 Jan 2014 11:30:51 +0530\r\nMessage-ID:
> <52d0de13f1aea_b9c93fd4a18349dc100029@sumans-MacBook-Pro-2.local.mail>\r\nMime-Version:
> 1.0\r\nContent-Type: multipart/alternative;\r\n boundary=\"--==_mimepart_52d0de13e9676_b9c93fd4a18349dc997cb\"\r\nContent-Transfer-Encoding:
> 7bit\r\n\r\n\r\n----==_mimepart_52d0de13e9676_b9c93fd4a18349dc997cb\r\nDate:
> Sat, 11 Jan 2014 11:30:51 +0530\r\nMessage-ID:
> <52d0de13f01cb_b9c93fd4a18349dc999bc@sumans-MacBook-Pro-2.local.mail>\r\nMime-Version:
> 1.0\r\nContent-Type: text/plain;\r\n charset=utf-8\r\nContent-Transfer-Encoding:
> quoted-printable\r\nContent-Disposition:
> inline\r\n\r\nhello=\r\n\r\n----==_mimepart_52d0de13e9676_b9c93fd4a18349dc997cb--\r\n\r\n----==_mimepart_52d0de13eaf0a_b9c93fd4a18349dc9982\r\nDate:
> Sat, 11 Jan 2014 11:30:51 +0530\r\nMessage-ID:
> <52d0de13f35ca_b9c93fd4a18349dc1001bc@sumans-MacBook-Pro-2.local.mail>\r\nMime-Version:
> 1.0\r\nContent-Type: multipart/mixed;\r\n charset=UTF-8\r\nContent-Transfer-Encoding:
> base64\r\ncontent-disposition: =?UTF-8?Q?attachment;?=\r\n
> =?UTF-8?Q?_filename=3D=E1=84=8E=E1=85=AE=E1=84=8C=E1=85=A5=E1=86=A8=5F.pdf?=\r\n\r\ndGVzdA==\r\n\r\n----==_mimepart_52d0de13eaf0a_b9c93fd4a18349dc9982--\r\n"

can anyone tell what is going wrong in encode method in mail gem the filename i receive in the mail as noname , english characters works properly

Thilo
  • 17,565
  • 5
  • 68
  • 84
yednamus
  • 582
  • 1
  • 4
  • 22

4 Answers4

2

Try

class English < ActionMailer::Base
    def test
        subject       "testing attachments"
        recipients    "test@test1.com"
        from          "test@gmail.com"
        sent_on       Time.now
        content_type  "multipart/mixed; charset=UTF-8"
        part :content_type => "multipart/alternative; charset=UTF-8" do |alt|
            alt.part "text/plain;  charset=UTF-8" do |plain|
                plain.body "hello"
            end
        end
        attachment :body => "test", :filename => '추적_.pdf'
        charset = "UTF-8"
        content_transfer_encoding = '8bit'
    end
end
Ilya Cherevkov
  • 1,743
  • 2
  • 17
  • 47
2

Have you tried adding an
# -*- encoding : utf-8 -*-
line at the top of your English class definition file?

--- EDIT ---

# encoding: UTF-8
# coding: UTF-8
# -*- coding: UTF-8 -*-

are equivalent.

xlembouras
  • 8,215
  • 4
  • 33
  • 42
  • What is the `-*-` for? I've always just used `# encoding: utf-8` – Thilo Jan 17 '14 at 09:38
  • it is one of the available syntaxes, see http://stackoverflow.com/questions/8879237/how-does-the-magic-comment-encoding-utf-8-in-ruby-works as well – xlembouras Jan 17 '14 at 10:12
1

Try add

config.action_mailer.default_charset = "utf-8"

to your

config/environments/development.rb.

Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70
1

I have found a solution to this issue i modified the patch https://gist.github.com/tmiller/5808900 and checked if the attachment then i have used the mail gem default methods it seems there is some bug in the mail gem while adding the attachment as a part.

yednamus
  • 582
  • 1
  • 4
  • 22