Newer
Older
web / apply / appjd.rb
@HIROSE Yuuji HIROSE Yuuji on 28 Jun 2021 2 KB Typo of charset
#!/usr/bin/env ruby27
# -*- coding: utf-8 -*-
require 'cgi'

myname	= File.basename($0)
rcpt	= "jd@e.koeki-u.ac.jp"
rcpt	= "yuuji@es.gentei.org"

=begin
params = {
  "pref" => "都道府県", "schoolid" => "学校番号", "schoolname" => "学校名",
  "hiraname" => "ふりがな", "fm" => "性別", "name" => "氏名",
  "birthday" => "生年月日",
  "pname" => "保護者名", "phiragana" => "ほごしゃめい",
  "grade" => "学年", "class" => "クラス", "seatnum" => "出席番号",
  "address" => "住所", "phone" => "電話番号", "email" => "email",
  "transport" => "交通手段", "clubs" => "課外活動",
  "motive" => "応募の動機", "interest" => "科学的興味",
  "contest" => "コンテスト等参加歴", "issues" => "地域課題意識",
  "selfintro" => "自己紹介"
}
# grep name= appform.html | sed -E 's/.*name="([^"]*)".*/\1/'|uniq 
for k, v in params
  printf("<h2>%s</h2>\n<p class=\"answer\">\n__%s__\n</p>\n",
         v, k.upcase)
end; exit
# ./appjd.rb > mailtmpl.txt
=end

header = <<_EOS_
<!DOCTYPE html>
<head><title>Junior Doctor Chokai Academy Application</title>
<link rel="stylesheet" type="text/css" href="../jdmain.css">
</head>
<body>
<main>
_EOS_
footer = "<p><a href=\"./#{myname}\">もどる</a></p>
</main></body>\n"

sent = <<_EOF_
<h1>送信完了</h1>
<p>以下の内容でジュニアドクター鳥海塾申請書を送信しました。</p>
_EOF_

c = CGI.new(:accept_charset => "UTF-8")
print "Content-type: text/html; charset=UTF-8\n\n"
print header

email = c["email"].read
name  = c["pname"].read
from = ""

require 'nkf'
sj = NKF.nkf('-jM', "申請書").strip
fr = NKF.nkf('-jM', name).strip
from = sprintf("%s <%s>", fr, email)

# For recievers
body = open("mailtmpl.txt"){|tp| tp.read}.force_encoding('utf-8')
body.gsub!(/__(\w+)__/){|k| CGI.escapeHTML(c[$1.downcase].read)}

# For applicant
table = open("mailtmpl.html"){|h| h.read}.force_encoding('utf-8')
table.gsub!(/__(\w+)__/){|k| CGI.escapeHTML(c[$1.downcase].read)}


if c["recfile"].original_filename > "" then
  require "tmpdir"
  Dir.mktmpdir {|tmp|
    of = c["recfile"].original_filename
    ext = File.extname(of)
    grade = c["grade"].read.sub("小", "E").sub("中", "J")
    bn = [c["schoolid"], grade, c["class"], c["seatnum"]].join("-") + ext
    file = File.expand_path(bn, tmp)
    open(file, "w"){|o| o.binmode; o.write c["recfile"].read}
    open("| ./sendmultipart.sh -t #{rcpt} -s '#{sj}' -f '#{fr}' '#{file}'", "w") do |m|
      m.print body
    end
    table.gsub!(/::FILE::/,
                sprintf("%dバイトの%sファイル",
                        c["recfile"].size, ext.sub(".", "").upcase))
  }
else
  open("| sendmail -f #{email} #{rcpt}", "w") do |m|
      m.puts(<<~EOS)
	To: #{rcpt}
	From: #{from}
	Subject: #{sj}
	Mime-Version: 1.0
	Content-Transfer-Encoding: 7bit
	Content-Type: Text/Plain; charset=utf-8

	#{body}
	EOS
  end
  table.gsub!(/::FILE::/, "なし")
end

print sent
print table
print footer