加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby mail gem – 在交付块中设置charset

发布时间:2020-12-17 03:47:50 所属栏目:百科 来源:网络整理
导读:我正在使用mail gem使用此代码发送带有UTF-8内容的电子邮件 Mail.defaults do ...endMail.deliver do from "user@example.com" to "otheruser@example.com" subject "M?bül?..." body "M?rchenbücher l?sen Leseschw?chen."end 这有效,但会发出警告 Non US
我正在使用mail gem使用此代码发送带有UTF-8内容的电子邮件

Mail.defaults do
    ...
end

Mail.deliver do
    from    "user@example.com"
    to      "otheruser@example.com"
    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.

现在经过多次尝试,咨询邮件gem生成的文档以及源代码,我仍然无法设置charset.在Message.rb中有一个方法charset =,但是当我添加对charset的调用时,如下所示:

Mail.deliver do
    from    "user@example.com"
    to      "otheruser@example.com"
    charset "UTF-8"
    subject "M?bül?..."
    body    "M?rchenbücher l?sen Leseschw?chen."
end

我得到这个ArgumentError:

/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()返回当前的charset,它不允许设置一个并且不接受任何参数.

为此,您需要使用mail.charset = …

它实际上可能在块内:

Mail.deliver do
  from    "user@example.com"
  to      "otheruser@example.com"
  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!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读