Rails: How to send emails to a recipient containing umlauts?

…衆ロ難τιáo~ 提交于 2020-01-24 08:50:08

问题


I'ld like to send an email with the following setup

def registration_confirmation(user)
  recipients    user.username + "<" + user.email + ">"
  from          "Netzwerk Muensterland<mailer@netzwerkmuensterland.de>"
  subject       "Vielen Dank für Ihre Registrierung"
  body          :user => user
  content_type  "text/html"
end

The subject line contains an umlaut and works fine. The log says me, it was encoded like this:

=?utf-8?Q?Vielen_Dank_f=C3=BCr_Ihre_Registrierung?=

But, if the user.username contains umlauts, the email will not send. I am using a google apps smtp server. How do I accomplish a encoding like this for recipients?


回答1:


I made it! There is a ActionMailer method called quote_if_necessary which takes care of this kind of problem.

def registration_confirmation(user)
  recipients    quote_if_necessary(user.username, "utf-8") + "<" + user.email + ">"
  from          quote_if_necessary("Netzwerk Münsterland", "utf-8") + " <mailer@netzwerkmuensterland.de>"
  subject       "Vielen Dank für Ihre Registrierung"
  body          :user => user
  content_type  "text/html"
end


来源:https://stackoverflow.com/questions/1961281/rails-how-to-send-emails-to-a-recipient-containing-umlauts

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