Search code examples
ruby-on-rails-3deviseruby-on-rails-3.2actionmailermailer

UserMailer with Devise


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"=>"[email protected]"}, "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 => "[email protected]"  

 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               => '[email protected]',
      :user_name            => 'yyyyyy',
      :password             => 'zzzzzzzz',
      :authentication       => 'plain',
      :openssl_verify_mode => 'none',
     :enable_starttls_auto => true  

    }

your assistance will be highly appreciated


Solution

  • 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 the[email protected]` if you haven't. So what I suggest you doing is make your mailer look like the following:

     class UserMailer < ActionMailer::Base 
    
      default :from => "[email protected]"  
    
     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: "[email protected]"
    
      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/