LoginSignup
1

More than 5 years have passed since last update.

railsで、メールフォームからサイト管理者と、送信者にメールを送信する実装について、教えていただけませんか?

Last updated at Posted at 2017-03-11

自分のホームページを作っています。

htmlとcssをrailsに反映させ、ホームページを作っており、お問い合わせフォームの、メール送信機能の部分で足踏みしています。

どなたか詳しい方、教えていただけませんか?

app/mailers/contact_mailer.rb
contact_mailer.rb
class ContactMailer < ApplicationMailer::Base
  default from: "from@gmail.com", 
          reply_to: "reply_to@example.com",
          charset: 'UTF-8'

  def received_email(arg)
    @arg = arg
    @greeting = "Hi"

    mail (to: "to@example.org", cc: "#ここに自分のGメールアドレスを記入しています", subject: '【MIYUKImémoires】ご連絡、ありがとうございます。')
  end
end
app/controllers/contact_controller.rb
contact_controller.rb
class ContactController < ApplicationController

  def index
    @contact = Contact.new
  end

  def received_emailcon
    arg = ContentModel.find(1)
    @mail = ContentMailer.received_email(arg).deliver
    render text: 'メール送信完了'
  end

end

app/views/contact_mailer/received_email.text.erb
received_email.text.erb

--------------------------
Name:<br>
<%= @contacts.name %>
Email:<br>
<%= @contacts.email %>
Text:<br>
<%= @contacts.text %>
--------------------------

app/confing/environments/development.rb

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'
  }
app/views/contact_mailer/received_email.html.erb
received_email.html.erb
ContactMailer#received_email

<%= @greeting %>, find me in /app/views/contact_mailer/received_email.html.erb

<h2>テスト</h2>
<p>あいうえおかきくけこさしすせそたちつてとなにぬねのあいうえおかきくけこさしすせそたちつてとなにぬねのあいうえおかきくけこさしすせそたちつてとなにぬねのあいうえおかきくけこさしすせそたちつてとなにぬねのあいうえおかきくけこさしすせそたちつてとなにぬねのあいうえおかきくけこさしすせそたちつてとなにぬねの</p>
app/views/contact/index.html.erb
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/views/contact/create.html.erb
create.html.erb
<div class="contents">
      <div class="success">
        <h3>投稿が完了しました。</h3>
        <a class="btn" href="/top">一覧へ戻る</a>
      </div>
  </div>
app/confing/routes.rb
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
app/models/contact.rb
contact.rb
class Contact 
  include ActiveModel::Model

  attr_accessor :name, :email, :text #これ大事

  validates :name, :presence => {:message => 'お名前(ニックネームも可)'}
  validates :email, :presence => {:message => 'メールアドレス'}
  validates :text, :presence => {:message => 'よかったら、メッセージをお願いします!'}
end

スクリーンショット 2017-03-11 18.30.50.png
スクリーンショット 2017-03-11 18.31.11.png

画像のように、ビューは動いていますが、メール自体はとどきませんでした。
どうやって、SENTボタンをクリックすると同時に、管理者側と送信者にメールを送れるのでしょうか。

試しに参考にしたサイトのコードをコピペしたりしました。結構ゴチャゴチャしているかもしれません。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1