【发布时间】:2020-12-16 18:18:19
【问题描述】:
我正在使用 AmazonSes 服务通过 Rails Web 应用程序发送电子邮件。我正在发送不同类型的电子邮件,一切正常,但在下一个场景中:
用户可以在表单的 textarea 字段中编写文本,例如带有换行符的消息。此消息存储在我们的 MySQL 数据库中,并通过电子邮件发送给另一个用户。我遇到了换行问题,因为取决于我是否在 Gmail 网络邮件或 Mac Mail 中打开电子邮件,是否尊重换行符。我已经阅读了很多类似问题的帖子并尝试过,但不幸的是我无法解决它。
如前所述,我正在使用 Rails Mailer 通过 AmazonSES 服务发送电子邮件。
这是我从数据库中的 textarea 字段中存储的内容:"messagePreOffer"=>"This is paragraph 2.\n\nThis is paragraph 2.\n\nThis is paragraph 3."
-
在呈现邮件模板之前,messagePreOffer 日志显示:
offer.message_pre_offer=这是第 1 段。
这是第 2 段。
这是第 3 段。
-
简化的邮件模板布局:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- NAME: 1:2 COLUMN --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <p><%= @offer.message_pre_offer %></p> </body> </html> -
我们用邮件发送电子邮件(我认为这无关紧要):
mail(from: APP_CONFIG['EMAIL_FROM'], to: offer.email, bcc: APP_CONFIG['EMAIL_FROM'], subject: t('new_offer_user_subject', departure: @offer.enquire.departure)) -
邮件投递配置:
config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => "email-smtp.eu-west-1.amazonaws.com", :port => 587, ... :authentication => :login, :enable_starttls_auto => true }
测试 1: 这是我运行并发送邮件后得到的日志:
Rendering email_service/send_new_offer_user_notification.html.erb within layouts/email_service
Rendered email_service/send_new_offer_user_notification.html.erb within layouts/email_service (2.0ms)
EmailService#send_new_offer_user_notification: processed outbound mail in 75.5ms
Sent mail to *****@gmail.com (1117.1ms)
Date: Wed, 16 Dec 2020 17:56:23 +0100
From: *******
To: *****@gmail.com
Message-ID: <5fda3c373232d_42433fc7916a717c9102b@MacBook-Pro-****.local.mail>
Subject: Whatever
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5fda3c3731200_42433fc7916a717c909a4";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5fda3c3731200_42433fc7916a717c909a4
Content-Type: text/plain;
**charset=UTF-8
Content-Transfer-Encoding: 7bit**
This is paragraph 1.
This is paragraph 2.
This is paragraph 3.
----==_mimepart_5fda3c3731200_42433fc7916a717c909a4
Content-Type: text/html;
**charset=UTF-8
Content-Transfer-Encoding: 7bit**
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- NAME: 1:2 COLUMN -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<p>This is paragraph 1.
This is paragraph 2.
This is paragraph 3.</p>
</body>
</html>
----==_mimepart_5fda3c3731200_42433fc7916a717c909a4--
有了这个初始配置,我得到了下一个结果:
我明白这是对的,因为我没有添加任何格式。
测试 2: 在为写这篇文章进行测试时,我更进了一步。我的第二个测试刚刚添加了下一个 css,以保持换行符:
<p style="white-space: pre-line;"><%= @offer.message_pre_offer %></p>
运行测试后,我非常惊讶它在 Gmail 网络邮件和 Mac 邮件中都能完美运行。在此处查找屏幕:
测试 3: 上一个测试中使用的行仅用于测试目的。随着测试的进行,我尝试使用西班牙语的真实文本模板。令人惊讶的是,测试没有成功。
这是变量日志:
"messagePreOffer"=>"Este es el párrafo 1.\n\nEste es el párrafo 2.\n\nEste es el párrafo 3."
这是我运行并发送邮件后得到的日志:
Sent mail to ***@gmail.com (1130.8ms)
Date: Wed, 16 Dec 2020 18:53:23 +0100
From: *****
To: *****
Message-ID: <5fda499338a0f_42433fc7916a717c946d1@MacBook-Pro-de-******.local.mail>
Subject: Whatever
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5fda49933789a_42433fc7916a717c945df";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5fda49933789a_42433fc7916a717c945df
Content-Type: text/plain;
charset=UTF-8
**Content-Transfer-Encoding: quoted-printable**
Este es el p=C3=A1rrafo 1.=0D
=0D
Este es el p=C3=A1rrafo 2.=0D
=0D
Este es el p=C3=A1rrafo 3.=
----==_mimepart_5fda49933789a_42433fc7916a717c945df
Content-Type: text/html;
charset=UTF-8
**Content-Transfer-Encoding: quoted-printable**
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.o=
rg/TR/xhtml1/DTD/xhtml1-strict.dtd">=0D
<html xmlns=3D"http://www.w3.org/1999/xhtml" xmlns=3D"http://www.w3.org/1=
999/xhtml">=0D
<head>=0D
<!-- NAME: 1:2 COLUMN -->=0D
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF-8" =
/>=0D
<meta name=3D"viewport" content=3D"width=3Ddevice-width, initial-scale=3D=
1.0" />=0D
</head>=0D
<body>=0D
<p style=3D"white-space: pre-line;">Este es el p=C3=A1rrafo 1.=0D
=0D
Este es el p=C3=A1rrafo 2.=0D
=0D
Este es el p=C3=A1rrafo 3.</p>=0D
</body>=0D
</html>=0D
----==_mimepart_5fda49933789a_42433fc7916a717c945df--
但是,在 Gmail 网络邮件中,断线是断开的,有很多行和更多空间:
在对内容文本进行了一些测试后,我发现问题是从重音开始的。如果您深入查看日志,在第一个测试中,字符集是:
charset=UTF-8
Content-Transfer-Encoding: 7bit
但是,只要我在文本中包含重音符号,内容传输编码就会自动更改为:
charset=UTF-8
**Content-Transfer-Encoding: quoted-printable**
这就是断线断裂的根本原因。实际上,如果我只是删除重音,Gmail 中的一切都会再次运行:
现在,我找到了问题的根本原因,但我一直在弄清楚哪个是正确的解决方案。
【问题讨论】:
标签: ruby-on-rails email gmail amazon-ses content-encoding