如何在Rails的电子邮件主题中加上英镑符号

时间:2013-09-09 14:55:47

标签: ruby-on-rails actionmailer

以下不起作用

subject = "The amount of £#{@amount} has been sent to your account".html_safe
mail(:to => @email, :subject => subject, :reply_to => 'support@xyz.com')

也没有

subject = "The amount of £#{@amount} has been sent to your account".html_safe

如何在电子邮件主题中显示英镑符号?

1 个答案:

答案 0 :(得分:3)

电子邮件主题(因为电子邮件中的所有标题)不接受网页上使用的HTML实体。相反,他们对非ASCII数据使用了几种不同的编码,所有这些编码都应由mail gem自动应用。

因此,最简单的方法就是将角色直接放入主题变量中,而不需要自己应用任何额外的编码:

subject = "The amount of £#{@amount} has been sent to your account"
mail(:to => @email, :subject => subject, :reply_to => 'support@xyz.com')

请注意,除非您使用Ruby 2.0(默认为utf-8源编码iirc),否则您可能必须在源文件的顶部放置一个magic encoding comment,以便ruby将您的源代码解释为UTF -8与此类似

# encoding: UTF-8