1

我正在使用我的自定义邮件程序 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

生产.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  

    }

您的帮助将不胜感激

4

2 回答 2

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 theconfig.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/

于 2013-04-29T18:53:36.690 回答
2

做@david 所说的一切。除了为 Devise > 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
于 2014-06-07T11:44:46.590 回答