使ってたのをそのままコピペ。
チェック甘いところもあり。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
class FormClass { public $errors = array(); public $post = array(); public $admin_email = ''; public $noreply_email = ''; public $template_path = ''; public $mail_template_path = ''; // コンストラクタ public function __construct() { $this->post = @$_POST; } // 必須チェック public function required($field = '', $name = '') { if(empty($this->post[$field])) { $this->errors[$field] = '「' . $name . '」は必須です。'; } } // 文字数最大 public function max_length($field = '', $name = '', $max) { if(!empty($this->post[$field]) and mb_strlen($this->post[$field]) > $max) { $this->errors[$field] = '「' . $name . '」は' . $max . '文字以内で入力してください。'; } } // 文字数最小 public function min_length($field = '', $name = '', $min) { if(!empty($this->post[$field]) and mb_strlen($this->post[$field]) <= $min) { $this->errors[$field] = '「' . $name . '」は' . $min . '文字以上で入力してください。'; } } // 数字チェック public function numeric($field, $field_name) { if(!empty($this->post[$field]) and !is_numeric($this->post[$field])) { $this->errors[$field] = '「' . $field_nam . '」は数字で入力してください。 '; } } // メールアドレス形式チェック public function email($field, $field_name) { if(!empty($this->post[$field]) and !preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $this->post[$field])) { $this->errors[$field] = '「' . $field_name . '」はメールアドレス形式で入力してください。'; } } // 郵便番号形式チェック public function postal($field, $field_name) { if(!empty($this->post[$field]) and !preg_match("/^\d{3}\-\d{4}$/", $this->post[$field])) { $this->errors[$field] = '「' . $field_name . '」は郵便番号形式(XXX-XXXX)で入力してください。'; } } // 電話番号 public function tel($field, $field_name) { if(!empty($this->post[$field]) and !is_numeric(str_replace('-', '', $this->post[$field]))) { $this->errors[$field] = '「' . $field_name . '」は半角数字(ハイフン可)で入力してください。'; } } // ひらがな public function hiragana($field, $field_name) { if(!empty($this->post[$field]) and !preg_match("/^[ぁ-ん]+$/u", $this->post[$field])) { $this->errors[$field] = '「' . $field_name . '」は全角ひらがなで入力してください。'; } } // カタカナ public function katakana($field, $field_name) { if(!empty($this->post[$field]) and !preg_match("/^[ァ-ヶー]+$/u", $this->post[$field])) { $this->errors[$field] = '「' . $field_name . '」は全角カタカナで入力してください。'; } } // メール文章 public function inc_mail_body($template = '', $params = array()) { if(!empty($template) and !empty($params)) { // 変数化 extract($params); // ファイル内容の取得 ob_start(); include $this->mail_template_path . $template; $html = ob_get_contents(); ob_end_clean(); return $html; } } // メール送信 public function send_mail($mailfrom, $mailto, $subject , $body , $force = false ) { mb_language("ja"); mb_internal_encoding("UTF-8"); // ヘッダー定義(送信者アドレス・返信アドレス) $header = ''; $header .= "From:".mb_encode_mimeheader($mailfrom) . "<".$mailfrom.">\r\n"; $header .= "Reply-To:".$mailfrom."\r\n"; $header .= "Content-Type: text/plain; charset=\"ISO-2022-JP\";\n"; // エラー受け取り先 $return_path = '-f ' . $mailfrom; return mb_send_mail($mailto, $subject, mb_convert_encoding($body, 'ISO-2022-JP-MS'), $header, $return_path); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
require_once(get_stylesheet_directory() . '/assets/php/form.class.php'); class Contact extends FormClass { /** * メイン処理 * * @access public * @return */ public function run() { // 設定 $this->admin_email = 'post@kyuei-inc.co.jp'; $this->template_path = get_stylesheet_directory() . '/assets/php/template/contact/'; $this->mail_template_path = $this->template_path . 'mail/'; // デフォルトテンプレート $tempalte = 'input.php'; if(!empty($this->post)) { // バリデーション $this->_valid(); // エラーなし if(empty($this->errors)) { // 確認画面へ if(!empty($this->post['confirm'])) { // 確認画面テンプレート $tempalte = 'confirm.php'; } // 完了画面へ elseif(!empty($this->post['thanks'])) { // 管理者 送信設定 $mailfrom = $this->admin_email; $mailto = $this->admin_email; $subject = 'Webサイトからお問い合わせがありました。'; $body = $this->inc_mail_body('admin.php', $this->post); // 送信 $this->send_mail($mailfrom, $mailto, $subject, $body); // 応募者 送信設定 $mailfrom = $this->admin_email; $mailto = $this->post['email']; $subject = 'お問い合わせを受け付けいたしました。'; $body = $this->inc_mail_body('user.php', $this->post); // 送信 $this->send_mail($mailfrom, $mailto, $subject, $body); // 完了画面テンプレート $tempalte = 'thanks.php'; } } } include($this->template_path . $tempalte); } /** * バリデーション * * @access public * @return */ private function _valid() { // 必須 $this->required('fullname', 'お名前'); $this->required('kana', 'ふりがな'); $this->required('email', 'メールアドレス'); $this->required('pref', '都道府県'); $this->required('address', 'ご住所'); $this->required('tel', 'お電話番号'); $this->required('message', 'お問い合わせ内容'); // 文字数最大 $this->max_length('fullname', 'お名前', 10); $this->max_length('kana', 'ふりがな', 20); $this->max_length('email', 'メールアドレス', 100); $this->max_length('pref', '都道府県', 5); $this->max_length('postal', '郵便番号', 8); $this->max_length('address', 'ご住所', 100); $this->max_length('tel', 'お電話番号', 15); $this->max_length('message', 'お問い合わせ内容', 2000); // メールアドレス $this->email('email', 'メールアドレス'); // 郵便番号 $this->postal('postal', '郵便番号'); // 電話番号 $this->tel('tel', 'お電話番号'); // ふりがな $this->hiragana('kana', 'ふりがな'); } } |
1 2 3 4 |
// フォーム出力 require_once(get_stylesheet_directory() . '/assets/php/contact.php'); $form = new Contact(); $form->run(); |