8

デバイスのメール件名のカスタマイズについて多くの議論を見てきましたが、私が望むものを解決しているようには見えません。現在、確認メールの件名は「Qitch.com アカウントの確認」となっています。この電子メールの件名をカスタマイズし、ユーザーの名前の動的な値を追加して、ユーザーALEXがアカウントにサインアップした場合、件名が「Welcome ALEX, confirm your Qitch.com account」の電子メール アドレスを取得するようにしたいと考えています。どうすれば工夫でこれを達成できますか?

devise.en.yml

mailer:
  confirmation_instructions:
    subject: 'Confirm your Qitch.com account'
  reset_password_instructions:
    subject: 'Reset your Qitch.com password'
  unlock_instructions:
    subject: 'Unlock your Qitch.com account'

最後に、返信アドレスまたは差出人アドレスに名前を追加するにはどうすればよいですか。現在、メールを受信すると、送信者:no-reply@qitch.comカスタマイズできる方法はありますかQitch

ありがとう

4

5 に答える 5

15

十分に明確な答えはないので、ここで簡単に要約したいと思います。

  1. まず、Devise元のメーラー メソッドを次のようにオーバーライドすることを伝える必要があります。

config/initializers/devise.rb

config.mailer = 'MyOverriddenMailer'

  1. その後、オーバーライドされたメーラー クラスを作成し、次のように任意のメソッドをオーバーライドする必要があります。

app/mailers/my_overridden_​​mailer.rb

class MyOverriddenMailer < 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 you mailer uses the devise views

  def confirmation_instructions(record, token, opts={})
    if record.name.present?
      opts[:subject] = "Welcome #{record.name}, confirm your Qitch.com account"
    else
      opts[:subject] = "Confirm your Qitch.com account"
    end

    super
  end

end
  1. Rails サーバーを再起動して、変更を適用することを忘れないでください。:)

:

  • オプションのリストopts: subject、to、from、reply_to、template_path、template_name。
  • recordUserモデルのインスタンスです
  • そしてもちろん、元のドキュメント
于 2015-01-26T09:06:24.647 に答える
5

ここでヘルパーを作成し、ハウツー: カスタムメーラーを使用する

class MyMailer < Devise::Mailer


   def confirmation_instructions(record, opts={})
    headers = {
        :subject => "Welcome  #{resource.name}, confirm your Qitch.com account"
    }
    super
  end

  def reset_password_instructions(record, opts={})
    headers = {
        :subject => "Welcome  #{resource.name}, reset your Qitch.com password"
    }
    super
  end

  def unlock_instructions(record, opts={})
    headers = {
        :subject => "Welcome  #{resource.name}, unlock your Qitch.com account"
    }
    super
  end

end

または

class MyMailer < Devise::Mailer
...
...
private

 def headers_for(action)
  if action == :confirmation_instructions
    headers = {
      :subject => "Welcome  #{resource.name}, confirm your Qitch.com account"
    }
  elsif action == :reset_password_instructions
    headers = {
      :subject => "Welcome  #{resource.name}, reset your Qitch.com password"
    }
  else
    headers = {
      :subject => "Welcome  #{resource.name}, unlock your Qitch.com account"
        }
  end
 end
end

そしてメーラーを使うようにdeviseに伝えます:

#config/initializers/devise.rb
config.mailer = "MyMailer"

注:まだ試していませんが、役に立つかもしれません。誰にとっても、私の答えを修正してください。エラーがある場合は、私の答えを編集できます

于 2013-05-24T21:28:58.690 に答える
4

私は作業してdevise (3.2.1)おり、次のソリューションを実装しましたが、from:ローカリゼーションでフィールドを変更するには:

# app/mailers/devise_mailer.rb
class DeviseMailer < Devise::Mailer

  def confirmation_instructions(record, token, opts={})
    custom_options(opts)
    super
  end

  def reset_password_instructions(record, token, opts={})
    custom_options(opts)
    super
  end

  def unlock_instructions(record, token, opts={})
    custom_options(opts)
    super
  end

  private

  def custom_options(opts)
    opts[:from] = I18n.t('devise.mailer.from', name: Tenancy.current_tenancy.name, mail: ENV['FROM_MAILER'] )
  end
end

次に、ロケールファイルでメッセージを定義しました

# config/locales/devise.es.yml
es:
  devise:
    mailer:
      from: "Portal de trabajo %{name} <%{mail}>"

件名を変更するには、ほぼ同じにする必要があります。

  def confirmation_instructions(record, token, opts={})
    custom_options(opts, :confirmation_instructions)
    super
  end

  private

  def custom_options(opts, key)
    opts[:from] = I18n.t('subject', scope: [:devise, :mailer, key])
  end

# and in your locale file
es:
  devise:
    mailer:
      confirmation_instructions:
        subject: Instrucciones de confirmación
于 2013-11-22T00:05:33.587 に答える
-2

次のような ActionMailer を作成する必要があります。

class Sender < ActionMailer::Base
   default :from => "'Eventer' <dfghjk@gmail.com>"

   def send_recover(user, pw)
      mail(:to => user.email , :subject => "You have requested to change your password from Eventer") do |format|
             format.text { 
             render :text =>"Dear #{user.name}, 

                             **Body**

                             Regards."
            }
      end
   end
end

次に、この方法でコントローラーから呼び出す必要があります。

Sender.send_recover(@mail, current_user, @meetup).deliver

うまくいくことを願っています!

よろしく

于 2013-05-24T21:31:18.937 に答える