首页 > 解决方案 > 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 邮件时,我发现它们具有相同的内容,没有文本版本,所以我有点困惑,在这种情况下我该怎么办?

在此处输入图像描述

标签: ruby-on-railsactionmailer

解决方案


我会像往常一样回答我的答案;)

作为最佳实践:

首先:确保您的 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>我们不希望在文本电子邮件中出现的标签。


推荐阅读