0

我正在尝试在 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。那么是什么导致了这个错误

4

1 回答 1

0

你有没有像这样设置你的 mailer.rb

ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "your.domain.com",
:user_name => ENV['MY_GMAIL_USER_NAME'],
:密码 => ENV['MY_GMAIL_PASSWORD'],
:authentication => "plain",
:enable_starttls_auto => true }

于 2013-10-28T12:19:53.647 回答