Rails HTML电子邮件显示HTML代码

4
我正在制作一封欢迎新用户的HTML邮件,但它无法正常工作。当我发送邮件时,它将HTML代码显示为文本,而不是呈现实际的HTML。我使用Mailchimp的模板开始制作我的模板的HTML,所以我很惊讶它不起作用。此外,当我使用Putsmail.com和emailonacid.com发送电子邮件时,它看起来很好。
然而,在生产中,邮件发送并且HTML在邮件中显示为文本,如下所示(来自registration_confirmation.html.erb):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" style="width: 100% !important; -webkit-text-size-adjust: none !important; background-color: #ffa500 !important; margin: 0; padding: 0;" bgcolor="#ffa500">
   <center>
     <table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="backgroundTable" style="height: 100% !important; width: 100% !important; margin: 0; padding: 0;">
       <tr>
         <td align="center" valign="top">
         <!-- // Begin Template Preheader \\ -->
           <table border="0" cellpadding="10" cellspacing="0" width="600" id="templatePreheader" style="background-color: #ffa500;" bgcolor="#ffa500">
             <tr>
               <td valign="top" class="preheaderContent">
                 <table border="0" cellpadding="10" cellspacing="0" width="100%">
                   <tr>
                     <td valign="top">
                       <div mc:edit="std_preheader_content" style="color: #505050; font-family: Arial; font-size: 10px; line-height: 100%; text-align: left;" align="left">
                       </div>
                     </td>
                     <td valign="top" width="180">
                       <div mc:edit="std_preheader_links" style="color: #505050; font-family: Arial; font-size: 10px; line-height: 100%; text-align: left;" align="left">
                       </div>
                     </td>
                   </tr>
                 </table>
               </td>
             </tr>
           </table>

           <table border="0" cellpadding="0" cellspacing="0" width="600" id="templateContainer" style="background-color: #ffffff; border: 1px solid #dddddd;" bgcolor="#ffffff">
             <tr>
               <td align="center" valign="top">
                 <table border="0" cellpadding="10" cellspacing="0" width="600" id="templateBody">
                   <tr>
                     <td valign="top" class="bodyContent" style="background-color: #ffffff;" bgcolor="#ffffff">
                       <table border="0" cellpadding="10" cellspacing="0" width="100%">
                         <tr>
                           <td>
                             <a href="http://foobar.com">                                                                                                                      
                               <img src="https://s3.amazonaws.com/images/email_logo.gif" style="float:right;" alt="logo" />
                             </a>
                           </td>
                         </tr>

                         <tr>
                           <td valign="top">
                             <div mc:edit="std_content00" style="color: #505050; font-family: Arial; font-size: 14px; line-height: 150%; text-align: left;" align="left">
                               <span class="h2" style="color: #202020; display: block; font-family: Arial; font-size: 22px; font-weight: bold; line-height: 100%; margin-bottom: 10px; text-align: left;">Welcome to <%= user.name %>!</span>
                               Thanks for registering!<br /><br />
                             </div>                                                                                                                
                           </td>
                         </tr>
                       </table>
                     </td>
                   </tr>
                 </table>
               </td>
             </tr>

             <tr>
               <td align="center" valign="top">     
                 <table border="0" cellpadding="10" cellspacing="0" width="600" id="templateFooter" style="background-color: #FDFDFD; border-top-width: 0;" bgcolor="#FDFDFD">
                 <tr>
                   <td valign="top" class="footerContent"></td>
                 </tr>
               </table>
             </td>
           </tr>
         </table>
         <br>
       </td>
     </tr>
   </table>
 </center>

 <style type="text/css">
   body { width: 100% !important; }
   body { -webkit-text-size-adjust: none !important; }
   body { margin: 0 !important; padding: 0 !important; }
   img { border: none !important; font-size: 14px !important; font-weight: bold                                    
   !important; height: auto !important; line-height: 100% !important; outline: none  
   !important; text-decoration: none !important; text-transform: capitalize !important; }
   #backgroundTable { height: 100% !important; margin: 0 !important; padding: 0
   !important; width: 100% !important; }
   body { background-color: #FAFAFA !important; }
   .preheaderContent div a:visited { color: #336699 !important; font-weight: normal
   !important; text-decoration: underline !important; }
   .headerContent a:visited { color: #336699 !important; font-weight: normal
   !important; text-decoration: underline !important; }
   .bodyContent div a:visited { color: #336699 !important; font-weight: normal
   !important; text-decoration: underline !important; }
   .footerContent div a:visited { color: #336699 !important; font-weight: normal
   !important; text-decoration: underline !important; }
   body { background-color: #ffa500 !important; }
 </style>

我的邮件客户端:

def registration_confirmation(user)
  @user = user
  subject     "Welcome #{@user.first_name}!"
  from        "info@foobar.com"
  recipients  @user.email
  sent_on     Time.now
end

从用户注册模型调用:

Notifier.registration_confirmation(user).deliver
1个回答

3

您似乎缺少 content-type 内容类型:

def registration_confirmation(user)
  @user = user
  subject       "Welcome #{@user.first_name}!"
  from          "info@foobar.com"
  recipients    @user.email
  sent_on       Time.now

  # Set content-type header
  content_type "text/html"
end

它默认为text/plain,因此您必须明确地为HTML或多部分消息设置它。


谢谢你,Michael!如果我想制作多部分电子邮件,以便我有文本和HTML版本,我可以使用相同的content_type吗?我想在Notifier中为所有电子邮件设置它(并且让它们都具有多部分)。 - yellowreign
@yellowreign 请查看 ActionMailer 文档示例以获取多部分电子邮件。您需要不同的内容类型,然后为每个 MIME 部分(纯文本和 HTML)调用 part - Michael Berkowski
@MichaelBerkowski,我正在尝试使用mail(:body => @body, subject: @subject, :to => @recipients, :headers => @headers, :content_type => "text/html")调用我的方法...但它仍然发送文本..这是它的正确写法吗? - abbood
@abbood,不清楚哪个模块或库提供了您的 mail() 方法,因此指定其他标头的过程可能会有所不同,但由于您有一个 :headers => @headers 键,我认为您需要将 content-type 添加到该 @headers 哈希中,因为它必须成为消息中的标头。 - Michael Berkowski
@MichaelBerkowski 我正在使用 ActionMailer,它是 Rails 的一部分。我查看了 headers documentation,但它们是电子邮件头(例如收件人、发件人、主题等),与内容类型无关。 - abbood
但是内容类型是邮件头。即使您在文档中没有看到它,请尝试将其插入到“:headers”哈希表中。 - Michael Berkowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接