2012-04-09 90 views
3

我正在開發一個html歡迎電子郵件給新用戶,但它不工作。當我發送它時,它將HTML代碼顯示爲文本,而不是呈現實際的HTML。我使用Mailchimp中的模板製作了我的模板的html,所以我很驚訝它不起作用。另外,當我使用Putsmail.com和emailonacid.com發送電子郵件時,它看起來很好。Rails HTML電子郵件顯示HTML代碼

然而,在生產中,電子郵件發送和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  "[email protected]" 
    recipients @user.email 
    sent_on  Time.now 
end 

從用戶登記模型調用:

Notifier.registration_confirmation(user).deliver 

回答

2

你似乎缺少內容類型:

def registration_confirmation(user) 
    @user = user 
    subject  "Welcome #{@user.first_name}!" 
    from   "[email protected]" 
    recipients @user.email 
    sent_on  Time.now 

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

它默認爲text/plain,所以你必須明確地設置它的HTML信息或連鎖信息。

+0

謝謝Michael!如果我想製作多部分電子郵件,所以我有文本和html版本,我可以使用相同的content_type嗎?我想在Notifier中爲我的所有電子郵件設置它(並且它們都有多個部分)。 – yellowreign 2012-04-09 20:50:58

+0

@yellowreign請參閱多部分電子郵件的[ActionMailer](http://rails.rubyonrails.org/classes/ActionMailer/Base.html)文檔示例。你需要一個不同的內容類型,然後爲每個MIME部分調用'part'(plain&html) – 2012-04-09 20:52:37

+0

@MichaelBerkowski我想用'mail(:body => @body,subject:@subject, :to => @recipients,:headers => @headers,:content_type =>「text/html」)'..但它仍然發送文本..是它如何寫入? – abbood 2014-01-03 12:28:30