【发布时间】:2017-06-15 00:36:17
【问题描述】:
更新: 我通过heroku的网站实际访问我的应用程序来发送电子邮件,但是当我从我的cloud9 ide中的生产服务器运行应用程序时它仍然失败。这正常吗?这是教程要我这样做的方式吗?
在 Michael Hartl 的 Ruby on Rails 教程的第 11 章中,您被要求使用您控制的电子邮件在部署模式下注册示例应用程序,并检查您的电子邮件帐户中的帐户激活电子邮件。我没有收到任何电子邮件,heroku 的 sendgrid 插件报告说没有发送电子邮件请求。但是,在 cloud 9 ide puma 服务器控制台中,它报告电子邮件以正确的格式发送到正确的地址,我可以从控制台检索激活令牌和电子邮件地址。我不确定我做错了什么。希望所有相关代码都包含在下面。请帮忙。
我实际上去了示例应用程序网站并单击了注册,然后等待电子邮件。这不是 Hartl 所说的“在生产环境中注册一个新帐户”的意思吗?
config/environment.rb:
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
#Sendgrid configurations from their website
ActionMailer::Base.smtp_settings = {
:user_name =>ENV['SENDGRID_USERNAME'],
:password =>ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
mailers/application_mailer:
class ApplicationMailer < ActionMailer::Base
default from: 'noreply@example.com'
layout 'mailer'
end
环境/production.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'gentle-lake-88943.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
users_controller.rb:
def create
@user = User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = "Please check your email to activate your account"
redirect_to root_url
else
render 'new'
end
end
user.rb:
def activate
update_columns(activated: true, activated_at: Time.zone.now)
end
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
routes.rb:
resources :account_activations, only: [:edit]
【问题讨论】:
-
您有活跃的Sendgrid 帐户吗?
-
我愿意,是的。它是我与部署教程一起使用的 heroku 应用程序的“附加组件”。当我访问它的仪表板时,它显示 0 个电子邮件请求。
-
您是否从控制台(或其他方式)将“SENDGRID_USERNAME”和“SENDGRID_PASSWORD”放入
Heroku? -
教程说SENDGRID_USERNAME和SENDGRID_PASSWORD是sendgrid插件自动设置的环境变量。我没有将它们的价值明确传达给 Heroku,因为我认为是 Heroku 设定了它们的价值。我确实将代码推送到 Heroku 并将我的数据库迁移到 Heroku,但除此之外没有。
-
您的任何配置文件中是否设置了
config.action_mailer.perform_deliveries?您还可以使用Rails.configuration.action_mailer['perform_deliveries']在 Rails 控制台中检查正在运行的应用程序中的当前值
标签: ruby-on-rails railstutorial.org