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

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

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

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

0回答

1447閲覧

Rails メールが送信されない

kwtkwt

総合スコア21

Ruby on Rails 5

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2019/07/25 03:07

編集2019/07/25 06:24

Rails でお問い合わせからメールが届く仕組みを作成しています。

エラーはでていないのですがメールが届きません。

試したこと

  • gmail 二段階認証を有効にした
  • 安全性の低いアプリの許可 を有効にした

コード

ruby

1Rails.application.routes.draw do 2 root "users#index" 3 resources :contacts 4 resources :users 5end

ruby

1class ContactsController < ApplicationController 2 def new 3 end 4 5 def create 6 @contact = Contact.new(contact_params) 7 8 if @contact.save 9 ContactMailer.contact_email(@contact).deliver_now 10 flash[:success] = '送信しました' 11 redirect_to(users_url) 12 # debugger 13 else 14 redirect_to(users_url) 15 flash[:success] = '失敗しました' 16 end 17 end 18 19 private 20 21 def contact_params 22 params.require(:contact).permit(:name, :message, :email) 23 end 24end

ruby

1class UsersController < ApplicationController 2 def index 3 @user = User.new 4 @contact = Contact.new 5 end 6 7 def create 8 end 9 10 def new 11 end 12 13 def edit 14 end 15end

erb

1<%= flash[:success] %> 2<header> 3 <p class="sub-title">fafadfa</p> 4 <h1 class="title">Let's play baseball</h1> 5 <p class="sub-title2">I can not find a prototype partner...</p> 6</header> 7 8<section class="link"> 9 10</section> 11 12<section class="contact"> 13 <h2>Contact</h2> 14 <%= form_for(@contact) do |f| %> 15 <%= f.label :Name, class: 'form-margin' %> 16 <%= f.text_field :name, class: 'form-control' %> 17 <%= f.label :Email, class: 'form-margin' %> 18 <%= f.text_field :email, class: 'form-control' %> 19 <%= f.label :Message, class: 'form-margin' %> 20 <%= f.text_field :message, class: 'form-control' %> 21 <%= f.submit 'SEND' %> 22 <% end %> 23</section> 24 25<footer class="footer"> 26 27</footer>

ruby

1class ContactMailer < ApplicationMailer 2 3 def contact_email(contact) 4 @contact = contact 5 mail( 6 from: "#{@contact.name}", 7 to: '任意のgmailアドレス', 8 subject: 'お問い合わせ通知' 9 ) 10 # debugger 11 end 12end

ruby

1Rails.application.configure do 2 # Settings specified here will take precedence over those in config/application.rb. 3 4 # In the development environment your application's code is reloaded on 5 # every request. This slows down response time but is perfect for development 6 # since you don't have to restart the web server when you make code changes. 7 config.cache_classes = false 8 9 # Do not eager load code on boot. 10 config.eager_load = false 11 12 # Show full error reports. 13 config.consider_all_requests_local = true 14 15 # Enable/disable caching. By default caching is disabled. 16 # Run rails dev:cache to toggle caching. 17 if Rails.root.join('tmp', 'caching-dev.txt').exist? 18 config.action_controller.perform_caching = true 19 20 config.cache_store = :memory_store 21 config.public_file_server.headers = { 22 'Cache-Control' => "public, max-age=#{2.days.to_i}" 23 } 24 else 25 config.action_controller.perform_caching = false 26 27 config.cache_store = :null_store 28 end 29 30 # Store uploaded files on the local file system (see config/storage.yml for options) 31 config.active_storage.service = :local 32 33 # Don't care if the mailer can't send. 34 config.action_mailer.raise_delivery_errors = false 35 36 config.action_mailer.perform_caching = false 37 38 # Print deprecation notices to the Rails logger. 39 config.active_support.deprecation = :log 40 41 # Raise an error on page load if there are pending migrations. 42 config.active_record.migration_error = :page_load 43 44 # Highlight code that triggered database queries in logs. 45 config.active_record.verbose_query_logs = true 46 47 # Debug mode disables concatenation and preprocessing of assets. 48 # This option may cause significant delays in view rendering with a large 49 # number of complex assets. 50 config.assets.debug = true 51 52 # Suppress logger output for asset requests. 53 config.assets.quiet = true 54 55 # Raises error for missing translations 56 # config.action_view.raise_on_missing_translations = true 57 58 # Use an evented file watcher to asynchronously detect changes in source code, 59 # routes, locales, etc. This feature depends on the listen gem. 60 config.file_watcher = ActiveSupport::EventedFileUpdateChecker 61 62 # gmailの設定 63 config.action_mailer.delivery_method = :smtp 64 config.action_mailer.smtp_settings = { 65 address: 'smtp.gmail.com', 66 port: 587, 67 domain: 'gmail.com', 68 user_name: '任意のgmailアドレス', 69 password: '付随するパスワード', 70 authentication: 'plain', 71 enable_starttls_auto: true 72 } 73end

ログ

Started POST "/contacts" for ::1 at 2019-07-25 12:06:25 +0900 Processing by ContactsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"3FdTVCpFBQdEijop3HzP6z63iyBZ2LBrlC1TOFmFQjFChKdUDT3jawbXPGoSYUzE8pz4JCAKqYp+pAyjUDmNvA==", "contact"=>{"name"=>"aaa", "email"=>"aaa@gmail.com", "message"=>"aaa"}, "commit"=>"SEND"} (0.1ms) begin transaction ↳ app/controllers/contacts_controller.rb:8 Contact Create (0.5ms) INSERT INTO "contacts" ("name", "message", "created_at", "updated_at", "email") VALUES (?, ?, ?, ?, ?) [["name", "aaa"], ["message", "aaa"], ["created_at", "2019-07-25 03:06:25.172183"], ["updated_at", "2019-07-25 03:06:25.172183"], ["email", "aaa@gmail.com"]] ↳ app/controllers/contacts_controller.rb:8 (1.0ms) commit transaction ↳ app/controllers/contacts_controller.rb:8 Rendering contact_mailer/contact_email.html.erb within layouts/mailer Rendered contact_mailer/contact_email.html.erb within layouts/mailer (1.0ms) Rendering contact_mailer/contact_email.text.erb within layouts/mailer Rendered contact_mailer/contact_email.text.erb within layouts/mailer (0.7ms) ContactMailer#contact_email: processed outbound mail in 41.7ms Sent mail to 任意のアドレス (1588.4ms) Date: Thu, 25 Jul 2019 12:06:25 +0900 From: aaa To: 任意のアドレス Message-ID: <5d391cb135a22_56923ff322bde23c355cc@r-kawata.local.mail> Subject: =?UTF-8?Q?=E3=81=8A=E5=95=8F=E3=81=84=E5=90=88=E3=82=8F=E3=81=9B=E9=80=9A=E7=9F=A5?= Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="--==_mimepart_5d391cb1342e5_56923ff322bde23c354ed"; charset=UTF-8 Content-Transfer-Encoding: 7bit ----==_mimepart_5d391cb1342e5_56923ff322bde23c354ed Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: base64 YWFh5qeY44KI44KK44CB44GK5ZWP44GE5ZCI44KP44Gb44GM5bGK44GN44G+ 44GX44Gf44CCDQotLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t LS0tLS0tLS0tLS0tLS0tLS0tLS0tDQoNCmFhYQ0KDQo= ----==_mimepart_5d391cb1342e5_56923ff322bde23c354ed Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <!DOCTYPE html>=0D <html>=0D <head>=0D <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf= -8" />=0D <style>=0D /* Email styles need to be inline */=0D </style>=0D </head>=0D =0D <body>=0D <!DOCTYPE html>=0D <html>=0D <head>=0D <meta content=3D'text/html; charset=3DUTF-8' http-equiv=3D'Content-Ty= pe' />=0D </head>=0D <body>=0D <h1>aaa=E6=A7=98=E3=82=88=E3=82=8A=E3=80=81=E3=81=8A=E5=95=8F=E3=81=84= =E5=90=88=E3=82=8F=E3=81=9B=E3=81=8C=E5=B1=8A=E3=81=8D=E3=81=BE=E3=81=97=E3= =81=9F=E3=80=82</h1>=0D <p>=0D aaa=0D </p>=0D </body>=0D </html>=0D =0D </body>=0D </html>=0D ----==_mimepart_5d391cb1342e5_56923ff322bde23c354ed-- Redirected to http://localhost:3000/users Completed 302 Found in 1637ms (ActiveRecord: 1.6ms) Started GET "/users" for ::1 at 2019-07-25 12:06:26 +0900 Processing by UsersController#index as HTML Rendering users/index.html.erb within layouts/application Rendered users/index.html.erb within layouts/application (2.4ms) Completed 200 OK in 50ms (Views: 47.5ms | ActiveRecord: 0.0ms)

よろしくお願いいたします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問