前提
ruby on rails でアプリを製作し、Herokuでリリースして本番環境で運用できるか確認しているところです。
ユーザー登録の際にDeviseを用いた認証を設定しているのですが、メールが送信されません。
SendGridへの登録、HerokuのAddonsにSendGridの追加、HerokuのConfig VarsにSENDGRID_APIKEY、SENDGRID_PASSWORD、SENDGRID_USERNAMEを設定しています。
Gmailの2段階認証も行っています。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- メールが届くようにする
発生している問題・エラーメッセージ
heroku logs -tを確認すると以下のエラーが出ていました。
stmpの設定が間違っているのではないかと考えているのですが、どこが間違っているのか良く分かりませんでした。
INFO -- : [42e2f8ce-1c3a-4b87-86bb-6fb25d57f2ec] Delivered mail INFO -- : [42e2f8ce-1c3a-4b87-86bb-6fb25d57f2ec] Completed 500 Internal Server Error in 44ms (ActiveRecord: 18.4ms | Allocations: 6214) FATAL -- : [42e2f8ce-1c3a-4b87-86bb-6fb25d57f2ec] ArgumentError (SMTP-AUTH requested but missing secret phrase):```
該当のソースコード
production.rbの設定
environments/production.rb
require "active_support/core_ext/integer/time" Rails.application.configure do config.cache_classes = true config.eager_load = true config.consider_all_requests_local = false config.action_controller.perform_caching = true config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? config.assets.compile = true config.active_storage.service = :local config.log_tags = [ :request_id ] /////////////////////////////////////////////ここからMailerの設定 config.action_mailer.perform_caching = false #default url config.action_mailer.default_url_options = {host: "○○○.herokuapp.com"} #mail setting config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp ActionMailer::Base.smtp_settings = { :user_name => 'apikey', :password => ENV['SENDGRID_API_KEY'], :domain => '○○○.herokuapp.com', :address => 'smtp.sendgrid.net', :port => 587, :authentication => :plain, :enable_starttls_auto => true } //////////////////////////////////////////////////////////////////////////////////////////////// config.i18n.fallbacks = true config.active_support.deprecation = :notify config.active_support.disallowed_deprecation = :log config.active_support.disallowed_deprecation_warnings = [] config.log_formatter = ::Logger::Formatter.new if ENV["RAILS_LOG_TO_STDOUT"].present? logger = ActiveSupport::Logger.new(STDOUT) logger.formatter = config.log_formatter config.logger = ActiveSupport::TaggedLogging.new(logger) end config.active_record.dump_schema_after_migration = false
application_mailer.rbの設定
application_mailer.rb
class ApplicationMailer < ActionMailer::Base default from: "△△△@gmail.com" layout 'mailer' end
HerokuのConfig Varsには以下を追加しました
- SENDGRID_APIKEY
- SENDGRID_PASSWORD
- SENDGRID_USERNAME
足りていない情報があればコメントいただけますと幸いです。
よろしくお願いします。
参考
Ruby on Railsでメール送信
Rails6 アプリからメールを送信する デプロイ環境編 - Qiita
Heroku にデプロイした Rails アプリから SENDGRID を使ってメール送信する
まだ回答がついていません
会員登録して回答してみよう