Rails 带有设计的 Action Mailer

Rails Action Mailer with Devise

我知道之前有人问过这个问题,我一直在关注 Rails ActionMailer 指南,并浏览了一些关于 Whosebug 的相关问题。我是 运行 本地主机并尝试从那里发送电子邮件。根据 rails 指南,我遵循了每一步,并仔细检查了指南中所写的所有内容。我还阅读了此处发现的一些问题,但我的电子邮件仍未从本地主机发送。另外,我的服务器没有收到任何错误。

config/environments/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
}

mailers/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

app/admin/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" =  LIMIT   [["email", "stenglinephoto@gmail.com"], ["LIMIT", 1]]
  SQL (0.6ms)  INSERT INTO "admins" ("email", "encrypted_password", "created_at", "updated_at") VALUES (, , , ) 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" = , "current_sign_in_at" = , "last_sign_in_at" = , "current_sign_in_ip" = , "last_sign_in_ip" = , "updated_at" =  WHERE "admins"."id" =   [["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" =  ORDER BY "admins"."id" ASC LIMIT   [["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)

另一个更新

我有另一个邮件程序,它在创建狗时发送,并且在本地主机和上述所有配置下都可以正常工作。我的猜测是,创建管理员帐户时邮件程序不会发送的原因是设计注册控制器覆盖了我的控制器。换句话说,它没有在 admin_controller 中触发我的创建操作,而是在设计注册控制器中触发注册。

如果您需要更多信息,请告诉我。

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