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

发布于 2024-10-16 00:38:49 字数 9939 浏览 5 评论 0原文

我正在尝试使用 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 [email protected]? 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: <[email protected]>
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: <[email protected]>

<% 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 [email protected]? 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: <[email protected]>

<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 但我不确定这就是问题所在。

I'm trying to build a multipart e-mail using Rails 3 ActionMailer and HAML. I'm not sure if it's the naming convention or what the issue is, the HAML part of the view is being rendered correctly, but the plain text (with ERB) is not. I'm seeing actual ERB code in the plain text version of my outbound emails.

I have the following files defined:

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

My mailer looks like this:

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

My ERB plain text version looks like this:

<% 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 [email protected]? 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"%>

The actual multipart delivered e-mail looks like this (note the ERB code in the generated email):

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: <[email protected]>
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: <[email protected]>

<% 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 [email protected]? 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: <[email protected]>

<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;'> </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;'> </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--

What am I missing?

EDIT: Not sure what I did, but it seems to be working now... updated to latest version of Haml but I'm not sure that was the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文