UserMailer with Devise

时间:2013-04-29 14:30:21

标签: ruby-on-rails-3 devise ruby-on-rails-3.2 actionmailer mailer

正在使用我的自定义邮件程序UserMailer发送电子邮件。它在开发中工作得很完美,但在生产中我得到错误500,就是在我忘记时检索密码的时候。

这是生产模式中生产日志中的内容

  Processing by Devise::PasswordsController#create as HTML
  Parameters: {"utf8"=>"✓","authenticity_token"=>"xxxxxxxxxxxxxxx", "user"=>{"email"=>"sam@gmail.com"}, "commit"=>"Send me r$
 Completed 500 Internal Server Error in 2ms

 NameError (uninitialized constant Devise::UserMailer):
  activesupport (3.2.8) lib/active_support/inflector/methods.rb:230:in `block in  constantize'
activesupport (3.2.8) lib/active_support/inflector/methods.rb:229:in `each'
activesupport (3.2.8) lib/active_support/inflector/methods.rb:229:in `constantize'
devise (2.1.2) lib/devise.rb:256:in `get'
devise (2.1.2) lib/devise.rb:279:in `mailer'

我的邮件配置。 user_mailer.rb

 class UserMailer < Devise::Mailer 

  default :from => "info@xxxxxxxx.com"  

 def signup_confirmation(user)
   @user = user
   mail :to => user.email, :subject=> "Thank you for signing with us"
end

# send password reset instructions

def reset_password_instructions(user)
  @resource = user
  mail(:to => @resource.email, :subject => "Reset password instructions", :tag => 'password-reset', :content_type => "text/html") do |format|
   format.html { render "devise/mailer/reset_password_instructions" }
  end
end  
end

production.rb文件

config.action_mailer.default_url_options = { :host => 'https://xxxxxxxxxx.com'  }

config.action_mailer.raise_delivery_errors = false

 config.action_mailer.perform_deliveries = true


   config.action_mailer.delivery_method = :smtp
   config.action_mailer.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :domain               => 'info@xxxxxxxx.com',
      :user_name            => 'yyyyyy',
      :password             => 'zzzzzzzz',
      :authentication       => 'plain',
      :openssl_verify_mode => 'none',
     :enable_starttls_auto => true  

    }

您的帮助将受到高度赞赏

2 个答案:

答案 0 :(得分:6)

好的,从它看起来你的邮件看起来不对劲。特别是设置。默认情况下,您可以创建这样的邮件:

class UserMailer < ActionMailer::Base
  default :from => DEFAULT_FROM
  def registration_confirmation(user)
    @user = user
    @url = "http://localhost:3000/login"
    mail(:to => user.email, :subject => "Registered")

  end
end

我在你的例子中注意到的是你的行为:

 class UserMailer < Devise::Mailer

你继承自Devise的Mailer实际上你不应该做任何这个!如果还没有,您可能还想查看config/initalizers/devise.rb and set the config.mailer_sender = example @ gmail.com`。所以我建议你做的就是让你的邮件看起来如下:

 class UserMailer < ActionMailer::Base 

  default :from => "info@xxxxxxxx.com"  

 def signup_confirmation(user)
   @user = user
   mail :to => user.email, :subject=> "Thank you for signing with us"
end

另外一件事......我注意到您的默认网址是:config.action_mailer.default_url_options = { :host => 'https://xxxxxxxxxx.com' }不需要https所以它应该看起来像config.action_mailer.default_url_options = { :host => 'xxxxxxxxxx.com' }。因为当你试图发射任何东西时会发生的事情是https://https://https://xxxxxxxxxx.com。这是人们容易犯的错误。

我也相信原因可能是因为您没有设置负责发送电子邮件的班级。

可能解决问题的其他可能解决方案

请注意,在config/intializers/devise.rb中,有以下注释掉的行:

  # Configure the class responsible to send e-mails.
  # config.mailer = "Devise::Mailer"

取消注释并将其设置为您正在使用的类,以便它可以

 config.mailer = "UserMailer" # UserMailer is your mailer class

同样在app / mailers / user_mailer.rb中你应该:

class UserMailer < ActionMailer::Base
  include Devise::Mailers::Helpers

  default from: "default@mydomain.com"

  def confirmation_instructions(record)
    devise_mail(record, :confirmation_instructions)
  end

  def reset_password_instructions(record)
    devise_mail(record, :reset_password_instructions)
  end

  def unlock_instructions(record)
    devise_mail(record, :unlock_instructions)
  end

  # you can then put any of your own methods here
end

也可能想要生成自己的观点:

rails generate devise:views

还将电子邮件模板从app / views / devise / mailer /移动到app / views / user_mailer /

  

mv app / views / devise / mailer / * app / views / user_mailer /

答案 1 :(得分:2)

做任何事都@david说过。除了为设备更改此设置&gt; 3.2.4

class UserMailer < ActionMailer::Base
include Devise::Mailers::Helpers

def confirmation_instructions(record, token, opts={})
  @token = token
  devise_mail(record, :confirmation_instructions, opts)
end

def reset_password_instructions(record, token, opts={})
  @token = token
  devise_mail(record, :reset_password_instructions, opts)
end

def unlock_instructions(record, token, opts={})
  @token = token
  devise_mail(record, :unlock_instructions, opts)
end