如何定制Devise使用PostMark邮件发送密码重置邮件

19

我试图使用PostMarkApp和Rails的gem(postmark-rails,postmark-gem,和mail)来将所有系统的电子邮件通知集中到一个地方。我已经成功创建了一个处理发送购买收据的邮件程序,但是我无法接收到忘记密码的电子邮件。我的开发日志显示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

使用 Postmark 的邮件发送器

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使用我的Notifier作为邮件处理器,在config/initializers/devise.rb中指定:

来自config/initializers/devise.rb的代码片段

# 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

7
了解,您可以(也应该)将您的解决方案作为答案添加,并接受它。 - RyanJM
3个回答

21

使用最新版本的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/

2
需要更新为 def reset_password_instructions(record,token,options) - cevaris
以防万一,如果有人发现这个问题,Devise默认邮件定义的最新版本有三个参数,而不是一个。https://github.com/plataformatec/devise/blob/66db52ce31b5d8629f5813a1d7f03a8bc17e5d52/app/mailers/devise/mailer.rb - Joseph

1
如果您想在Postmark邮件头中指定“标签”,则需要在您的邮件程序中执行以下操作:

  # 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 => action.dasherize 改为 :tag => action.to_s.dasherize。否则,其他部分似乎完美运行。 - Webdevotion

0

我还需要为Devise生成视图,并将邮件模板复制到我的邮件发送程序的正确位置。就像这样 -

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接