如何使用PostMark邮件程序自定义Devise以发送密码重置电子邮件

gra*_*aos 18 forgot-password devise postmark ruby-on-rails-3

我正在尝试使用PostMarkApp并使用Rails gems(postmark -rails,postmark -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
Run Code Online (Sandbox Code Playgroud)

我的邮件使用邮戳

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
Run Code Online (Sandbox Code Playgroud)

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

通过让我的Notifier邮件程序扩展Devise :: Mailer并指定Devise使用我的通知程序作为邮件程序来解决它config/initializers/devise.rb

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

# Configure the class responsible to send e-mails.
config.mailer = "Notifier"
Run Code Online (Sandbox Code Playgroud)

我的Notifier Mailer现在

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
Run Code Online (Sandbox Code Playgroud)

Adr*_*eil 20

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

在config/application.rb中:

config.action_mailer.delivery_method   = :postmark
config.action_mailer.postmark_settings = { :api_key => "your-API-key-here" }
Run Code Online (Sandbox Code Playgroud)

在config/initializers/devise.rb中:

config.mailer = "UserMailer" # UserMailer is my mailer class
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

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

rails generate devise:views
Run Code Online (Sandbox Code Playgroud)

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

mv app/views/devise/mailer/* app/views/user_mailer/
Run Code Online (Sandbox Code Playgroud)

  • 需要更新为`def reset_password_instructions(record,token,options)` (2认同)

dec*_*lan 0

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

rails generate devise:views
cp app/views/devise/mailer/* app/views/notification_mailer/
Run Code Online (Sandbox Code Playgroud)