How can I customize Devise to send password reset emails using PostMark mailer

半腔热情 提交于 2019-11-28 17:36:30

Using the latest version of Devise, the methods above didn't help me. This is my solution.

In config/application.rb:

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

In config/initializers/devise.rb:

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

In 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

Finally, make sure you have generated custom devise views

rails generate devise:views

and move the email templates from app/views/devise/mailer/ to app/views/user_mailer/

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

If you also want to specify 'tags' in postmark headers you have to do this in your mailer:

  # 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

I also had to generate the views for devise and copy the mail templates into the right place for my mailer. Something like this -

rails generate devise:views
cp app/views/devise/mailer/* app/views/notification_mailer/
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!