このように、Devise::Mailer
を拡張するクラスを作成し、カスタムメールプログラムを使用するには:次に
class MyMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views
end
は、あなたのconfig/initializers/devise.rb
で、"MyMailer"
にconfig.mailer
を設定します。
他のメーラーと同じ方法でMyMailer
を使用できるようになりました。余分なヘッダーを追加するために特定のメールをオーバーライドする場合は、メソッドをオーバーライドしてカスタムメソッドの最後にsuper
を呼び出して、Deviseのデフォルト動作をトリガーします。次のように
は例えば、我々はconfirmation_instructions
電子メールのための新しいヘッダを追加することができます。
def confirmation_instructions(record, token, opts={})
headers["Custom-header"] = "Bar"
super
end
また、手動でオプションを設定することで(、REPLY_TOなどからの)基本的なヘッダのいずれかをオーバーライドすることができます
ハッシュ:(User
はあなたの工夫のモデル名である場合)、プレビューを得るために
def confirmation_instructions(record, token, opts={})
headers["Custom-header"] = "Bar"
opts[:from] = '[email protected]'
opts[:reply_to] = '[email protected]'
super
end
:
# test/mailers/previews/my_mailer_preview.rb
# Preview all emails at http://localhost:3000/rails/mailers/my_mailer
class MyMailerPreview < ActionMailer::Preview
def confirmation_instructions
MyMailer.confirmation_instructions(User.first, "faketoken", {})
end
def reset_password_instructions
MyMailer.reset_password_instructions(User.first, "faketoken", {})
end
def unlock_instructions
MyMailer.unlock_instructions(User.first, "faketoken", {})
end
end
メーラーが新しいメールを並ぶキュー名に制御するために
、(上記、MyMailer
)あなたのクラスに次のメソッドを追加します。
def deliver_later
Devise::Mailer.delay(queue: 'my_queue').send(...)
end