Rails Action Mailer with Devise

时间:2017-08-06 02:12:40

标签: ruby-on-rails devise actionmailer

我知道之前已经问过这个问题,我一直在关注Rails ActionMailer指南,并在stackoverflow上查看一些相关问题。我正在运行本地主机并尝试从那里发送电子邮件。从导轨指南,我按照每一步,双重检查所有内容都写在指南中。我也读过这里发现的一些问题,但我的电子邮件仍然没有从localhost发送。另外,我的服务器中没有出现任何错误。

配置/环境/ development.rb

修改

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

  #Mailers
  config.action_mailer.delivery_method = :sendmail
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_options = {from: 'dogseeker7@gmail.com'}

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'localhost:3000',
    user_name:            ENV["ADMIN_EMAIL"],                                                                                                                   
    password:             ENV["DOG_SEEKER_GMAIL"],
    authentication:       'plain',
    enable_starttls_auto: true
}

寄件人/ admin_mailer.rb

Class AdminMailer < Devise::Mailer
helper :application
include Devise::Controllers::UrlHelpers    

修改

def welcome_email(admin)                                                                                                                                
    @admin = admin
    @login_url = "localhost:3000/admins/sign_in"
    mail(to: @admin.email, subject: "Welcome to Dog Seeker!")
end

应用程序/管理/ admin_controllers.rb

def create
   @admin = Admin.new(params[:admin])

   respond_to do |format|
     if @admin.save
       AdminMailer.welcome_email(@admin).deliver_now
       format.html { redirect_to(@admin, notice: 'Admin was successfully created.') }
       format.json { render json: @admin, status: :created, location: @admin }
     else
       format.html { redirect_to new_admin_registration_path }
       format.json { render json: @admin.errors, status: :unprocessable_entity }
     end
   end
 end

更新

新管理员注册时记录

Started POST "/admins" for 127.0.0.1 at 2017-08-05 22:09:15 -0700
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"<token>==", "admin"=>{"email"=>"stenglinephoto@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.2ms)  BEGIN
  Admin Exists (0.6ms)  SELECT  1 AS one FROM "admins" WHERE "admins"."email" = $1 LIMIT $2  [["email", "stenglinephoto@gmail.com"], ["LIMIT", 1]]
  SQL (0.6ms)  INSERT INTO "admins" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["email", "stenglinephoto@gmail.com"], ["encrypted_password", "<password>"], ["created_at", "2017-08-06 05:09:15.904920"], ["updated_at", "2017-08-06 05:09:15.904920"]]
   (2.0ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.5ms)  UPDATE "admins" SET "sign_in_count" = $1, "current_sign_in_at" = $2, "last_sign_in_at" = $3, "current_sign_in_ip" = $4, "last_sign_in_ip" = $5, "updated_at" = $6 WHERE "admins"."id" = $7  [["sign_in_count", 1], ["current_sign_in_at", "2017-08-06 05:09:15.909337"], ["last_sign_in_at", "2017-08-06 05:09:15.909337"], ["current_sign_in_ip", "127.0.0.1/32"], ["last_sign_in_ip", "127.0.0.1/32"], ["updated_at", "2017-08-06 05:09:15.909955"], ["id", 29]]
   (0.5ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 172ms (ActiveRecord: 4.5ms)


Started GET "/" for 127.0.0.1 at 2017-08-05 22:09:15 -0700
Processing by HomepagesController#index as HTML
  Rendering homepages/index.html.erb within layouts/application
  Rendered homepages/index.html.erb within layouts/application (0.5ms)
  Admin Load (0.7ms)  SELECT  "admins".* FROM "admins" WHERE "admins"."id" = $1 ORDER BY "admins"."id" ASC LIMIT $2  [["id", 29], ["LIMIT", 1]]
  Rendered shared/_header.html.erb (5.5ms)
  Rendered shared/_main.html.erb (1.0ms)
Completed 200 OK in 18ms (Views: 16.2ms | ActiveRecord: 0.7ms)

另一次更新

我有一个另外的邮件程序,在创建一个狗时发送,并且从localhost和所有上述配置工作正常。我的猜测是,当创建管理员帐户时,邮件发送者不会发送的原因是设备注册控制器覆盖了我的控制器。换句话说,它没有在admin_controller中命中我的创建动作,而是在设计注册控制器中进行注册。

如果您需要其他信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

必须遵循Devise Wiki中的指南,并且能够在管理员注册时使其正常工作。