使ってたのをそのままコピペ。
チェック甘いところもあり。
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);
}
}
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', 'ふりがな');
}
}
// フォーム出力 require_once(get_stylesheet_directory() . '/assets/php/contact.php'); $form = new Contact(); $form->run();
