前提・実現したいこと
【実現したいこと】
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)では問題ありませんでした。
回答1件
あなたの回答
tips
プレビュー