LoginSignup
6
2

More than 3 years have passed since last update.

本番環境でお問い合わせメールが送れない

Last updated at Posted at 2021-04-07

はじめに

ruby 2.7.2
Rails 6.1.1

お問い合わせのメールが送信できない

ローカルでは問題なく送信できていたお問い合わせが、本番環境で機能しなくなった。

スクリーンショット 2021-04-07 23.02.11.png
送信を押すと↓
awserroer.png
エラー画面が表示される。なぜだ。。。

サーバー側のログを確認

ひとまずログを確認することで、以下のようなエラーメッセージが出ていた。

log/production.log
Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):

エラー内容としては、ActionMailerの配信方法が正しく設定されていない?
ローカルホストポート25でSMTPサーバーを使用して電子メールを送信するように構成されていることを示しています。(引用)
構成が使用されていないことを示している。

【解決策】config/environments/production.rbにもMailerの設定を記述する

サーバーへのデプロイをproduction環境で稼働させている場合は、config/environments/production.rbにMailerの設定を記述する必要がある。
(config/environments/development.rbでしか設定をしていなかった...。)

config/environments/development.rb
config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    domain: 'gmail.com',
    port: 587,
    user_name: <ユーザー名>,
    password: <パスワード>,
    authentication: 'plain',
    enable_starttls_auto: true
  }

上記に記述しているMailerの設定を、config/environments/production.rbにも記述することで、本番環境でも動くことを確認できた。

参考資料

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2