2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Rails】Rails6 + docker メール設定エラー 「Errno::EADDRNOTAVAIL (Cannot assign requested address - connect(2) for "localhost" port 1025)」

Last updated at Posted at 2022-07-09

概要

Rails6とdocker環境でメール設定をした際に発生したエラーの備忘録です。

環境

  • ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]
  • Rails 6.1.6
docker-compose.yml
# 中略
  mailcatcher:
    image: schickling/mailcatcher
    ports:
      - 1080:1080     
      - 1025:1025

エラー発生時のメール設定

development.rb
 config.action_mailer.delivery_method = :smtp
 config.action_mailer.smtp_settings = { address: 'localhost', port: 1025 }
 config.action_mailer.raise_delivery_errors = true

エラー内容

  • Railsサーバ側 (localhost:3000) の結果

Errno::EADDRNOTAVAIL
Cannot assign requested address - connect(2) for "localhost" port 1025

railstask2.png

  • メールサーバ側 (localhost:1080) の結果
    railstask3.png

コンテナのログを確認してみると、メール送信まではできているよう。。
しかし、Internal Server Errorにより、メールはサーバまで到達できておらず。

web_1          | TaskMailer#creation_email: processed outbound mail in 51.4ms
web_1          | Delivered mail 62c96fb5de0b4_1530c5853b@c88a1a8f7fc3.mail (20.5ms)
web_1          | Date: Sat, 09 Jul 2022 12:08:21 +0000
web_1          | From: test@example.com
web_1          | To: user@example.com
web_1          | Message-ID: <62c96fb5de0b4_1530c5853b@c88a1a8f7fc3.mail>
〜〜〜中略〜〜〜
web_1          | Completed 500 Internal Server Error in 221ms (ActiveRecord: 8.1ms | Allocations: 56411) 
web_1          | Errno::EADDRNOTAVAIL (Cannot assign requested address - connect(2) for "localhost" port 1025):

原因

  • smtpメールサーバのアドレス設定が間違っていた。
  • Railsのwebサーバコンテナもローカルホストなので、メールサーバのサービス名(コンテナ名)を指定する必要があった。

解決方法

  • メール設定を下記に修正。
 config.action_mailer.delivery_method = :smtp
+ config.action_mailer.smtp_settings = { address: 'mailcatcher', port: 1025 }
 config.action_mailer.raise_delivery_errors = true

再度メールを送信してみると、MailCatcherで受信確認できた。
railstask4.png

web_1          | TaskMailer#creation_email: processed outbound mail in 58.5ms
mailcatcher_1  | ==> SMTP: Received message from '<test@example.com>' (1240 bytes)

参考

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?