UserMailer with Devise

拈花ヽ惹草 提交于 2020-01-02 23:09:34

问题


am using my custom mailer UserMailer to send emails. It works perfect in development, but in production i get error 500, that is when am trying to retrieve my password on forgetting.

here is what is in the production logs in the production mode

  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'

my mailer configuration. 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 file

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  

    }

your assistance will be highly appreciated


回答1:


Okay from the looks of it your mailer looks wrong. Especially the set up. By default you can create a mailer like this:

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

What I did notice in your example is that your doing:

 class UserMailer < Devise::Mailer

Your inheriting from Devise's Mailer when in actual fact you shouldn't have do any of this! You may also want to check your config/initalizers/devise.rb and set theconfig.mailer_sender=example@gmail.com` if you haven't. So what I suggest you doing is make your mailer look like the following:

 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

Also another thing... I noticed that your default url is: config.action_mailer.default_url_options = { :host => 'https://xxxxxxxxxx.com' } there is no need for https so it should really look like config.action_mailer.default_url_options = { :host => 'xxxxxxxxxx.com' }. Because when you try to fire anything what will happen is that it will be doing https://https://https://xxxxxxxxxx.com. This is an easy mistake for people to make.

And I also believe the cause of this may be due to the fact you have not set the class that is responsible for sending your e-mails.

Other possible solution that may fix your problem

Notice that in config/intializers/devise.rb that there is the following line which is commented out:

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

Uncomment this and set this to your class you are using which in your example so that it would be

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

Also in app/mailers/user_mailer.rb you should have:

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

May also want to generate your own views:

rails generate devise:views

also move the email templates from app/views/devise/mailer/ to app/views/user_mailer/

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




回答2:


Do everthing @david has said. except change this for the 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


来源:https://stackoverflow.com/questions/16281254/usermailer-with-devise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!