ruby-on-rails - Rails actionMailer Message 只有 text/html MIME 部分
问题描述
我正在使用 ActionMailer 通过第三方 SMTP 服务器发送带有 rails 的电子邮件,电子邮件总是落入垃圾邮件文件夹(发送到 gmail 地址时),我联系了支持人员,他们使用 mail-tester.com 进行了测试,这表明有以下问题:
Message only has text/html MIME parts
You should also include a text version of your message (text/plain)
在我的邮件视图中,我只有 html 文件,但是当我查看 Devise 邮件时,我发现它们具有相同的内容,没有文本版本,所以我有点困惑,在这种情况下我该怎么办?
解决方案
我会像往常一样回答我的答案;)
作为最佳实践:
首先:确保您的 Html 电子邮件包含在一个 html 标记中,或者更好地使用这个:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<!-- your html email here -->
</body>
</html>
第二:除了你的每一个 html 电子邮件文件,添加一个具有.text.erb
扩展名的等效文件,例如,如果你有一个名为的邮件程序文件,reset_password.html.erb
那么让你添加另一个reset_password.text.erb
只包含纯文本的文件(永远没有 html 标记!)。
这样,如果收件人不使用 HTML 电子邮件,则将使用您的电子邮件的文本版本。
这是一个如何将设计 html 模板之一转换为纯文本的示例:
HTML 版本(confirmation_instructions.html.erb):
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<p>Welcome <%= @email %>!</p>
<p>You can confirm your account email through the link below:</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
</body>
</html>
文本版本(confirmation_instructions.text.erb):
Welcome <%= @email %>!
You can confirm your account email through the link below:
<%= confirmation_url(@resource, confirmation_token: @token) %>
还要注意我是如何改变的:
<%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %>
至:
<%= confirmation_url(@resource, confirmation_token: @token) %>
因为link_to会生成<a>
我们不希望在文本电子邮件中出现的标签。
推荐阅读
- reactjs - Formik & 是的 | 多文件上传验证
- maven - 嗨,我已经安装了 JDK 16.0.1 版本,并且在 eclipse 中我创建了一个 maven 项目,我想添加黄瓜依赖项
- java - 如何使用 Java 在 SQL 数据库中存储多维数组?
- javascript - Puppeteer - 找不到选择器的节点
- typescript - 如何 console.log 打字稿类型
- keyboard - 在没有管理员权限的情况下重新映射密钥
- python - IBM Db2 SQL Alchemy 适配器——客户端驱动程序问题
- python - 数组结果中的条件
- python - 如何制作一个简单的 Python 文本服务器
- javascript - 验证出错