我在rails 2.3.18中使用mail gem
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
当我发送电子邮件时,如果文件不是英文,则文件名会损坏 当它是非英语字符时我工作得很好
邮件对象看起来像这样:
#<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>>]
这里的文件名是正确的,但是当我使用
发送电子邮件时smtp.sendmail(mail.encoded,sender,destinations) 文件名已损坏
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"
任何人都可以告诉我在邮件gem中编码方法出了什么问题我在邮件中收到的文件名为noname,英文字符正常工作
答案 0 :(得分:2)
尝试
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
答案 1 :(得分:2)
你有没有尝试添加一个
# -*- encoding : utf-8 -*-
在English
类定义文件的顶部?
---编辑---
# encoding: UTF-8
# coding: UTF-8
# -*- coding: UTF-8 -*-
是等价的。
答案 2 :(得分:1)
尝试添加
config.action_mailer.default_charset = "utf-8"
到你的
config/environments/development.rb
。
答案 3 :(得分:1)
我找到了解决这个问题的方法我修改了补丁https://gist.github.com/tmiller/5808900并检查了附件然后我是否使用了邮件gem默认方法似乎邮件gem中存在一些错误,同时将附件添加为一部分。