Rails mailer undefined方法`encoding'for#<hash:0x774b2d51> </hash:0x774b2d51>

时间:2013-10-28 11:40:45

标签: ruby-on-rails actionmailer

我正在尝试在Rails中发送电子邮件。 代码是

class UserMailer < ActionMailer::Base
  default :from => "from@example.com"

  def send_password_reset(player)
    Rails.logger.info "Password email"
    @player = player
    delivery_options = { 
         user_name: 'sites.smtp@mymail.com',
         password: 'password',
         address: 'smtpout.secureserver.net' 
    }
    mail(to: player.email,
         subject: "Recover your Password.", 
         body: "Password email",
         delivery_method_options: delivery_options)
  end
end

控制器

if player.valid?
  player.send_password_reset_token #(Line: 209)
  render json: {SENT: 'YES'}
else
  render json: {error: "Player does not exist."}, status: :not_found
end

模型

  def send_password_reset_token
    password_reset_token = SecureRandom.urlsafe_base64(nil, false)
    self.update_attribute(:password_reset_token, password_reset_token)
    UserMailer.send_password_reset(self).deliver #(line: 25)
  end

记录器说

Sent mail to ranasaani@gmail.com (25ms)

Completed   in 433ms

NoMethodError (undefined method `encoding' for #<Hash:0x774b2d51>):
app/models/player.rb:25:in `send_password_reset_token'
app/controllers/players_controller.rb:209:in `password_forgot'

但我没有收到任何电子邮件,这里没有任何方法encoding。那么是什么导致了这个错误

1 个答案:

答案 0 :(得分:0)

让你像这样设置mailer.rb

  

ActionMailer :: Base.smtp_settings = {
   :address =&gt; “smtp.gmail.com”,
   :port =&gt; 587,
   :domain =&gt; “your.domain.com”,
   :user_name =&gt; ENV [ 'MY_GMAIL_USER_NAME'],
   :password =&gt; ENV ['MY_GMAIL_PASSWORD'],
   :authentication =&gt; “普通”,
   :enable_starttls_auto =&gt;是的}