【问题标题】:How can I customize Devise to send password reset emails using PostMark mailer如何自定义设计以使用 PostMark 邮件程序发送密码重置电子邮件
【发布时间】:2011-08-06 11:30:20
【问题描述】:

我正在尝试使用PostMarkApp 并利用Rails gem(postmark-railspostmark-gemmail)将我系统的所有电子邮件通知集中在一个保护伞下。我已成功创建了一个邮件程序,用于处理发送购买收据,但我无法接收忘记密码的电子邮件。我的开发日志显示 Devise 发送了消息,但我的收件箱中没有收到任何电子邮件,并且 PostMark 积分没有减少。

让 Devise 的邮件通过我的 PostMark 帐户发送的最佳或最简单的方法是什么?

来自 config/environments/development.rb 的片段

config.action_mailer.delivery_method      = :postmark
config.action_mailer.postmark_settings    = { :api_key => "VALID_API_KEY_WAS_HERE" }
config.postmark_signature                 = VALID_POSTMARK_SIGNATURE_WAS_HERE

我的使用邮戳的邮件程序

class Notifier < ActionMailer::Base
  # set some sensible defaults
  default :from => MyApp::Application.config.postmark_signature

  def receipt_message(order)
    @order = order
    @billing_address = order.convert_billing_address_to_hash(order.billing_address)

    mail(:to => @order.user.email, :subject => "Your Order Receipt", :tag => 'order-receipt', :content_type => "text/html") do |format|
      format.html
    end
  end
end

编辑:我的问题的解决方案如下

通过让我的 Notifier 邮件程序扩展 Devise::Mailer 并指定 Devise 使用我的通知程序作为 config/initializers/devise.rb 内的邮件程序解决了这个问题

来自 config/initializers/devise.rb 的 sn-p

# Configure the class responsible to send e-mails.
config.mailer = "Notifier"

我的通知邮件程序现在

class Notifier < Devise::Mailer
  # set some sensible defaults
  default :from => MyApp::Application.config.postmark_signature

  # send a receipt of the Member's purchase
  def receipt_message(order)
    @order = order
    @billing_address = order.convert_billing_address_to_hash(order.billing_address)

    mail(:to => @order.user.email, :subject => "Your Order Receipt", :tag => 'order-receipt', :content_type => "text/html") do |format|
      format.html
    end
  end

  # send password reset instructions
  def reset_password_instructions(user)
     @resource = user
     mail(:to => @resource.email, :subject => "Reset password instructions", :tag => 'password-reset', :content_type => "text/html") do |format|
       format.html { render "devise/mailer/reset_password_instructions" }
     end
   end
end

【问题讨论】:

  • 仅供参考,您可以(并且应该)将您的解决方案添加为答案,然后接受它。

标签: devise forgot-password ruby-on-rails-3 postmark


【解决方案1】:

如果您还想在邮戳标题中指定“标签”,则必须在邮件中执行此操作:

  # this override method is from Devise::Mailers::Helpers
  def headers_for(action)
    headers = {
      :subject        => translate(devise_mapping, action),
      :from           => mailer_sender(devise_mapping),
      :to             => resource.email,
      :template_path  => template_paths,
      :tag            => action.dasherize # specify the tag here
    }
    if resource.respond_to?(:headers_for)
      headers.merge!(resource.headers_for(action))
    end
    unless headers.key?(:reply_to)
      headers[:reply_to] = headers[:from]
    end
    headers
  end

【讨论】:

  • 我不得不将此行从 :tag =&gt; action.dasherize 更改为 :tag =&gt; action.to_s.dasherize。否则这似乎工作得很好。
【解决方案2】:

使用最新版本的 Devise,上述方法对我没有帮助。这是我的解决方案。

在 config/application.rb 中:

config.action_mailer.delivery_method   = :postmark
config.action_mailer.postmark_settings = { :api_key => "your-API-key-here" }

在 config/initializers/devise.rb:

config.mailer = "UserMailer" # UserMailer is my mailer class

在 app/mailers/user_mailer.rb 中:

class UserMailer < ActionMailer::Base
  include Devise::Mailers::Helpers

  default from: "default@mydomain.com"

  def confirmation_instructions(record)
    devise_mail(record, :confirmation_instructions)
  end

  def reset_password_instructions(record)
    devise_mail(record, :reset_password_instructions)
  end

  def unlock_instructions(record)
    devise_mail(record, :unlock_instructions)
  end

  # you can then put any of your own methods here
end

最后,确保您已生成自定义设计视图

rails generate devise:views

并将电子邮件模板从 app/views/devise/mailer/ 移动到 app/views/user_mailer/

mv app/views/devise/mailer/* app/views/user_mailer/

【讨论】:

  • 需要更新为def reset_password_instructions(record,token,options)
  • 以防万一有人发现这一点,最新版本的 Devise 的默认邮件定义有三个参数,而不是一个。 github.com/plataformatec/devise/blob/…
【解决方案3】:

我还必须为设计生成视图并将邮件模板复制到我的邮件程序的正确位置。像这样的 -

rails generate devise:views
cp app/views/devise/mailer/* app/views/notification_mailer/

【讨论】:

    猜你喜欢
    • 2019-12-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多