【问题标题】:How do you put dynamic content in an action mailer subject line?您如何将动态内容放入操作邮件主题行中?
【发布时间】:2012-10-10 19:39:38
【问题描述】:

我正在尝试将我的应用程序的名称放在 action mailer 发送的电子邮件的主题行中。使用电子邮件地址的默认值执行此操作没有问题,但是当我尝试将其添加到主题时,它会以纯文本 <%= app_name %> 的形式发送。

这是我的邮件/user_mailer.rb:

class UserMailer < ActionMailer::Base  
  add_template_helper(ApplicationHelper)
  extend ApplicationHelper

  default from: "#{app_name} <#{system_email}>"

  def reset_password_email(user)
    @user = user
    @url  = "#{root_url}/password_resets/#{user.reset_password_token}/edit"
    mail(:to => user.email,
         :subject => "Reset Your Password | #{app_name}")
  end
end

这是我的应用程序助手中的内容:

  def app_name
    'Tip Share'
  end

  def app_domain
    'tipshare.herokuapp.com'
  end

  def system_email
    'info@wingardcreative.com'
  end

#{app_name} 或 &lt;%= app_name %&gt; 都不起作用。我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    您是否尝试过像这样添加它:

    include ApplicationHelper
    

    【讨论】:

      【解决方案2】:

      通过使用ApplicationHelper 扩展UserMailer,这些方法可以作为类方法使用——这就是default from: "#{app_name} &lt;#{system_email}&gt;" 中的引用有效的原因。 在reset_password_email 方法中,app_name 将是一个未定义的局部变量。 所以你要么需要另外include ApplicationHelper,要么用self.class.app_name调用它。

      【讨论】:

      • 嗯,我尝试将"include ApplicationHelper" 放入reset_password_email 方法中,但触发了错误。我尝试了extend ApplicationHelper,但它仍然有效,但仍然以纯文本形式发送&lt;%= app_name %&gt;。如果我使用#{app_name},则没有错误,但电子邮件不会送达。如果我使用&lt;%= self.class.app_name %&gt;,则电子邮件不会送达。
      • include 语句是一个类级指令,就像您在示例代码中的 extend 一样。这些不在方法范围内。 extend 将命名模块中的方法添加为类方法,而include 将它们添加为实例方法。在您当前的reset_password_email 方法中,您需要坚持使用插值#{ app_name }。由于您已经扩展了您的辅助方法,我将在这里使用#{ self.class.app_name },在这种情况下您不需要include。一个或另一个,两者都不需要。
      猜你喜欢
      • 1970-01-01
      • 2011-04-29
      • 2011-04-22
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 2020-03-16
      相关资源
      最近更新 更多