2013年4月24日

私的メモ:PHPメールフォーム

【HTML】
フォームページ「index.html」
<div id="wrap_formbox">
<div id="formbox">
<form method="post" action="confirm.php">
<table>
<tr>
<th>プルダウン</th>
<td>
<select name="title">
<option value="項目1">項目1</option>
<option value="項目2">項目2</option>
<option value="項目3">項目3</option>
</select></td>
</tr>
<tr>
<th>テキストエリア</th>
<td><textarea name="comment" cols="60%" rows="5"></textarea></td>
</tr>
<tr>
<th>チェック項目</th>
<td>
<input name="check[]" value="チェック1" type="checkbox" />チェック1 
<input name="check[]" value="チェック2" type="checkbox" />チェック2
</td>
</tr>
<tr>
<th>名前 <span style="color:#FF0000;">※</span></th>
<td><input style="width: 60%;" type="text" name="name" /></td>
</tr>
<tr>
<th>郵便番号</th>
<td><input style="width: 20%;" type="text" name="zip1" /></td>
</tr>
<tr>
<th>住所</th>
<td><input style="width: 80%;" type="text" name="add" /></td>
</tr>
<tr>
<th>電話番号</th>
<td><input style="width: 50%;" type="text" name="tel" /></td>
</tr>
<tr>
<th>Email <span style="color:#FF0000;">※</span></th>
<td><input style="width: 80%;" type="text" name="email" /></td>
</tr>
</table>
<div class="center"><p><input value="送信確認" type="submit" name="preview_sys" /></p></div>
</form>
</div>
</div>

確認ページ「confirm.php」
<?php session_cache_limiter('private_no_expire'); session_start(); ?>
<!-- ↑ページ最上部に設置  -->

<div id="wrap_formbox">
<div id="formbox">
<h2>送信確認</h2>
<?php include ('sendmail.php'); ?>
<p><?php echo $explain; ?></p>
<form action="result.php" method="post">
<table><?php confirm(); ?></table>
<div class="center"><p><?php submit(); ?></p></div>
</form>
</div>
</div>

送信完了ページ「result.php」
<?php session_cache_limiter('private_no_expire'); session_start(); ?>
<!-- ↑ページ最上部に設置 -->

<div id="wrap_formbox">
<div id="formbox">
<h2>送信完了</h2>
<?php include ('sendmail.php'); ?>
<p><?php transmited(); ?></p>
<p class="center"><input type="button" onclick="location.href='/'" value="ホームへ" /></p>
</div>
</div>


【CSS】
@charset "utf-8";
#wrap_formbox {width:100%;}
#formbox {width:780px; margin:0 auto;}
#formbox table{width:100%; background:#f3f2ef; border-collapse:collapse;}
#formbox th, #formbox td{padding:5px 10px; border:1px solid #c8c8c8;}
#formbox th{width:30%;}
.center{text-align:center;}

【Sendmail】

<?php

/////////////////////////////////////////////////////////////////////////////////
//各種設定値


//自動返信フラグ
$remailflg = 1;

//送信者名
$sender = "送信者名";

//送信元アドレス
$senderMail = "hoge@hogehoge.com";

//通知先アドレス
$myMail = "hoge@hogehoge.com";
// 複数先に送信する場合は、「hoge@hogehoge.com,hoge@hogehoge.jp」と『,』で区切る

//通知メール件名
$mailTitle = "【送信者名】お問い合わせ・資料請求がありました";

//通知メール本文
$mailText = <<<EOF
───────────────────────────────────
送信者名「お問い合わせ・資料請求」フォームより、以下の内容が
送信されました。
───────────────────────────────────
EOF;

//自動返信メール件名
$remailTitle = "【送信者名】お問い合わせ・資料請求のありがとうございます";

//自動返信メール本文
$remailText = <<<EOF
───────────────────────────────────
本メールは、自動配信にてお送りしております。
このメールにお心当たりのない方は、申し訳ありませんが、
そのむねを hoge@hogehoge.com までご連絡くださいませ。
───────────────────────────────────

このたびは、「送信者名」のホームページをご覧くださいまして、
誠にありがとうございます。

以下の内容で、お問い合わせ・資料請求フォームからの送信を受け付けました。
ご返答までに数日かかることがあります。
恐れながら、今しばらくお待ちくださいませ。

今後とも「送信者名」を、よろしくお願い申し上げます。

───────────────────────────────────
EOF;

//自動返信メール署名
$sign = <<<EOF
───────────────────────────────────
送信者名 Produced by 株式会社阪本工務店
http://www.hogehoge.com/
〒636-0246 奈良県磯城郡田原本町千代360-1
TEL:0744-34-3030
───────────────────────────────────
EOF;

//確認画面メッセージ
$explain = <<<EOF
以下の内容で送信されます。ご確認いただき、問題がなければ「送信」ボタンをクリックしてください。
修正箇所があれば、「入力画面に戻る」ボタンをクリックしてください。
EOF;

//確認画面エラーメッセージ
$explain_err = <<<EOF
入力内容に誤りがあります。<br />
<span style="color: #ff0000;">赤文字</span>が表示されている箇所は入力エラーです。
お手数ですが、「入力画面に戻る」ボタンをクリックし、再度入力してください。
EOF;

//送信結果メッセージ
$result = <<<EOF
一括資料請求をお申し込みくださり、ありがとうございました。近日中に、ご希望の資料を送付させて頂きます。
また、お送り頂いた内容を、自動送信メールにてお送りしておりますので、ご確認ください。
EOF;

//送信結果エラーメッセージ
$result_err = <<<EOF
メールの送信に失敗しました。お手数ですが、入力画面より再度入力をお願いいたします。
EOF;

//エラー内容のテキストカラー
$color = "#ff0000";

//必須項目エラーのテキスト
$absErr = "必須項目です。入力してください。";

//入力エラーのテキスト
$inputErr = "入力が正しくありません。";

//メールアドレス不一致エラーのテキスト
$mailErr = "メールアドレスが一致していません。";

//半角数字入力エラーのテキスト
$fontErr = "半角数字で入力してください。";

//文字コード
$charset = "utf-8";

//自動返信用メールアドレス
$remailAdd = "";

//フラグ
$errorflg = 0;

//フォーム項目
// 必須の場合は、「false」を「true」にする。
// テキストフォームの場合    "項目名" => output("項目ID","false"),
// メールフォームの場合    "項目名" => mailAdd("項目ID","true"),
// チェックボタンの場合    "項目名" => checkBox("項目ID","、","false"),
// ZIPをつなげるの場合    "項目名" => postcode("項目ID","項目ID","false"),
$form_post = array(
"プルダウン" => output("title","false"),
"テキストエリア" => output("comment","false"),
"チェック項目" => checkBox("check","、","false"),
"名前" => output("name","true"),
"郵便番号" => output("zip","false"),
"住所" => output("add","false"),
"電話番号" => output("tel","false"),
"Email" => mailAdd("email","","true"),
);


/////////////////////////////////////////////////////////////////////////////////
//外部コール

//確認結果表示
if($errorflg == 1) $explain = $explain_err;

//確認内容、エラー内容表示
function confirm() {
    foreach($GLOBALS['form_post'] as $key => $value) {
        $brValue = nl2br($value);
        echo "<tr>\n<th>".$key."</th>\n<td>".$brValue."</td>\n</tr>\n";
    }
}

//送信ボタンを表示、エラー処理後の全POSTを出力
function submit() {
    if($GLOBALS['errorflg'] == 1) {
        //echo "<a href='javascript:history.back()'><img src='".get_theme_url()."/images/btn_back.gif' alt='入力画面に戻る' title='入力画面に戻る'/></a>\n";
        echo "<input type='button' value='入力画面に戻る' alt='入力画面に戻る' onclick='history.back()' />";
    }
    else {
        $_SESSION['contents'] = $GLOBALS['form_post'];
        $_SESSION['mailflg'] = 1;
        $_SESSION['remailflg'] = $GLOBALS['remailflg'];
        $_SESSION['remail'] = $GLOBALS['remailAdd'];
        //echo "<a href='javascript:history.back()'><img src='".get_theme_url()."/images/btn_back.gif' alt='入力画面に戻る' title='入力画面に戻る'/></a>\n";
        //echo "<input type='hidden' name='submit' value='送信' />\n";
        //echo "<input type='image' name='image' src='".get_theme_url()."/images/btn_send.gif' alt='送信' title='送信' />\n";
        echo "<input type='button' value='入力画面に戻る' alt='入力画面に戻る' onclick='history.back()' /> <input type='submit' name='submit' value='送信' />";
    }
}

//送信結果表示、メールを送信、セッションを削除
function transmited() {
    if($_SESSION['remailflg'] == 1) $ret1 =remail();
    else $ret1 = 1;
    if($_SESSION['mailflg'] == 1) $ret2 = sendmail();
    if($ret1 && $ret2) echo $GLOBALS['result'];
    else echo $GLOBALS['result_err'];
    session_destroy();
}



/////////////////////////////////////////////////////////////////////////////////
//内部コール


//HTMLのエスケープ処理、バックスラッシュ処理
function escString($value) {
    if(get_magic_quotes_gpc() == 1) {
        $var = stripslashes($value);
        return htmlspecialchars($var, ENT_QUOTES);
    } else {
        return htmlspecialchars($value, ENT_QUOTES);
    }
}

//必須エラー(テキスト入力)
function absErr() {
    $GLOBALS['errorflg'] = 1;
    return "<span style='color:{$GLOBALS['color']};'>{$GLOBALS['absErr']}</span>";
}

//入力エラー
function inputErr() {
    $GLOBALS['errorflg'] = 1;
    return "<span style='padding-left: 15px; color:{$GLOBALS['color']};'>{$GLOBALS['inputErr']}</span>";
}

//メールエラー
function mailErr() {
    $GLOBALS['errorflg'] = 1;
    return "<span style='padding-left: 15px; color:{$GLOBALS['color']};'>{$GLOBALS['mailErr']}</span>";
}

//半角数字エラー
function fontErr() {
    $GLOBALS['errorflg'] = 1;
    return "<span style='padding-left: 15px; color:{$GLOBALS['color']};'>{$GLOBALS['fontErr']}</span>";
}


//名前入力結果を表示
function name($value1="", $value2="", $abs="true") {
    $post1 = $_POST[$value1];
    $post2 = $_POST[$value2];

    $str = $post1." ".$post2;
   
    if($post1 == "" && $post2 == "") {
        if($abs == "true") {
            return absErr();
        }
        else {
            return "";
        }
    }
    else if($post1 == "" || $post2 == "") {
        return escString($str).inputErr();
    }
    else {
        return escString($str);
    }
}

//生年月日(日付)を表示
function birthday($value1="", $value2="", $value3="", $abs="false", $sec0="") {
    $post1 = $_POST[$value1];
    $post2 = $_POST[$value2];
    $post3 = $_POST[$value3];
   
    $str = $sec0." ".$post1." 年".$post2." 月".$post3." 日";

    if($post1 == "" && $post2 == "" && $post3 == "") {
        if($abs == "true") {
            return absErr();
        }
        else {
            return "";
        }
    }
    else if($post1 == "" || $post2 == "" || $post3 == "") {
        return escString($str).inputErr();
    }
    else {
        return escString($str);
    }
}

//郵便番号入力結果を表示
function postcode($value1="", $value2="", $abs="false") {
    $post1 = $_POST[$value1];
    $post2 = $_POST[$value2];

    $str = $post1."-".$post2;

    if($post1 == "" && $post2 == "") {
        if($abs == "true") {
            return absErr();
        }
        else {
            return "";
        }
    }
    else if($post1 != "" && $post2 != "") {
        if(preg_match("/^[0-9]+$/", $post1) && preg_match("/^[0-9]+$/", $post2)) {
            if(preg_match("/^\d{3}$/", $post1) && preg_match("/^\d{4}$/", $post2)) {
                return escString($str);
            }
            else {
                return escString($str).inputErr();
            }
        }
        else {
            return escString($str).fontErr();
        }          
    }
    else {
        return escString($str).inputErr();
    }
}

//電話番号入力結果を表示
function tel($value1="", $value2="", $value3="", $abs="false") {
    $post1 = $_POST[$value1];
    $post2 = $_POST[$value2];
    $post3 = $_POST[$value3];

    $str = $post1."-".$post2."-".$post3;

    if($post1 == "" && $post2 == "" && $post3 == "") {
        if($abs == "true") {
            return absErr();
        }
        else {
            return "";
        }
    }
    else if($post1 != "" && $post2 != "" && $post3 != "") {
        if(preg_match("/^[0-9]+$/", $post1) && preg_match("/^[0-9]+$/", $post2) && preg_match("/^[0-9]+$/", $post3)) {
            if(preg_match("/^\d{2,4}$/", $post1) && preg_match("/^\d{2,4}$/", $post2) && preg_match("/^\d{3,4}$/", $post3)) {
                return escString($str);
            }
            else {
                return escString($str).inputErr();
            }
        }
        else {
            return escString($str).fontErr();
        }          
    }
    else {
        return escString($str).inputErr();
    }
}

//メールアドレス入力結果を表示
function mailAdd($value1="", $value2="", $abs="true") {
    $post1 = $_POST[$value1];
    $post2 = $_POST[$value2];

    if($post2 == "") {
        if($post1 == "") {
            if($abs == "true") {
                return absErr();
            }
            else {
                return "";
            }
        }
        else if($post1 != "") {
            if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $post1)) {
                $GLOBALS['remailAdd'] = escString($post1);
                return escString($post1);
            }
            else {
                return escString($post1).inputErr();
            }
        }
    }
    else {
        if($post1 == "") {
            if($abs == "true") {
                return absErr();
            }
            else {
                return "";
            }
        }
        else if($post1 != $post2) {
            return escString($post1).mailErr();
        }
        else if($post1 == $post2) {
            if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $post1)) {
                $GLOBALS['remailAdd'] = escString($post1);
                return escString($post1);
            }
            else {
                return escString($post1).inputErr();
            }
        }
    }
}

//チェックボックス入力結果を表示
function checkBox($value="", $sec0="", $abs="false") {
    $post1 = $_POST[$value];

    $connect = @implode($sec0,$post1);
    $str = $connect;

    if($post1 == "") {
        if($abs == "true") {
            return absErr();
        }
        else {
            return "";
        }
    }
    else {
        return escString($str);
    }
}

//汎用テキスト入力結果を表示
function output($value="", $abs="false", $sec0="", $sec1="") {
    $post1 = $_POST[$value];

    $str = $sec0.$post1.$sec1;

    if($post1 == "") {
        if($abs == "true") {
            return absErr();
        }
        else {
            return "";
        }
    }
    else {
        return escString($str);
    }
}

//input連結用(2個)入力結果を表示
function connect($value1="", $value2="", $abs="false", $sec0="", $sec1="", $sec2="") {
    $post1 = $_POST[$value1];
    $post2 = $_POST[$value2];

    $str = $sec0.$post1.$sec1.$post2.$sec2;

    if($post1 == "" && $post2 == "") {
        if($abs == "true") {
            return absErr();
        }
        else {
            return "";
        }
    }
    else if($post1 == "" || $post2 == "") {
        return escString($str).inputErr();
    }
    else {
        return escString($str);
    }
}

//input連結用(3個)入力結果を表示
function multi($value1="", $value2="", $value3="", $abs="false", $sec0="", $sec1="", $sec2="", $sec3="") {
    $post1 = $_POST[$value1];
    $post2 = $_POST[$value2];
    $post3 = $_POST[$value3];

    $str = $sec0.$post1.$sec1.$post2.$sec2.$post3.$sec3;

    if($post1 == "" && $post2 == "" && $post3 == "") {
        if($abs == "true") {
            return absErr();
        }
        else {
            return "";
        }
    }
    else if($post1 == "" || $post2 == "" || $post3 == "") {
        return escString($str).inputErr();
    }
    else {
        return escString($str);
    }
}

//管理者受信メール内容
function sendmail() {
    mb_language("japanese");
    mb_internal_encoding($GLOBALS['charset']);

    $to = $GLOBALS['myMail'];
    $subject = $GLOBALS['mailTitle'];
    $header = "From:".mb_encode_mimeheader($GLOBALS['sender'])."<".$GLOBALS['senderMail'].">";

    $message = $GLOBALS['mailText']."\n\n";
    foreach($_SESSION['contents'] as $key => $value) {
        $message .= escString($key). " : " .$value."\n\n";
    }
    $message .= "\n\n送信日時 : ".date( "Y/m/d (D) H:i:s", time() )."\n";
    $message .= "ホスト名 : ".getHostByAddr(getenv('REMOTE_ADDR'))."\n\n";

    $encMsg = mb_convert_kana($message, "rnasK", $GLOBALS['charset']);

    return mb_send_mail($to, $subject, $encMsg, $header);
}

//自動返信メール内容
function remail() {
    mb_language("japanese");
    mb_internal_encoding($GLOBALS['charset']);
   
    $to = $_SESSION['remail'];
    $subject = $GLOBALS['remailTitle'];
    $header = "From:".mb_encode_mimeheader($GLOBALS['sender'])."<".$GLOBALS['senderMail'].">";

    $message = $GLOBALS['remailText']."\n\n";
    foreach($_SESSION["contents"] as $key => $value) {
        $message .= escString($key). " : " .$value."\n\n";
    }
    $message .= "\n\n".$GLOBALS['sign'];

    $encMsg = mb_convert_kana($message, "rnasK", $GLOBALS['charset']);
   
    return mb_send_mail($to, $subject, $encMsg, $header);
}



?>

ドメインって、早い者勝ちですよ!