2017-08-06 98 views
0

我明白這已被問及之前,我一直在跟隨Rails的ActionMailer指南,以及通過幾個相關的問題stackoverflow。我正在運行本地主機並嘗試從那裏發送電子郵件。從導軌,我跟着每一步,並雙重檢查一切都寫在導遊。我也讀過這裏發現的幾個問題,但我的電子郵件仍然沒有從本地主機發送。另外,我在我的服務器中沒有收到任何錯誤。Rails的行動梅勒與設計

配置/環境/ development.rb

EDIT

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: '[email protected]'} 

    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 

UPDATE

登錄時,新的管理報

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"=>"[email protected]", "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", "[email protected]"], ["LIMIT", 1]] 
    SQL (0.6ms) INSERT INTO "admins" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "[email protected]"], ["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

從我過去使用的ActionMailer中,我不相信電子郵件會通過開發發送。看看你的日誌,你應該看到那裏的電子郵件。 – hashrocket

+1

我認爲它應該去。我們需要日誌來了解事情是如何被觸發的。不過,我會推薦使用Mailhog或mailcatcher(最新版本是寶石,並且都可在Docker中使用,因此它們不會混淆您的環境)。 –

+0

當你實際將開發環境的url定義爲config.action_mailer時,在'@login_url =「https://dogseeker.herokuapp.com/admins/sign_in」'上方的代碼中引起了我的注意。 default_url_options = {host:'localhost',port:3000} '。所以我不明白,如果你的問題是針對開發或生產(測試或分期)環境。 –

回答

0

不得不按照從Devise Wiki指導,並能夠使其工作時管理員註冊。