因此,Rails 具有将纯文本电子邮件与 HTML 电子邮件一起发送的出色功能。
您只需在 .html.erb 旁边放置一个 .text.erb。我在这里提出了一个这样的应用程序:https ://github.com/cairo140/rails-email-test 。只需下载并运行。访问主页并查看日志。
这是输出:
Sent mail to test@test.com (19ms)
Date: Tue, 01 Nov 2011 14:48:59 -0400
From: test@test.com
To: test@test.com
Message-ID: <4eb03f1b7403b_178858111fcc060bd@Steven-Xus-Macbook-Pro.local.mail>
Subject: test
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_4eb03f1b708ce_178858111fcc057a4";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_4eb03f1b708ce_178858111fcc057a4
Date: Tue, 01 Nov 2011 14:48:59 -0400
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <4eb03f1b72b72_178858111fcc058ce@Steven-Xus-Macbook-Pro.local.mail>
Unescaped: &
Escaped: &
ERB: &
----==_mimepart_4eb03f1b708ce_178858111fcc057a4
Date: Tue, 01 Nov 2011 14:48:59 -0400
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <4eb03f1b73784_178858111fcc05933@Steven-Xus-Macbook-Pro.local.mail>
<!doctype html>
<html>
<head>
<title>test</title>
</head>
<body>
<ul>
<li>Unescaped: &</li>
<li>Escaped: &</li>
<li>ERB: &</li>
</ul>
</body>
</html>
----==_mimepart_4eb03f1b708ce_178858111fcc057a4--
现在,这是文本视图 ( app/views/application_mailer/index.text.erb
):
$ cat app/views/application_mailer/index.text.erb
Unescaped: &
Escaped: &
ERB: <%= "&" %>
如您所见,生成的文本电子邮件被转义了。
有什么办法可以防止这种情况发生吗?
进一步澄清:
如果您隐藏 HTML 电子邮件并仅发送文本,您会在电子邮件客户端中获得此信息(我使用的是 Gmail):
如您所见,第三行被过度转义。
显然,您可以调用html_safe
每个字符串或raw
每个 ERB 标签,但肯定有一种方法可以让 Rails/Erubis 识别它在文本电子邮件中的事实并相应地转义。