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

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

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

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

Q&A

1回答

2619閲覧

お問い合わせ機能のAction Mailer の実装をしたいのですが、エラーの原因が分からず困っています

Honda56

総合スコア0

Ruby on Rails

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

0グッド

0クリップ

投稿2020/09/14 10:31

前提・実現したいこと

お問い合わせのメッセージを送った際に、お問い合わせをした人に対して、メールを送れる様にしたいのですが、
Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
とエラーになってしまい、初めてAction Mailer機能を触るので、どこをどう修正すればいいのかわかりません。どなたかわかる方がいらっしゃれば助けて下さい。

発生している問題・エラーメッセージ

"ターミナル上" Rendering contact_message_mailer/contact_message.html.erb within layouts/mailer Rendered contact_message_mailer/contact_message.html.erb within layouts/mailer (Duration: 0.1ms | Allocations: 6) Rendering contact_message_mailer/contact_message.text.erb within layouts/mailer Rendered contact_message_mailer/contact_message.text.erb within layouts/mailer (Duration: 0.0ms | Allocations: 4) ContactMessageMailer#contact_message: processed outbound mail in 4.2ms Delivered mail 5f5f3c64d89ea_1ded5c6c1033d9@Taikis-MacBook-Air.local.mail (3.2ms) Date: Mon, 14 Sep 2020 18:48:20 +0900 From: sample@icloud.com To: contact@icloud.com Message-ID: <5f5f3c64d89ea_1ded5c6c1033d9@Taikis-MacBook-Air.local.mail> Subject: =?UTF-8?Q?=E3=81=8A=E5=95=8F=E3=81=84=E5=90=88=E3=82=8F=E3=81=9B=E5=86=85=E5=AE=B9=E3=81=AE=E7=A2=BA=E8=AA=8D?= Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="--==_mimepart_5f5f3c64d7fa5_1ded5c6c10321d"; charset=UTF-8 Content-Transfer-Encoding: 7bit ----==_mimepart_5f5f3c64d7fa5_1ded5c6c10321d Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: base64 SG9uZGHjgZXjgpPjgIHjgYrllY/jgYTlkIjjgo/jgZvjgYLjgorjgYzjgajj gYbjgZTjgZbjgYTjgb7jgZnjgIINCg0K5Lul5LiL44Gu5YaF5a6544Gr44Gm 44CB44GK5ZWP44GE5ZCI44KP44Gb44KS5om/44KK44G+44GX44Gf44CCDQrj g6Hjg7zjg6vlsYrjgYTjgabjgYTjgb7jgZnjgYvvvJ8NCg0K ----==_mimepart_5f5f3c64d7fa5_1ded5c6c10321d Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: base64 PCFET0NUWVBFIGh0bWw+DQo8aHRtbD4NCiAgPGhlYWQ+DQogICAgPG1ldGEg aHR0cC1lcXVpdj0iQ29udGVudC1UeXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7 IGNoYXJzZXQ9dXRmLTgiIC8+DQogICAgPHN0eWxlPg0KICAgICAgLyogRW1h aWwgc3R5bGVzIG5lZWQgdG8gYmUgaW5saW5lICovDQogICAgPC9zdHlsZT4N CiAgPC9oZWFkPg0KDQogIDxib2R5Pg0KICAgIDxoMj5Ib25kYeOBleOCk+OA geOBiuWVj+OBhOWQiOOCj+OBm+OBguOCiuOBjOOBqOOBhuOBlOOBluOBhOOB vuOBmeOAgjwvaDI+DQo8cD7ku6XkuIvjga7lhoXlrrnjgavjgabjgIHjgYrl lY/jgYTlkIjjgo/jgZvjgpLmib/jgorjgb7jgZfjgZ/jgII8YnI+44Oh44O8 44Or5bGK44GE44Gm44GE44G+44GZ44GL77yfPC9wPg0KDQogIDwvYm9keT4N CjwvaHRtbD4NCg== ----==_mimepart_5f5f3c64d7fa5_1ded5c6c10321d-- Completed 500 Internal Server Error in 40ms (ActiveRecord: 3.5ms | Allocations: 20072) Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25): app/controllers/contacts_controller.rb:17:in `create'

該当のソースコード

"config/environments/development.rb" Rails.application.configure do config.cache_classes = false config.eager_load = false config.consider_all_requests_local = true if Rails.root.join('tmp', 'caching-dev.txt').exist? config.action_controller.perform_caching = true config.action_controller.enable_fragment_cache_logging = true config.cache_store = :memory_store config.public_file_server.headers = { 'Cache-Control' => "public, max-age=#{2.days.to_i}" } else config.action_controller.perform_caching = false config.cache_store = :null_store end config.active_storage.service = :local config.action_mailer.raise_delivery_errors = true config.action_mailer.perform_deliveries = true    config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'gmail.com', user_name: '<sample@gmail.com>', password: '<mlmt >', authentication: 'plain', enable_starttls_auto: true } config.action_mailer.default_url_options = { host: 'localhost:3000' } config.action_mailer.perform_caching = false config.active_support.deprecation = :log config.active_record.migration_error = :page_load config.active_record.verbose_query_logs = true config.assets.debug = true config.assets.quiet = true config.file_watcher = ActiveSupport::EventedFileUpdateChecker end "config/environments/production.rb" 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 = false config.active_storage.service = :local config.log_level = :debug config.log_tags = [ :request_id ] config.action_mailer.perform_caching = false config.i18n.fallbacks = true config.active_support.deprecation = :notify 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 config.action_mailer.raise_delivery_errors = true #メール送信ができなかった場合、エラーを発生させる config.action_mailer.perform_deliveries = true   #メール送信を有効にする config.action_mailer.delivery_method = :smtp #メール送信する際に通信プロトコルSMTPを利用する ??? config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'gmail.com', user_name: '<zvldvuofqt@gmail.com>', password: '<mlmtkhsumazjarji>', authentication: 'plain', enable_starttls_auto: true } config.action_mailer.default_url_options = { host: 'localhost:3000' } end "お問い合わせの controller" def create @contact = Contact.new( name: params[:name], email: params[:email], message: params[:message] ) if @contact.save ContactMessageMailer.contact_message(@contact).deliver_now redirect_to("/posts/index") else @name = params[:name] @email = params[:email] @message = params[:message] render("contacts/new") end end "Mailerのmethod" class ContactMessageMailer < ApplicationMailer def contact_message(contact) @name = contact.name @message = contact.message mail(to: contact.email, subject: 'お問い合わせ内容の確認') end end

試したこと

同じ様なエラーが出ていた方を参考にして "config/environments/production.rb"にも、Action Mailerのsmtp設定をしたのですがうまくいきません。

補足情報(FW/ツールのバージョンなど)

Rails 6.0.3.3

見づらくてすみません。もし他に必要な情報があれば、ぜひおっしゃって下さい。

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

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

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

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

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

no1knows

2020/09/15 00:02

そもそもproductionの話ということでよいのでしょうか?
guest

回答1

0

localhost" port 25 に接続できないと言ってますので、Railsを動かしているマシンにメールサーバーが立ち上がっていないのでしょう。
gmailを使っておこなうという例がネット上に沢山ありますから
Action Mailer gmail で検索してみてください。

投稿2020/09/14 22:50

winterboum

総合スコア23329

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

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

Honda56

2020/09/15 07:57

ご回答ありがとございます!やっぱりサーバーが立っていなかったんですね、、 ネットでgmailのサーバーの立て方を調べ2段階認証設定やアプリパスワードも設定し development にも設定を書いてもみたのですが、なぜサーバーが立っていないのかもわからないです、、
winterboum

2020/09/15 08:21

? 「なぜサーバーが立っていないのか」 メールサーバーは自動で立ち上がるものではないです。ご自身が立ち上げる設定をしていないなら立ち上がっていません。 というか、、、、 ドメインを持っていなければメールサーバを立ち上げても(昨今は)送信はできても怪しいサーバーからのメールだということで受け取ってもらえないでしょう
Honda56

2020/09/15 08:36

僕の理解度が低くわかりにくい返答ですみません “一応サーバーを立ち上げる設定をしたものの(つもりかもしれませんが)、やっぱり立っていなかったのか”、という意味でした、、 自分が持っているgmailアドレスを使って次のように設定したのですが、何かおかしいところはありますでしょうか? それともそもそもドメインとは、gmailのアドレスのことではないのでしょうか? :( config.action_mailer.raise_delivery_errors = true config.action_mailer.perform_deliveries = true config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'gmail.com', user_name: '<自分のgmailアドレス>', password: '<自分のアプリパスワード>', authentication: 'login', enable_starttls_auto: true }
winterboum

2020/09/15 11:14

ちらっとみでは良さそうです
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問