phpな休日 BBS sitemap
1. 複数ファイル添付メールフォームの概要

複数ファイルを添付出来るメールフォームです。
BBSでご要望がありましたので作ってみました。元になっているのは「30.ファイル添付メールフォーム」です。

添付ファイル処理の流れとしてはBBSにも書いたように
    バウンダリー開始 --$boundary
    添付ファイル
    バウンダリー開始 --$boundary
    添付ファイル
    バウンダリー開始 --$boundary
    添付ファイル
    バウンダリー終了 --$boundary--
としていけば、いくつでも添付できるのですが、問題は複数ファイルとなるとどうしても容量オーバーになる可能性が高いこと。
添付容量が post_max_size を超えた場合の処理を付け足しました。
ただし、添付容量×1.3 < post_max_size と、余裕をもって容量を設定してください。
あるいは、サーバの制限により post_max_size よりもメール受信可能容量のほうが小さい場合もあります。その場合も $pmsize×1.3=受信可能容量 で計算して、控えめに設定していただけると、エラーなく送信できます。

無事に送信が完了した場合、送信した内容を表示するようにしました。(添付はファイル名とファイルサイズ、合計サイズを表示)

2. 送信フォーム

送信フォームは
    <input type="file" name="attach[]"><br />
    <input type="file" name="attach[]"><br />
    <input type="file" name="attach[]"><br />
          ・
          ・
          ・
と、10個並べましたが、受け取り時は、配列の数だけforで回していますで、任意に減らしても増やしても対応できます。

3. スクリプト(ppmail.php)

スクリプトは ppmail.php 1つだけです。ファイル名を変更する場合は、スクリプト中の ppmail.php も書きかえてください。

<?php
mb_language("ja");
mb_internal_encoding("UTF-8");
header("Content-Type:text/html; charset=UTF-8");
if ((isset($_GET["message"])) && ($_GET["message"] == 1)){$message = "<br />  <font color=¥"#ff0000¥">エラー</font> -添付ファイル容量が大きすぎます<br />";}
$pmsize = 1572864; //post_max_sizeが2Mの場合バイト単位で(1.5MB×1024×1024)
if ($pmsize < $_SERVER["CONTENT_LENGTH"]) { //ポストされた量がpost_max_size を超えている場合
header("Location: http://hogehoge.net/ppmail.php?message=1"); //エラー表示へ(アップロードした場所に書き換えてください)
exit; //終了する
}
$boundary = md5(uniqid(rand(),1)); //境界バウンダリー文字生成
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>p-ho 複数添付メールフォーム</title>
</head>
<body>
<?php
if (isset($message)) {print "<font color=¥"#ff0000¥">" . $message . "</font><br /><br />¥n";}
if(isset($_POST["name"])){ $name = $_POST["name"]; //POSTに値があれば処理
if( get_magic_quotes_gpc() ) { $name = stripslashes("$name"); } //クォートをエスケープする
$name = htmlspecialchars ($name); //HTMLタグ禁止
$viewname = $name; //送信後のHTML表示用
}
if(isset($_POST["email"])){ $email = $_POST["email"]; //POSTに値があれば処理
if (!preg_match('/^[a-zA-Z0-9_¥.¥-]+?@[A-Za-z0-9_¥.¥-]+$/',$email)) { //メールアドレスでなければ
$email = ""; //変数 $email を空にする
}
}
if(isset($_POST["title"])){ $title = $_POST["title"]; //POSTに値があれば処理
if( get_magic_quotes_gpc() ) { $title = stripslashes("$title"); } //クォートをエスケープする
$title = htmlspecialchars ($title); //HTMLタグ禁止
$viewtitle = $title; //送信後のHTML表示用
}
if(isset($_POST["comment"])){ $comment = $_POST["comment"]; //POSTに値があれば処理
if( get_magic_quotes_gpc() ) { $comment = stripslashes("$comment"); } //クォートをエスケープする
$comment = str_replace("¥r¥n", "¥r", $comment); //Windowsの改行コードを置き換え
$comment = str_replace("¥r", "¥n", $comment); //Machintoshの改行コードを置き換え
$comment = htmlspecialchars ($comment); //HTMLタグ禁止
$viewcomment = str_replace("¥n", "<br />", $comment); //¥nを<br />に変換(表示用)
}
$time = date("Y/n/j G:i"); //日時の取得
$ip = getenv("REMOTE_ADDR"); //IPアドレス取得
$host = gethostbyaddr(getenv("REMOTE_ADDR")); //HOST取得
$iphost = ($host . "/" . $ip);
if(($_POST["name"] != "") && ($_POST["email"] != "") && ($_POST["title"] != "") && ($_POST["comment"] != "")&& ($email != "")) { //全てに入力があってメールアドレスが正しい形式なら
$body = "";
$header = "From: $email¥n";
$header .= "X-Mailer: PHP/".phpversion()."¥n";
$header .= "MIME-version: 1.0¥n";
if (!$_FILES) { //添付がない場合
$header .= "Content-Type: text/plain; charset=ISO-2022-JP¥n";
$header .= "Content-Transfer-Encoding: 7bit¥n";
}
else{ //添付がある場合
$header .= "Content-Type: multipart/mixed;¥n";
$header .= "¥tboundary=¥"$boundary¥"¥n";
$body .= "This is a multi-part message in MIME format.¥n¥n";
$body .= "--$boundary¥n";
$body .= "Content-Type: text/plain; charset=ISO-2022-JP¥n";
$body .= "Content-Transfer-Encoding: 7bit¥n¥n";
}
$name = mb_convert_encoding($name, "JIS", "UTF-8"); //JISに変換
$body .= $name . "¥n";
$body .= $email . "¥n¥n";
$title = mb_convert_encoding($title, "JIS", "UTF-8"); //JISに変換
$body .= $title . "¥n¥n";
$comment = mb_convert_encoding($comment, "JIS", "UTF-8"); //JISに変換
$body .= $comment . "¥n¥n";
$body .= $iphost . "¥n";
$body .= $time . "¥n";
$message = "";
$attachsize = 0;
for ($i=0;$i<count($_FILES["attach"]["tmp_name"]);$i++) {
if ($_FILES["attach"]["tmp_name"][$i]) { //添付がある場合
$upattach = $_FILES["attach"]["tmp_name"][$i];
$upattachname = $_FILES["attach"]["name"][$i];
$upattachtype = $_FILES["attach"]["type"][$i];
$upattachsize = $_FILES["attach"]["size"][$i];
$attachsize = $attachsize + $upattachsize;
$fp = fopen($_FILES["attach"]["tmp_name"][$i], "r");
$attachcon = fread($fp, filesize($_FILES["attach"]["tmp_name"][$i]));
fclose($fp);
$message .= "添付 | " . $upattachname . " | " . $upattachsize . "byte / 合計 " . $attachsize . "byte<br />¥n";
$attachenco = chunk_split(base64_encode($attachcon)); //エンコードして分割
$body .= "--$boundary¥n";
$body .= "Content-Type: " . $upattachtype . ";¥n";
$body .= "¥tname=¥"$upattachname¥"¥n";
$body .= "Content-Transfer-Encoding: base64¥n";
$body .= "Content-Disposition: attachment;¥n";
$body .= "¥tfilename=¥"$upattachname¥"¥n¥n";
$body .= "$attachenco¥n";
}
}
$body .= "--$boundary--¥n¥n";
$body = mb_convert_encoding($body, "JIS", "UTF-8"); //JISに変換
if (mail('hoge@hoge.net', 'p-ho PluralAttachMailForm', $body, $header)) { //送信先メールアドレスは変更してください。
print "以下の内容で送信しました<br /><br />¥n";
print "【宛先】PHPな休日管理人(haku)<br />¥n";
print "【件名】p-ho PluralAttachMailForm<br />¥n";
print "【お名前】&nbsp;" . $viewname . "<br />¥n";
print "【E-mail】&nbsp;" . $email . "<br />¥n";
print "【タイトル】&nbsp;" . $viewtitle . "<br />¥n";
print "【コメント】&nbsp;<br />¥n";
print $viewcomment . "<br /><br />¥n";
print $iphost . "<br />¥n";
print $time . "<br />¥n";
if ((isset($message)) && (!$message == "")) {
print "【添付ファイル】&nbsp;<br />¥n";
print $message . ";<br />¥n";
}
print "※[送信]ボタンの2度押し、 [再読込] はしないでください。<br />¥n";
}
}
else { //入力漏れの場合
print "<b>p-ho 複数添付 MailForm</b><br />¥n";
print "<font size=¥"2¥" color=¥"#808080¥"> ※全ての項目に入力必要</font><br />¥n";
print "<form action=¥"./ppmail.php¥" method=¥"POST¥" enctype=¥"multipart/form-data¥">¥n";
print "お名前<br />¥n";
if ($_POST["name"] != "") {
print "<input name=¥"name¥" value=¥"" . $name . "¥"><br />¥n";
}
else {
print "<input name=¥"name¥"><br />¥n";
}
print "E-mail<br />¥n";
if ($_POST["email"] != "") {
print "<input name=¥"email¥" value=¥"" . $email . "¥"><br />¥n";
}
else {
print "<input name=¥"email¥"><br />¥n";
}
print "タイトル<br />¥n";
if ($_POST["title"] != "") {
print "<input name=¥"title¥" value='" . $title . "¥"><br />¥n";
}
else {
print "<input name=¥"title¥"><br />¥n";
}
print "コメント<br />¥n";
if ($_POST["comment"] != "") {
print "<textarea name=¥"comment¥">" . $comment . "</textarea><br />¥n";
}
else {
print "<textarea name=¥"comment¥"></textarea><br />¥n";
}
print "<font size=¥"2¥" color=¥"#808080¥"> ※添付容量の合計は1.5MB以内で</font><br />¥n";
print "<input type=¥"file¥" name=¥"attach[]¥"><br />¥n";
print "<input type=¥"file¥" name=¥"attach[]¥"><br />¥n";
print "<input type=¥"file¥" name=¥"attach[]¥"><br />¥n";
print "<input type=¥"file¥" name=¥"attach[]¥"><br />¥n";
print "<input type=¥"file¥" name=¥"attach[]¥"><br />¥n";
print "<input type=¥"file¥" name=¥"attach[]¥"><br />¥n";
print "<input type=¥"file¥" name=¥"attach[]¥"><br />¥n";
print "<input type=¥"file¥" name=¥"attach[]¥"><br />¥n";
print "<input type=¥"file¥" name=¥"attach[]¥"><br />¥n";
print "<input type=¥"file¥" name=¥"attach[]¥"><br />¥n";
print "<input type=¥"submit¥" value=¥"送信¥"></form>¥n";
}
?>
</body>
</html>

 

サブウインドウで実行 //実際にはメール送信されません。

この複数添付メールフォームを こちら からzip形式でダウンロードできるようにしました。 お役立てください。


Fatal error: Uncaught Error: Undefined constant "Y.n.j" in /home/users/2/secret.jp-final/web/phpholiday/38/index.cgi:222 Stack trace: #0 /home/users/2/secret.jp-final/web/phpholiday/index.php(334): include() #1 {main} thrown in /home/users/2/secret.jp-final/web/phpholiday/38/index.cgi on line 222