ActionMailer non-ASCII characters

柔情痞子 提交于 2020-02-04 11:51:24

问题


I'm trying to send text/plain emails with ActionMailer that have non-ASCII characters in them. However, an email with the contents:

“This has smart quotes”

Displays in emails (and the logs) as:

=E2=80=9CThis has smart quotes=E2=80=9D

And the Content-Transfer-Encoding of the email is quoted-printable. In the view, this text is rendered like so:

<%= raw(strip_tags(@message)) %>

I'm not sure where this is happening, the charset header of the email is UTF-8. This is Ruby 1.9.3-p194 and Rails 3.2.11.


回答1:


I know this is a bit old, but I actually just ran into this problem last week so I'm going to put my findings here in case anyone else has this question.

ActionMailer depends on the mail gem (https://github.com/mikel/mail). Mail adheres to RFC2822 for non multipart emails. RFC2822 compliant means it ONLY allows US-ASCII characters or characters within the range of 1-126 dec. Therefore, what your seeing is the mail gem checks your message body and finds 8bit chars so it sets Content-Transfer-Encoding to quoted-printable converting non US-ASCII chars to their hex equivalent's (E2 80 9C / E2 80 9D | “ / ” | left / right double quotes respectively). If you wish to send emails with non ASCII chars you can by setting content_transfer_encoding to 8bit.

mail = Mail.new
mail.transport_encoding = "8bit"
mail.deliver

Although there may exist mail servers who will deny your email message due to it containing non US-ASCII chars so be ADVISED.



来源:https://stackoverflow.com/questions/14245335/actionmailer-non-ascii-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!