Rails的行动梅勒与设计
问题描述:
我明白这已被问及之前,我一直在跟随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中创建我的创建操作,而是在设计注册控制器中进行注册。
如果您需要更多信息,请让我知道。
从我过去使用的ActionMailer中,我不相信电子邮件会通过开发发送。看看你的日志,你应该看到那里的电子邮件。 – hashrocket
我认为它应该去。我们需要日志来了解事情是如何被触发的。不过,我会推荐使用Mailhog或mailcatcher(最新版本是宝石,并且都可在Docker中使用,因此它们不会混淆您的环境)。 –
当你实际将开发环境的url定义为config.action_mailer时,在'@login_url =“https://dogseeker.herokuapp.com/admins/sign_in”'上方的代码中引起了我的注意。 default_url_options = {host:'localhost',port:3000} '。所以我不明白,如果你的问题是针对开发或生产(测试或分期)环境。 –