ERB使用HAML和ActionMailer在多部分电子邮件中显示

时间:2011-02-07 14:46:42

标签: ruby-on-rails-3 haml actionmailer erb

我正在尝试使用Rails 3 ActionMailer和HAML构建多部分电子邮件。我不确定它是否是命名约定或问题是什么,视图的HAML部分正确呈现,但纯文本(使用ERB)不是。我在出站电子邮件的纯文本版本中看到了实际的ERB代码。

我定义了以下文件:

app/views/layouts/email.html.haml
app/views/layouts/email.text.erb
app/views/user_mailer/password_reset_instructions.html.haml
app/views/user_mailer/password_reset_instructions.text.erb

我的邮件是这样的:

class UserMailer < ActionMailer::Base
  helper :application
  helper :user_mailer
  layout "email"

  #layout "email"
  # single method for either a password reset or initial account activation
  def password_reset_instructions(user)  
    setup_email(user)
    user.reload
    subject = (user.confirmed? ? "Password Reset Instructions" : "New Account Information")  
    @user = user
    @edit_password_reset_url = url_for(  :controller => "password_resets", 
                                      :id => user.perishable_token,
                                      :action => 'edit',
                                      :host => "#{user.subdomain}.#{Assignr::HOST_NAME}")
    @assignor_name = user.creator ? user.first_last_name : 'Your assignor'
    @site = user.site
    mail :to => @setup[:to], :from => @setup[:from], :subject => @setup[:subject] + subject    
  end

  protected
    def setup_email(user)
      # send to one or both e-mail addresses
      user.reload
      recipients = []
      recipients << user.first_last_name + " <" + user.email + ">" if user.email 
      recipients << user.first_last_name + " <" + user.email2 + ">" if user.email2 
      @recipients  = recipients.join(", ")
      @setup = {:to => @recipients, :from => email_address_for(user) , :subject => "assignr.com: "}
      @user = user
    end

    def email_address_for(user)
      "assignr.com <#{user.site_id}-#{user.id}@email.#{Assignr::HOST_NAME}>"
    end

end

我的ERB纯文本版本如下所示:

<% if @user.confirmed? -%>
A request to reset your password has been made.
If you did not make this request, simply ignore this email.
If you did make this request just click the link below:
<% else -%>
An account with assignr.com has been created for you by <%= @user.creator ? @user.creator.first_last_name : 'your assignor'%>. 

assignr.com is a web-based service used to help referee and umpire assignors with game scheduling.  

Please visit the following URL to begin:
<% end -%>

<%= @edit_password_reset_url %>

If the above URL does not work try copying and pasting it into your browser.
If you continue to have problem please feel free to contact us.

<% if !@user.confirmed? and @upcoming_game_count > 0 %> 
<%= @user.creator ? @user.creator.first_last_name : 'Your assignor'%> has assigned you to <%= pluralize( @upcoming_game_count, 'game') %>, beginning <%= humanize_time(@next_game.date) %>.  To view detailed information on these assignments, you will need to click the URL above and follow the on-screen instructions to register.  
<% end %>

<%= render :partial => "footer"%>

实际的多部分传递电子邮件如下所示(请注意生成的电子邮件中的ERB代码):

Delivered-To: <snip>
Received: by 10.216.163.14 with SMTP id z14cs47630wek;
        Mon, 7 Feb 2011 06:21:54 -0800 (PST)
Received: by 10.142.141.16 with SMTP id o16mr4281923wfd.390.1297088513239;
        Mon, 07 Feb 2011 06:21:53 -0800 (PST)
Return-Path: <snip>
Received: from assignr.com (email.assignr.com [0.0.0.0])
        by mx.google.com with ESMTPS id g39si10275834yhd.193.2011.02.07.06.21.52
        (version=TLSv1/SSLv3 cipher=RC4-MD5);
        Mon, 07 Feb 2011 06:21:53 -0800 (PST)
Received-SPF: pass (google.com: domain of <snip> designates 0.0.0.0 as permitted sender) client-ip=0.0.0.0;
Authentication-Results: mx.google.com; spf=pass (google.com: domain of snip designates 0.0.0.0 as permitted sender) smtp.mail=snip
Received-SPF: pass (assignr.com: domain of snip designates 127.0.0.1 as permitted sender) receiver=assignr.com; client-ip=127.0.0.1; helo=localhost.localdomain; envelope-from=snip; x-software=spfmilter 0.97 http://www.acme.com/software/spfmilter/ with libspf2-1.0.0;
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
        by assignr.com (8.14.4/8.14.3) with ESMTP id p17ELhYP013916
        for <snip>; Mon, 7 Feb 2011 09:21:43 -0500
Date: Mon, 07 Feb 2011 09:21:43 -0500
From: "assignr.com" <snip>
To: Jeff Wigal <snip>
Message-ID: <4d4ffff7d072_362b55b3bbe40347@assignr.com.mail>
Subject: assignr.com: Password Reset Instructions
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_4d4ffff6d6fb6_362b55b3bbe4000";
 charset=UTF-8
Content-Transfer-Encoding: 7bit



----==_mimepart_4d4ffff6d6fb6_362b55b3bbe4000
Date: Mon, 07 Feb 2011 09:21:43 -0500
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <4d4ffff78acc_362b55b3bbe40119@assignr.com.mail>

<% if @user.confirmed? -%>
A request to reset your password has been made.
If you did not make this request, simply ignore this email.
If you did make this request just click the link below:
<% else -%>
An account with assignr.com has been created for you by <%= @user.creator ? @user.creator.first_last_name : 'your assignor'%>.
assignr.com is a web-based service used to help referee and umpire assignors with game scheduling.
Please visit the following URL to begin:
<% end -%>
<%= @edit_password_reset_url %>
If the above URL does not work try copying and pasting it into your browser.
If you continue to have problem please feel free to contact us.
<% if !@user.confirmed? and @upcoming_game_count > 0 %>
<%= @user.creator ? @user.creator.first_last_name : 'Your assignor'%> has assigned you to <%= pluralize( @upcoming_game_count, 'game') %>, beginning <%= humanize_time(@next_game.date) %>.  To view detailed information on these assignments, you will need to click the URL above and follow the on-screen instructions to register.
<% end %>
<%= render :partial => "footer"%>


CIM Test Site

=======================================
assignr.com
Referee and Umpire Assigning Made Easy


----==_mimepart_4d4ffff6d6fb6_362b55b3bbe4000
Date: Mon, 07 Feb 2011 09:21:43 -0500
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <4d4ffff7b0aa_362b55b3bbe4023d@assignr.com.mail>

<html>
<body>
<style media='screen' type='text/css'>
a { color: #39B2E5; font-weight: bold; }
a:hover { color: #0a97d2; }
p, ul { font: 13px 'Lucida Grande', Lucida Grande, Helvetica, Arial, sans-serif; line-height: 18px; color: #56594c; }
h2 { font: 18px 'Lucida Grande', Lucida Grande, Helvetica, Arial, sans-serif; line-height: 18px; font-weight: bold; color: #56594c; }
h3 { font: 14px 'Lucida Grande', Lucida Grande, Helvetica, Arial, sans-serif; line-height: 14px; font-weight: bold; color: #56594c; }
.clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden;   }
.clearfix { display: inline-block; }
</style>
<div style='padding: 10px; background-color:#ececdf;'>
<table style='margin-bottom: 10px;' width='100%'>
<tr>
<td align='center'>
<img alt='assignr.com' height='50' src='http://assignr.com/images/assignr_com_logo_50.png' width='136'>
</td>
</tr>
</table>
<table style='background: #fff;padding: 20px 10px 10px 10px;border-bottom: 1px solid #e5e7e0;' width='100%'>
<tr width='100%'>
<td style='background:#617494;height: 10px;'>&nbsp;</td>
</tr>
<tr width='100%'>
<td style=' padding: 20px 0px'>
<h2>
Hi
Jeff,
</h2>
<div style="width: 100%; font: 13px 'Lucida Grande', Lucida Grande, Helvetica, Arial, sans-serif; padding-right: 10px; color: #56594c;">

.. assume valid HTML goes here.

<p>
Follow
<a href='http://twitter.com/assignr'>assignr.com on Twitter</a>
<br>
Keep up with the latest news
<a href='http://dropkickstudios.com?utm_source=outbound-email'>on our blog</a>
<br>
Share your
<a href='http://assignr.zendesk.com/forums?utm_source=outbound-email'>questions and feedback</a>
<br>
View
<a href='http://twitter.com/assignr'>assignr.com system status</a>
</p>
</td>
</tr>
<tr width='100%'>
<td style='background:#617494;height: 10px;'>&nbsp;</td>
</tr>
<tr width='100%'>
<td>
<p style='font-size: 11px; color: #b7b9ad; padding-top: 10px; border-top: 1px solid #f5f7f0; '>
Want to modify your assignr.com email address? Visit your
<a href='https://assignr.com/login?utm_source=outbound-email' style='font-weight: normal;'>settings</a>
page.
<br>
assignr.com is a trademark of Dropkick Studios, LLC, all rights reserved.  
</p>
</td>
</tr>
</table>
</div>
</body>
</html>


----==_mimepart_4d4ffff6d6fb6_362b55b3bbe4000--

我错过了什么?

编辑:不知道我做了什么,但它现在似乎正在工作......更新到最新版本的Haml,但我不确定是不是问题。

0 个答案:

没有答案