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

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

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

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

Q&A

解決済

1回答

5697閲覧

RailsでActionMailerを用いた時のOpenTimeoutエラーを解決したい

syclimb

総合スコア13

Ruby on Rails 5

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

0グッド

0クリップ

投稿2018/10/26 13:16

編集2018/11/01 11:45

前提・実現したいこと

【実現したいこと】
ECにおいて注文が発生した際に、注文の内容がメールで自動送信されるようにしたい。
【前提】
Ruby on Rails 5を用いてWebアプリケーションを作成しています。
そこで注文が発生した際に、その注文内容がメールで送られてくるようにしたくいです。
それがRailsのActionMailerを用いればできると知り、下記のサイトを参考にコードを書きました。

【参考にしたサイト】
https://www.sejuku.net/blog/48739
http://a-new-step.hatenablog.com/entry/2018/06/24/130107
http://www.cattlemute.com/2017/10/30/55/

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

注文確定時にはform_tagでcompleteアクションが実行されるのですが、下記のエラーがでてしまい、メールが送信されません。

Net::OpenTimeout in OrderController#complete
execution expired

なおメーラーをインストールする前は特に問題なく動いていました。
おそらくcompleteアクション内(下記参照)の

OrderMailer.send_mail(order).deliver_now

部分で何かしらエラーが発生しているものと思われます。
(binding.pryを用いたところ、その直前までは問題なく動きました)

メーラーに関わる設定など

###order_mailer.rb
Order.Mailerモデルにおいてsend_mailメソッドを用いています。

class OrderMailer < ApplicationMailer def send_mail(order) @order = order mail( from: "from@example.com", to: "******@gmail.com", subject: "mailerテスト" ) end end

###order_controller.rb
注文確定時のcompleteアクション

def complete @order=Order.new( name1: session[:name1], name2: session[:name2], tel1: session[:tel1], tel2: session[:tel2], tel3: session[:tel3], email: session[:email], zipcode: session[:zipcode], pref: session[:pref], addr1: session[:addr1], addr2: session[:addr2], delivery_date: session[:delivery_date], pay_type: session[:pay_type], ) if order.save OrderMailer.send_mail(order).deliver_now flash[:notice]="ご注文ありがとうございました" render("home/top") end end

###config\environment\development.rb
- 正直このファイルの意味はよくわかっていないです…

Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. # In the development environment your application's code is reloaded on # every request. This slows down response time but is perfect for development # since you don't have to restart the web server when you make code changes. config.cache_classes = true # Do not eager load code on boot. config.eager_load = false # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = true # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = true # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log # Raise an error on page load if there are pending migrations. config.active_record.migration_error = :page_load # Debug mode disables concatenation and preprocessing of assets. # This option may cause significant delays in view rendering with a large # number of complex assets. config.assets.debug = true # Asset digests allow you to set far-future HTTP expiration dates on all assets, # yet still be able to expire them through the digest params. config.assets.digest = true # Adds additional error checking when serving assets at runtime. # Checks for improperly declared sprockets dependencies. # Raises helpful error messages. config.assets.raise_runtime_errors = true # Raises error for missing translations # config.action_view.raise_on_missing_translations = true config.action_mailer.perform_deliveries = true config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } config.action_mailer.default_options = {from: 'from@example.com'} config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'gmail.com', user_name: '******@gmail.com', password: '******', authentication: 'plain', enable_starttls_auto: true, } end

###application_mailer.rb

class ApplicationMailer < ActionMailer::Base default from: "from@example.com" layout 'mailer' end

試したこと

mailerのプレビュー機能(localhost:3000/rails/mailers/order_mailer)では問題ありませんでした。

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

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

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

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

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

troch

2018/11/02 14:15

メーラーをインストールする前は特に問題なく動いていた、とありますが、以前は上記のコードでうまくメールを送れていたのに急に送れなくなった、ということでしょうか?
syclimb

2018/11/05 02:10 編集

以前送れていたわけではありません(誤解を与える記載ですみません)。 開発をしている中で、新しくRailsのメーラーを生成してからエラーがでるようになったということです。メーラー以外の部分のエラーではないと考えており、その旨をお伝えする意図で記載しました。
guest

回答1

0

ベストアンサー

タイムアウトが発生している、ということは、メーラーの問題ではなくネットワークの問題だと思います。
開発環境ではIPv6を使っていますでしょうか?
IPv6通信を無効化してみてはどうでしょうか。
https://www.pebblewind.com/entry/2016/10/01/182251

投稿2018/11/05 03:35

troch

総合スコア349

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

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

syclimb

2018/11/05 09:45

開発環境はIPv6を使っています。 ご教授いただいたサイトを参考に試そうと思ったのですが、会社のPCの通信をIPv6からIPv4優位に変更するためには管理者権限が必要のため、すぐにはできなさそうです(できれば別の方法を探したいです)。いずれにせよ、ご回答いただきありがとうございます。もう少し頑張ってみます。
troch

2018/11/05 12:49

社用PCでしたか… その場合、確かにPCの設定いじるよりは別の方法を検討した方がいいかもしれまません。 https://support.google.com/a/answer/176600?hl=ja Googleの公式サイトを少し調べたのですが、 今回syclimbさんはGoogleのsmtpサーバを使用していますので、 Google側の設定で問題回避できるかもしれません。 Googleの設定で「安全性の低いアプリの許可: 有効」にしてみてはどうでしょうか。 あとは…SSL(TLS)通信が必須と書いてますので、 SSL対応にする必要もあるかもしれません。 https://tokyo-engineer.com/ruby-on-rails-ssl/ ↑のgemでSSL対応が出来るようです。 頑張ってください!
syclimb

2018/11/08 04:31

おかげさまで解決することができました! 原因は以下2つでした。 1.社内ネットワークを用いていたこと 2.development.rbにおけるメーラー設定のパスワードが間違っていたこと 1.社内ネットワークを用いていたことがタイムアウトの原因でした。別のネットワーク(個人用 Wi-Fiなど)を用いることで解決しました。 2.メーラーの設定パスワードはGoogle App Passwordから取得したものにする必要がありました。 とても助かりました、ありがとうございました!
troch

2018/11/08 05:15

なるほど、LAN環境だったのですね。 ともかく解決してよかったです!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問