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您将覆盖其原始邮件方法:

配置/初始化程序/devise.rb

config.mailer = 'MyOverriddenMailer'

  1. 之后,您需要创建覆盖的邮件程序类并覆盖您想要的任何方法,如下所示:

应用程序/邮件程序/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

并告诉设计使用你的邮件:

#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 回答
0

例如headers_for,为所有设计邮件添加主题前缀。

# config/initializers/devise.rb

Devise.setup do |config|
  # ...
  config.mailer = 'DeviseMailer'
  # ...
end
# app/mailers/devise_mailer.rb

class DeviseMailer < Devise::Mailer
  def headers_for(action, opts)
    super.merge!({ subject: 'Hi ALEX! ' + subject_for(action) })
  end
end
于 2020-10-14T19:20:55.200 回答
-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 回答