[英]ruby mail gem - set charset in deliver block
我正在使用郵件 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 生成的文檔以及源代碼,我仍然無法設置字符集。 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()
返回當前字符集,它不允許設置一個並且不接受任何參數。
為此,您需要使用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!
您還需要為各個部分設置編碼。 maxdec 的回答顯示了它。 確保您也對 text_part 執行此操作。
這對我有用。
mail = Mail.deliver do
charset='UTF-8'
content_transfer_encoding="8bit"
require 'pry';binding.pry
to 'xxx@xxx.yy'
from 'yyy@yyy.ss'
subject "Tet with äöüß"
text_part do
content_type "text/plain; charset=utf-8"
body <<-EOF
this is a test with äöüß
EOF
end
end
mail.deliver!
我使用郵件(2.7.1), charset
和content_transfer_encoding
都不適合我。
charset='UTF-8'
content_transfer_encoding="8bit"
以下對我有用!
content_type "text/plain; charset=utf-8"
添加
# encoding: utf-8
在文件的開頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.