LoginSignup
8
12

More than 5 years have passed since last update.

Sinatraを使って問い合わせフォーム(超ざっくり)の仕上げ

Last updated at Posted at 2015-07-20

はじめに

  1. http://qiita.com/ms2sato/items/7c07b2079b48c49fad2a
  2. http://qiita.com/ms2sato/items/eed1e3c929ac8f6195a9
  3. http://qiita.com/ms2sato/items/5bd201c84dab858ee501
  4. http://qiita.com/ms2sato/items/901d8c5ca77c1133522c
  5. http://qiita.com/ms2sato/items/9b102144f221f9ac5bf8

の続き。

各種調整

メール送信を完成させます。それぞれ整えるだけです。

app.rb
require 'rubygems'
require 'bundler'
Bundler.require

require 'sinatra'
require 'sinatra/reloader'
require 'rest_client'

get '/' do
  erb :index  
end

get '/about' do
  erb :about
end

post '/confirm' do
  @email = params[:email]
  @message = params[:message]
  erb :confirm
end

post '/send_mail' do
  email = params[:email]
  message = params[:message]
  send_message(email, message)  

  erb :thanks
end

def send_message(email, message)
    RestClient.post "https://api:key-[secret...]"\
      "@api.mailgun.net/v3/sandbox[secret...].mailgun.org/messages",
      :from => "お問い合わせ <xxx@xxx.com>",
      :to => "MasashiSato <xxx@xxx.com>",
      :subject => "お問い合わせがきました!",
      :text => "email: #{email}さんからお問い合わせがきました。本文: #{message}"
end
views/confirm.erb
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <h1>お問い合わせ内容確認</h1>
  <div>
    <div>
      email: <%=@email%>
    </div>  
    <div>
      本文:
      <p>
        <%=@message%>
      </p>
    </div>    
  </div>
  <form action="send_mail" method="POST">
    <input type="hidden" value="<%=@email%>" name="email">
    <input type="hidden" value="<%=@message%>" name="message">
    <input type="submit" value="送信する">
  </form>
</body>
</html>

送信後のページを作成します。

thanks.erb
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
<h1>ありがとうございました</h1>
<p>送信しました。お返事がくるまでしばらくお待ち下さい。</p>
<p><a href="/">トップへ戻る</a></p>
</body>
</html>

おしまいに

とりあえずここまでで、問い合わせ内容のメール送信ができるようになった。わーい!ただ、とりあえず実装が入っているだけで、セキュリティ面やエラー処理が全くないので実運用には耐えません。ご注意を。

8
12
0

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
8
12