UserMailer与Devise [英] UserMailer with Devise

查看:180
本文介绍了UserMailer与Devise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

我的邮件配置user_mailer.rb

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文件

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  

    }

您的帮助将高度赞赏

推荐答案

从你的邮件看起来好像是错误的。特别设置。默认情况下,您可以创建如下邮件:

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

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

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

另外还有一件事我注意到您的默认网址为: 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 。这是人们容易犯的错误。

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.

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

请注意,在配置/intializers/devise.rb 有以下行被注释掉:

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

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

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

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

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


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

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

这篇关于UserMailer与Devise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆