###RailsのAction Mailerで、送信ボタンを押すと同時に、管理者側と返信先にメールが届く機能の実装。
HTMLで作ったものをRailsに反映させる方法で、自分のホームページを作っています。お問い合わせフォームで、手も足も出ない状況になりました。
###発生している問題・「メールが宛先に届かなかった。」
app/mailer/contact_mailer.rb
class ContactMailer < ApplicationMailer::Base default from: "#自分のメアド", reply_to: "<%= @contact.email %>", cc: "#自分のメアド", charset: 'UTF-8' def received_email(arg) @arg = arg @greeting = "Hi" mail (to: "<%= @contact.email %>", cc: "#自分のメアド", subject: 'ご連絡、ありがとうございます。') end end
app/views/contact_mailer/received_email.text.rb
webサイトから問い合わせがありました。 -------------------------- Name:<br> <%= @contact.name %> Email:<br> <%= @contact.email %> Text:<br> <%= @contact.text %> --------------------------
app/models/contact.rb
class Contact include ActiveModel::Model attr_accessor :name, :email, :text #これ大事 validates :name, :length => { minimum: 3, :too_short => 'お名前(ニックネームも可)を入力してください。'} validates :email, :length => { minimum: 3, :too_short => 'メールアドレスを入力してください。'} validates :text, :presence => {:message => 'メッセージを入力してください。'} end
app/views/contact/index.html.erb
<%= form_for @contact, :url => contact_create_path do |f| %> <% if @contact.errors.any? %> <div class="alert alert-danger" role="alert"> <strong>入力内容にエラーがあります</strong> <ul> <% @contact.errors.each do |attr, msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <h3>メッセージを送る</h3> <table class="table"> <tr> <td><%= f.text_field :name, placeholder: 'お名前(必須)', class: 'name' %></td> </tr> <tr> <td><%= f.text_field :email, placeholder: 'メールアドレス(必須)', class: 'email' %></td> </tr> <tr> <td><%= f.text_area :text, placeholder: 'よかったら、メッセージをお願いします!', rows: '10', class: 'text' %></td> </tr> </table> <div class="submit"> <%= f.submit 'SENT', class: 'submit-button' %> </div> <% end %>
app/controllers/contact_controller.rb
class ContactController < ApplicationController def index @contact = Contact.new render :action => 'index' end def create @contact = Contact.new(contact_params) end def sendmail @mail = ContactMailer.received_email(@contact).deliver render text: 'メール送信完了' render :action => 'create' end private def contact_params params.require(:contact).permit(:name, :email, :text) end end
confing/routes.rb
Rails.application.routes.draw do root "top#index" get "top" => "top#index" get "top/profile" => "top#profile" get "top/cost" => "top#cost" get "contact" => "contact#index" post "contact/create" => "contact#create" end
confing/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :enable_starttls_auto => true, :address => "smtp.gmail.com", :port => 587, :domain => 'gmail.com', :user_name => "#自分のgmailアドレス", :password => "#自分のgmailアドレスのパスワード", :authentication => 'plain' }
$ vi config/environments/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 = false # 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 = false # Don't care if the mailer can't send. # ActionMailer config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } config.web_console.whitelisted_ips = '0.0.0.0/0' config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :letter_opener_web config.action_mailer.smtp_settings = { :enable_starttls_auto => true, :address => "smtp.gmail.com", :port => 587, :domain => 'gmail.com', :user_name => "", #自分のgmailアドレス :password => "", #自分のgmailアドレスのパスワード :authentication => 'plain' } # 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 end
app/views/contact/create.html.erb
<div class="contents"> <div class="success"> <h3>投稿が完了しました。</h3> <a class="btn" href="/top">一覧へ戻る</a> </div> </div>
###試したこと
・dbと関係ない[Rails 4.1] ActiveModelを使って、DBと関係ないFormを作り、確認画面付きのお問い合わせフォームを作る方法
・メール送信の方法 (Rails4/ActionMailer)
を参考にしました。
画像のように、ビューは動くのですが、肝心のメールが送信できませんでした。
どなたかお詳しい方、お教え願えませんでしょうか。
よろしくお願いいたします。

回答4件
あなたの回答
tips
プレビュー