我正在使用mail gem使用以下代码发送包含utf-8内容的电子邮件

Mail.defaults do
    ...
end

Mail.deliver do
    from    "[email protected]"
    to      "[email protected]"
    subject "Mäbülö..."
    body    "Märchenbücher lösen Leseschwächen."
end

这是有效的,但是给出了警告
Non US-ASCII detected and no charset defined.
Defaulting to UTF-8, set your own if this is incorrect.

现在,经过多次尝试,参考mail gem生成的文档和源代码,我仍然无法设置字符集。message.rb中有一个方法charset=,但是当我添加对charset的调用时,如下所示:
Mail.deliver do
    from    "[email protected]"
    to      "[email protected]"
    charset "UTF-8"
    subject "Mäbülö..."
    body    "Märchenbücher lösen Leseschwächen."
end

我得到这个参数错误:
/usr/local/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:1423:in `charset': wrong number of arguments (1 for 0) (ArgumentError)

如何在传送块内设置字符集?

最佳答案

mail.charset()返回当前字符集,它不允许设置字符集,也不接受任何参数。
为此,您需要使用mail.charset = ...
实际上在这个街区里面有可能有:

Mail.deliver do
  from    "[email protected]"
  to      "[email protected]"
  subject "Mäbülö..."
  body    "Märchenbücher lösen Leseschwächen."
  charset = "UTF-8"
end

也可以不使用块:
mail         = Mail.new
mail.charset = 'UTF-8'
mail.content_transfer_encoding = '8bit'

mail.from    = ...
mail.to      = ...
mail.subject = ...

mail.text_part do
  body ...
end

mail.html_part do
  content_type 'text/html; charset=UTF-8'
  body ...
end

mail.deliver!

关于ruby - ruby 邮件 gem -在传递块中设置字符集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16859980/

10-13 00:11