Mail form

"mail.php"ファイル(CGI)。
まず、太字部分4ヶ所を、各自の設定に変更。
JavaScriptによる入力チェックを利用する際は、色分け部分を削除。
(文末の閉じ括弧、"}"も忘れずに...)


<?

//mb_language("Ja");
//mb_internal_encoding("EUC-JP") ;

$name = "april snow";//管理者名。半角英数字、
$to = "signinafragrance@snow.odn.ne.jp";//管理者のメールアドレス。

$title1 = "mail from april snow";//管理者宛のメールのタイトル。日本語可、
$title2 = "thanx 4 ur message!";//送り先のメール(複写分)のタイトル。日本語可、

$title1 = mb_convert_encoding($title1,"EUC-JP","auto");
$title2 = mb_convert_encoding($title2,"EUC-JP","auto");

$author = $_POST['author'];
$email = $_POST['email'];
$url = $_POST['url'];
$cc = $_POST['cc'];
$text = $_POST['text'];

if(get_magic_quotes_gpc()){
$author = stripslashes($author);
$email = stripslashes($email);
$url = stripslashes($url);
$text = stripslashes($text);
}

$author = sprintf("%s",$author);
$email = sprintf("%s",$email);
$url = sprintf("%s",$url);
$text = sprintf("%s",$text);

$TM = date("Y/m/d(D) H:i:s");
$PX = $_SERVER['HTTP_FORWARDED'];
$UA = $_SERVER['HTTP_USER_AGENT'];
$RA = $_SERVER['REMOTE_ADDR'];

if($TM == ""){$TM = "undefined";}
if($PX == ""){$PX = "none";}
if($UA == ""){$UA = "undefined";}
if($RA == ""){$RA = "undefined";}

$error = "";

if($author == ""){$error .= "please input your name.\r\n";}
if($email == ""){$error .= "please input your e-mail address.\r\n";}
if($text == ""){$error .= "please input your message.\r\n";}

if(strlen($author) > 50){$error .= "sorry,name is restricted to 25 characters with 2 byte value.\r\n";}
if(strlen($email) > 100){$error .= "sorry,e-mail is restricted to 50 characters with 2 byte value.\r\n";}
if(strlen($url) > 100){$error .= "sorry,url is restricted to 50 characters with 2 byte value.\r\n";}
if(strlen($text) > 2000){$error .= "sorry,message is restricted to 1000 characters with 2 byte value.\r\n";}

/*メールアドレスのチェック(正規表現のみ)
if($email != ""){
$format1 = "^[^@]+@[^@]+[^@]+$";
if(!ereg($format1,$email)){$error .= "e-mail address is incorrect.\n";}
}
*/

if($email != ""){
$format1 = "^[^@]+@[^@]+[^@]+$";
if(!ereg($format1,$email)){
$error .= "e-mail address is incorrect.\n";
}else{
$at = split("@",$email);
if(strlen($at[0]) < 2){
$error .= "e-mail address is incorrect.\n";
}else{
if(strpos($at[1],".") == false){
$error .= "e-mail address is incorrect.\n";
}else{
$dot = split("\.",$at[1]);
$cnt = count($dot);
if($cnt < 2){
$error .= "e-mail address is incorrect.\n";
}else{
for($i=0; $i<$cnt; $i++){
if(strlen($dot[$i]) < 2){
$error = "e-mail address is incorrect.\n";
}
}
}
}
}
}
}

/*URLのチェック(正規表現のみ)
if($url != ""){
$format2 = "(https?|ftp):\/\/.+";
if(!ereg($format2,$url)){$error .= "url is incorrect.\n";}
}else{
$url = "none";
}
*/

if($url != ""){
$format2 = "(https?|ftp):\/\/.+";
if(!ereg($format2,$url)){
$error .= "url is incorrect.\n";
}else{
if(strpos($url,".") == false){
$error .= "url is incorrect.\n";
}else{
$dot = split("\.",$url);
$cnt = count($dot);
if($cnt < 2){
$error .= "url is incorrect.\n";
}else{
for($i=0; $i<$cnt; $i++){
if(strlen($dot[$i]) < 2){
$error .= "url is incorrect.\n";
}
}
}
}
}
}else{
$url = "none";
}

if($error != ""){
echo "<font color=\"red\">Correct errors,if any.</font>";
echo "<pre>$error</pre>";
exit;
}else{

//管理者宛のメッセージには、アクセスログを追記。
$message1 = "";
$message1 .= "name : " . $author . "\r\n";
$message1 .= "e-mail : " . $email . "\r\n";
$message1 .= "url : " . $url . "\r\n";
$message1 .= "message : " . "\r\n" . $text . "\r\n\r\n";
$message1 .= "date : " . $TM . "\r\n";
$message1 .= "proxy : " . $PX . "\r\n";
$message1 .= "user agent : " . $UA . "\r\n";
$message1 .= "remote address : " . $RA;

//送り先のメッセージには、アクセスログを控除。
$message2 = "";
$message2 .= "name : " . $author . "\r\n";
$message2 .= "e-mail : " . $email . "\r\n";
$message2 .= "url : " . $url . "\r\n";
$message2 .= "message : " . "\r\n" . $text;

$message1 = mb_convert_encoding($message1,"EUC-JP","auto");
$message2 = mb_convert_encoding($message2,"EUC-JP","auto");

//ヘッダー。URLのみ表示、
//$header1 = "From:" . $email . "\r\n" . "Reply-To:" . $email . "\r\n";
//$header2 = "From:" . $to . "\r\n" . "Reply-To:" . $to . "\r\n";

//ヘッダー。"$name"は、半角英数字のみ有効(標準仕様)。
$header1 = "From:" . $email . "\r\n" . "Reply-To:" . $email . "\r\n";
$header2 = "From:" . $name . "<" . $to . ">" . "\r\n" . "Reply-To:" . $name . "<" . $to . ">" . "\r\n";

//ヘッダー。"$name"は、半角英数字のみ有効("$author"に、文字化けの可能性あり)。
//$header1 = "From:" . $author . "<" . $email . ">" . "\r\n" . "Reply-To:" . $author . "<" . $email . ">" . "\r\n";
//$header2 = "From:" . $name . "<" . $to . ">" . "\r\n" . "Reply-To:" . $name . "<" . $to . ">" . "\r\n";

//ヘッダー。"$name"の日本語化。おそらくMacでは、文字化け必至...
//$header1 = "From:" . mb_encode_mimeheader($author) . "<" . $email . ">" . "\r\n" . "Reply-To:" . mb_encode_mimeheader($author) . "<" . $email . ">" . "\r\n";
//$header2 = "From:" . mb_encode_mimeheader("草森冬弥") . "<" . $to . ">" . "\r\n" . "Reply-To:" . mb_encode_mimeheader("草森冬弥") . "<" . $to . ">" . "\r\n";

if($cc != cc){
mb_send_mail($to,$title1,$message1,$header1);
}else{
mb_send_mail($to,$title1,$message1,$header1);
mb_send_mail($email,$title2,$message2,$header2);
}

//Webサーバーに保存するログの種類。
//1年ごと($Y)、1月ごと($M)、1日ごと($D)、1件ごと($S),全件($F),
//"$H = fopen($F,"a") ... "の$Fの部分を変更。デフォルトは全件。
$Y = date("Y");
$M = date("Ym");
$D = date("Ymd");
$S = date("YmdHis");

$Y = sprintf("./data/%s.txt",$Y);
$M = sprintf("./data/%s.txt",$M);
$D = sprintf("./data/%s.txt",$D);
$S = sprintf("./data/%s.txt",$S);

//フォルダ"data"を作成。パーミッションは、777、
//要アクセス制限。パスワード保護(.htaccess,.htpasswd)ほか、
$F = "./data/mail.txt";

$H = fopen($F,"a") or die("error is occurred while opening file.");
flock($H,LOCK_EX);
$L = sprintf("%s,%s,%s,%s,%s,%s,%s,%s\r\n",$author,$email,$url,$text,$TM,$PX,$UA,$RA);
$L = mb_convert_encoding($L,"Shift_JIS","auto");
fputs($H,$L);
flock($H,LOCK_UN);
fclose($H);

//完了画面へリダイレクト。
header("Location:maildone.php");
exit;

}

?>


"mailform.html"ファイル(入力フォーム)。
PHPによる入力チェックを利用する際は、送信ボタンのタイプを、"submit"に変更。
また、JavaScript部分を全て削除(文末の、"onclick="mail()""も忘れずに...)。


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>april snow</title>

<script Language="JavaScript"><!--
function mail(){
if(confirm("are you sure?")){
var obj1 = document.getElementById("author");
var str1 = obj1.value;

var obj2 = document.getElementById("email");
var str2 = obj2.value;

var obj3 = document.getElementById("url");
var str3 = obj3.value;

var obj4 = document.getElementById("text");
var str4 = obj4.value;

var ans1 = str2.match(/^[^@]+@[^@]+[^@]+$/);
var ans2 = str3.match(/(https?|ftp):\/\/.+/);

if((str1 == "") || (str2 == "") || (str4 == "")){
alert("please input blank except url.");
}else{
if(!ans1 && ((str3 != "") && !ans2)){
alert("incorrect email & url!");
}else if(!ans1){
alert("incorrect email!");
}else if((str3 != "") && !ans2){
alert("incorrect url!");
}else{

var at = str2.split("@");
if(at[0].length < 2){
alert("incorrect email!");
return false;
}
if(at[1].indexOf(".") == -1){
alert("incorrect email!");
return false;
}
var dot = at[1].split(".");
if(dot.length < 2){
alert("incorrect email!");
return false;
}
for(i=0; i<dot.length; i++){
if(dot[i].length < 2){
alert("incorrect email!");
return false;
}
}

if(str3 != ""){
if(str3.indexOf(".") == -1){
alert("incorrect url!");
return false;
}
var dot = str3.split(".");
if(dot.length < 2){
alert("incorrect url");
return false;
}
for(i=0; i<dot.length; i++){
if(dot[i].length < 2){
alert("incorrect url!");
return false;
}
}
}

document.comments_form.submit();

}
}

}
}

/*
//onblur="N()"
function N(){
var obj = document.getElementById("author");
var str = obj.value;
if(str == ""){
alert("please input name!");
}
}
*/

/*
//onblur="M()"
function M(){
var obj = document.getElementById("email");
var str = obj.value;
if(str == ""){
alert("please input e-mail!");
}else{
var ans = str.match(/^[^@]+@[^@]+[^@]+$/);
if(!ans){
alert("incorrect email!");
}
}
}
*/

/*
//onblur="U()"
function U(){
var obj = document.getElementById("url");
var str = obj.value;
var ans = str.match(/(https?|ftp):\/\/.+/);
if(str != ""){
if(!ans){
alert("incorrect url!");
}
}
}
*/

/*
//onblur="T()"
function T(){
var obj = document.getElementById("text");
var str = obj.value;
if(str == ""){
alert("please input message!");
}
}
*/

//--></script>

</head>

<body>

<h2>Mail</h2>

<div>
<form method="post" action="mail.php" name="comments_form">

<p>*name :<br />
<input tabindex="1" id="author" name="author" />
</p>

<p>*e-mail address :<br />
<input tabindex="2" id="email" name="email" />
</p>

<p>*url :<br />
<input tabindex="3" type="text" name="url" id="url" /><br />
</p>

<p>
<input type="checkbox" id="cc" name="cc" value="cc" checked />&nbsp;send you Cc?
</p>

<p>*comment<br />

<textarea tabindex="4" id="text" name="text" rows="8" cols="24"></textarea></p>

<input style="font-weight: bold;" type="button" name="post" tabindex="5" value="&nbsp;Post&nbsp;" onclick="mail()" />

</form>
</div>

</body>
</html>


"maildone.html"ファイル(送信完了画面)。
管理者名、および送信完了後のメッセージを変更。


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>april snow</title>
</head>

<body>

<h2>Mail</h2>

<div>
<form method="post" action="mail.php" name="comments_form">

<p>*name :<br />
<input tabindex="1" id="author" name="author" value="april snow" readonly="readonly" />
</p>

<p>*e-mail address :<br />
<input tabindex="2" id="email" name="email" readonly="readonly" />
</p>

<p>*url :<br />
<input tabindex="3" type="text" name="url" id="url" readonly="readonly" />
</p>

<p>
<input type="checkbox" id="cc" name="cc" value="cc" checked />&nbsp;send you Cc?
</p>

<p>*message<br />

<textarea tabindex="4" id="text" name="text" rows="8" cols="24" readonly="readonly">
thanx 4 ur message!
</textarea></p>

<input style="font-weight: bold;" type="submit" name="post" tabindex="5" 
value="&nbsp;Post&nbsp;" disabled="disabled" />

</form>
</div>

</body>
</html>


".htpasswd"ファイル。
まず、"htpasswd.txt"に、任意のパスワードを設定。
cf.http://www.futomi.com/lecture/htaccess/htpasswd.html


パスワード:暗号化されたパスワード


".htaccess"ファイル。
4行目の太字部分を、"htpasswd.txt"までのフルパス(≠絶対パス)に変更。


AuthType Basic
AuthName "members only"
AuthGroupFile /dev/null
AuthUserFile /home/.htpasswd
require valid-user
<Files ~ "^.(htpasswd|htaccess)$">
	deny from all
</Files>



ログ保存用に、新規に"data"フォルダを作成。
"htaccess.txt"、"htpasswd.txt"ファイルを格納。

全ファイル、フォルダ(全6点)を、同一ディレクトリにアップロード。
"data"フォルダのパーミッションを、777に、
"htaccess.txt"を、".htaccess"に、
"htpasswd.txt"を、".htpasswd"に、


Download