无法使用SENDGRID发送激活电子邮件Hartl Rails 5 11.4

时间:2017-07-28 23:49:40

标签: ruby-on-rails ruby heroku sendgrid cloud9-ide

我参考Michael Hartl的Rails 5书中的第11.4章。一切都是绿色的。但是,我在发送激活电子邮件时遇到问题。 heroku和Cloud9之间可能出现的故障?请记住,如果我复制并粘贴电子邮件,则激活确实有效。

HEROKU日志:

INFO -- : [14c19bd6-0d0c-4f55-bc38-fc7330189513] Completed 500 Internal 
Server Error in 710ms (ActiveRecord: 10.9ms)
2017-07-28T23:22:44.797443+00:00 app[web.1]: F, [2017-07-28T23:22:44.797379     
#6] FATAL -- : [14c19bd6-0d0c-4f55-bc38-fc7330189513]   
2017-07-28T23:22:44.797511+00:00 app[web.1]: F, [2017-07-28T23:22:44.797451         
#6] FATAL -- : [14c19bd6-0d0c-4f55-bc38-fc7330189513] Errno::ECONNREFUSED         
(Connection refused - connect(2) for "localhost" port 587):
2017-07-28T23:22:44.797572+00:00 app[web.1]: F, [2017-07-28T23:22:44.797514 
#6] FATAL -- : [14c19bd6-0d0c-4f55-bc38-fc7330189513]   
2017-07-28T23:22:44.797642+00:00 app[web.1]: F, [2017-07-28T23:22:44.797588 
#6] FATAL -- : [14c19bd6-0d0c-4f55-bc38-fc7330189513] 
app/models/user.rb:41:in `send_activation_email'
2017-07-28T23:22:44.797644+00:00 app[web.1]: [14c19bd6-0d0c-4f55-bc38-
fc7330189513] app/controllers/users_controller.rb:22:in `create'`enter code 
here`

Production.rb:

config.force_ssl = true

# Use the lowest log level to ensure availability of diagnostic information
# when problems arise. \
config.log_level = :debug

# Prepend all log lines with the following tags.
config.log_tags = [ :request_id ]

# Use a different cache store in production.
# config.cache_store = :mem_cache_store

# Use a real queuing backend for Active Job (and separate queues per 
environment)
# config.active_job.queue_adapter     = :resque
# config.active_job.queue_name_prefix = "sample_apps_#{Rails.env}"
config.action_mailer.perform_caching = false

# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to 
raise delivery errors.
# config.action_mailer.raise_delivery_errors = false

# Enable locale fallbacks for I18n (makes lookups for any locale fall back 
to
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true

# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify

# Use default logging formatter so that PID and timestamp are not 
suppressed.
config.log_formatter = ::Logger::Formatter.new

# Use a different logger for distributed setups.
# require 'syslog/logger'
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

if ENV["RAILS_LOG_TO_STDOUT"].present?
logger           = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'infinite-plains-13710.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:addresses      => 'smtp.sendgrid.net',
:port           => '587',
:authentication => :plain,
:user_name      => ENV['SENDGRID_USERNAME'],
:password       => ENV['SENDGRID_PASSWORD'],
:domain         => 'heroku.com',
:enable_starttls_auto => true
}
end

users_controller.rb文件的一部分:

class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user,   only: [:edit, :update]
before_action :admin_user,     only: :destroy

def index
@users = User.where(activated: true).paginate(page: params[:page])
end

def show
@user = User.find(params[:id])
redirect_to root_url and return unless @user.activated?
end

def new
@user = User.new
end

def create
@user = User.new(user_params)
if @user.save
  @user.send_activation_email
  flash[:info] = "Please check your email to account your account"
  redirect_to root_url
else
  render 'new'
end
end

development.rb:

Rails.application.configure do
# Settings specified here will take precedence over those in 
config/application.rb.

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for 
development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

# Do not eager load code on boot.
config.eager_load = false

# Show full error reports.
config.consider_all_requests_local = true

# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true

config.cache_store = :memory_store
config.public_file_server.headers = {
  'Cache-Control' => 'public, max-age=172800'
}
else
config.action_controller.perform_caching = false

config.cache_store = :null_store
end

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :test
host = 'rails-tutorial-dball1126.c9users.io' # Don't use this literally;  
user your local dev host instead
config.action_mailer.default_url_options = { host: host, protocol: 'https' }

config.action_mailer.perform_caching = false

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load

# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true

# Suppress logger output for asset requests.
config.assets.quiet = true

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

# Use an evented file watcher to asynchronously detect changes in source 
code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

1 个答案:

答案 0 :(得分:1)

您的真实错误在日志中:

(Connection refused - connect(2) for "localhost" port 587):

要在Heroku上使用SMTP,我需要为Heroku提供一个“支持服务”插件,根据他们的页面:https://devcenter.heroku.com/articles/smtp