質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
SendGrid

SendGridは、米SendGrid社のクラウド型メール配信サービス。アカウントを作成するだけですぐに利用することが可能です。さらに到達率向上のための送信ドメイン認証対応や、柔軟性のあるスケーラビリティなど多くの機能を有します。

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Q&A

解決済

1回答

1127閲覧

本番環境でメールを受信できない【rails】【SendGrid】

kanieksuke

総合スコア33

SendGrid

SendGridは、米SendGrid社のクラウド型メール配信サービス。アカウントを作成するだけですぐに利用することが可能です。さらに到達率向上のための送信ドメイン認証対応や、柔軟性のあるスケーラビリティなど多くの機能を有します。

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

0グッド

0クリップ

投稿2022/03/27 12:58

実行環境
ruby 2.6.5
rails 6.0.4.6

deviseの機能を用いてパスワード再発行機能を実装中です。

起こっている問題

サインアップ済ユーザーのメールアドレスをフォームに入力すれば、パスワード変更画面に遷移するためのメールが送信される機能を、こちらの記事を参考にして実装。
ローカル環境では問題なく送信されますが、SendGridを用いた本番環境ではメールを受け取る事ができません。
いくつか試しましたが煮詰まってきた感があるので、皆様のお知恵をお借りしたいです。

config/environments/production.rb

ruby

1Rails.application.configure do 2 # 10000字超えてしまったので関係なさそうなところは省略しています 3 config.cache_classes = true 4 5 config.eager_load = true 6 7 config.consider_all_requests_local = false 8 config.action_controller.perform_caching = true 9 10 config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? 11 12 config.assets.compile = true 13 14 config.active_storage.service = :local 15 16 config.log_level = :debug 17 18 config.log_tags = [ :request_id ] 19 20 config.action_mailer.perform_caching = false 21 config.action_mailer.default_url_options = {host: 'https://www.アプリ名.herokuapp.com'} 22 # Ignore bad email addresses and do not raise email delivery errors. 23 # Set this to true and configure the email server for immediate delivery to raise delivery errors. 24 config.action_mailer.raise_delivery_errors = false 25 config.action_mailer.delivery_method = :smtp 26 config.action_mailer.smtp_settings = { 27 :user_name => ENV["SENDGRID_USERNAME"], 28 :password => ENV["SENDGRID_PASSWORD"], 29 :domain => "heroku.com", 30 :port => 587, 31 :authentication => :plain, 32 :enable_starttls_auto => true, 33 :address => "smtp.sendgrid.net" 34 } 35 36 # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 37 # the I18n.default_locale when a translation cannot be found). 38 config.i18n.fallbacks = true 39 40 # Send deprecation notices to registered listeners. 41 config.active_support.deprecation = :notify 42 43 # Use default logging formatter so that PID and timestamp are not suppressed. 44 config.log_formatter = ::Logger::Formatter.new 45 46 if ENV["RAILS_LOG_TO_STDOUT"].present? 47 logger = ActiveSupport::Logger.new(STDOUT) 48 logger.formatter = config.log_formatter 49 config.logger = ActiveSupport::TaggedLogging.new(logger) 50 end 51 52 config.active_record.dump_schema_after_migration = false 53 54ActiveRecord::Middleware::DatabaseSelector::Resolver::Session 55end 56

config/initializer/devise/rb

ruby

1Devise.setup do |config| 2 # ==> Mailer Configuration 3 # Configure the e-mail address which will be shown in Devise::Mailer, 4 # note that it will be overwritten if you use your own mailer class 5 # with default "from" parameter. 6 config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' 7 8 # Configure the class responsible to send e-mails. 9 # config.mailer = 'Devise::Mailer' 10 11 # Configure the parent class responsible to send e-mails. 12 # config.parent_mailer = 'ActionMailer::Base' 13 14 require 'devise/orm/active_record' 15 16 config.case_insensitive_keys = [:email] 17 18 config.strip_whitespace_keys = [:email] 19 20 config.skip_session_storage = [:http_auth] 21 22 config.stretches = Rails.env.test? ? 1 : 12 23 24 # Send a notification to the original email when the user's email is changed. 25 # config.send_email_changed_notification = false 26 27 # Send a notification email when the user's password is changed. 28 # config.send_password_change_notification = false 29 30 config.reconfirmable = true 31 32 config.password_length = 6..128 33 34 config.email_regexp = /\A[^@\s]+@[^@\s]+\z/ 35 36 config.reset_password_keys = [:email] 37 38 config.reset_password_within = 6.hours 39 40 config.sign_in_after_reset_password = true 41 42 config.sign_out_via = :delete 43 44end

試してみたこと①

logを確認しました。
私が見る限りでは、正常に送信されているように見受けられます。
実際の挙動も、エラーは起こらず正しい画面に遷移します。
ただし送信先のメールボックスには受信できていませんでした。

terminal

12022-03-27T10:59:15.254718+00:00 app[web.1]: D, [2022-03-27T10:59:15.254686 #4] DEBUG -- : [52033afe-b1b1-4c51-b9d3-ca5da5c4c4bb] Date: Sun, 27 Mar 2022 10:59:15 +0000 22022-03-27T10:59:15.254719+00:00 app[web.1]: From: please-change-me-at-config-initializers-devise@example.com 32022-03-27T10:59:15.254719+00:00 app[web.1]: Reply-To: please-change-me-at-config-initializers-devise@example.com 42022-03-27T10:59:15.254720+00:00 app[web.1]: To: hoge@hoge 52022-03-27T10:59:15.254721+00:00 app[web.1]: Message-ID: <6240438330d9e_42ab2964a9318449ee@829be847-198e-435d-a889-9c926c18a088.mail> 62022-03-27T10:59:15.254721+00:00 app[web.1]: Subject: Reset password instructions 72022-03-27T10:59:15.254721+00:00 app[web.1]: Mime-Version: 1.0 82022-03-27T10:59:15.254722+00:00 app[web.1]: Content-Type: text/html; 92022-03-27T10:59:15.254722+00:00 app[web.1]: charset=UTF-8 102022-03-27T10:59:15.254723+00:00 app[web.1]: Content-Transfer-Encoding: base64 112022-03-27T10:59:15.254723+00:00 app[web.1]: 122022-03-27T10:59:15.254723+00:00 app[web.1]: 13 14〜 省略 〜 15 16[52033afe-b1b1-4c51-b9d3-ca5da5c4c4bb] Redirected to https://アプリ名.herokuapp.com/users/sign_in 172022-03-27T10:59:15.256883+00:00 app[web.1]: I, [2022-03-27T10:59:15.256850 #4] INFO -- : [52033afe-b1b1-4c51-b9d3-ca5da5c4c4bb] Completed 302 Found in 186ms (ActiveRecord: 18.9ms | Allocations: 7951) 182022-03-27T10:59:15.257753+00:00 heroku[router]: at=info method=POST path="/users/password" host=hoge.herokuapp.com request_id=52033afe-b1b1-4c51-b9d3-ca5da5c4c4bb fwd="60.138.7.109" dyno=web.1 connect=0ms service=209ms status=302 bytes=1334 protocol=https 192022-03-27T10:59:15.429519+00:00 app[web.1]: I, [2022-03-27T10:59:15.429457 #4] INFO -- : [a5eb24d1-241d-4c2c-affd-a9c45bb38172] Started GET "/users/sign_in" for 60.138.7.109 at 2022-03-27 10:59:15 +0000 202022-03-27T10:59:15.430249+00:00 app[web.1]: I, [2022-03-27T10:59:15.430200 #4] INFO -- : [a5eb24d1-241d-4c2c-affd-a9c45bb38172] Processing by Devise::SessionsController#new as HTML 212022-03-27T10:59:15.431515+00:00 app[web.1]: I, [2022-03-27T10:59:15.431471 #4] INFO -- : [a5eb24d1-241d-4c2c-affd-a9c45bb38172] Rendering devise/sessions/new.html.erb within layouts/application 222022-03-27T10:59:15.433360+00:00 app[web.1]: I, [2022-03-27T10:59:15.433317 #4] INFO -- : [a5eb24d1-241d-4c2c-affd-a9c45bb38172] Rendered devise/shared/_links.html.erb (Duration: 0.2ms | Allocations: 134) 232022-03-27T10:59:15.433408+00:00 app[web.1]: I, [2022-03-27T10:59:15.433384 #4] INFO -- : [a5eb24d1-241d-4c2c-affd-a9c45bb38172] Rendered devise/sessions/new.html.erb within layouts/application (Duration: 1.8ms | Allocations: 1232) 242022-03-27T10:59:15.433918+00:00 app[web.1]: I, [2022-03-27T10:59:15.433882 #4] INFO -- : [a5eb24d1-241d-4c2c-affd-a9c45bb38172] Completed 200 OK in 4ms (Views: 2.6ms | ActiveRecord: 0.0ms | Allocations: 2178) 252022-03-27T10:59:15.434682+00:00 heroku[router]: at=info method=GET path="/users/sign_in" host=アプリ名.herokuapp.com request_id=a5eb24d1-241d-4c2c-affd-a9c45bb38172 fwd="60.138.7.109" dyno=web.1 connect=0ms service=6ms status=200 bytes=3209 protocol=https 262022-03-27T11:33:29.968366+00:00 heroku[web.1]: Idling 272022-03-27T11:33:29.971010+00:00 heroku[web.1]: State changed from up to down 282022-03-27T11:33:30.720213+00:00 heroku[web.1]: Stopping all processes with SIGTERM 292022-03-27T11:33:30.764200+00:00 app[web.1]: - Gracefully stopping, waiting for requests to finish

試してみたこと②

ローカル環境では正常にGoogleメールが送信できている、という事で、
SendGridに問題があるのではと思い問い合わせてみたら、アカウントを凍結されていたようです。
2,3メールをやりとりしてアカウントが使えるようになったので、

terminal

1git push heroku master 2heroku run rails db:migrate

で再度メールを送信しましたが、受信できませんでした。

見づらいファイルばかり貼り付けて面目ないです。
お目汚ししてしまい恐縮ですが、回答いただければと思います。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

根本的な解決には至りませんでしたが、こちらの記事を参考にしてMailgunに乗り換えてみたところ、晴れてメールが届きました!

ruby

1 config.action_mailer.perform_caching = false 2 config.action_mailer.default_url_options = {host: 'https://www.mammon-quest.herokuapp.com'} 3 # Ignore bad email addresses and do not raise email delivery errors. 4 # Set this to true and configure the email server for immediate delivery to raise delivery errors. 5 config.action_mailer.raise_delivery_errors = false 6 config.action_mailer.delivery_method = :smtp 7 config.action_mailer.smtp_settings = { 8 :port => ENV['MAILGUN_SMTP_PORT'], 9 :address => ENV['MAILGUN_SMTP_SERVER'], 10 :user_name => ENV['MAILGUN_SMTP_LOGIN'], 11 :password => ENV['MAILGUN_SMTP_PASSWORD'], 12 :domain => 'heroku.com', 13 :authentication => :plain, 14 }

投稿2022/03/29 10:55

kanieksuke

総合スコア33

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問