UserMailer with Devise

假装没事ソ 提交于 2019-12-08 05:31:25

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/

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