ファイルのアップロードからそれを添付してメールを送るスケルトン

  upload1.php



OneDrive へ移動



<?
require_once("common.php");

require_once("html_head.php");

print "<pre>";
print_r( $_FILES );
print "</pre>";

$target = $_FILES['file_1']['name'];
$_SESSION['mime'] = $_FILES['file_1']['type'];

$upload_dir = "./upload_file/";
$upload = $upload_dir;
$upload .= $target;
print $upload . "<br>";

if ( move_uploaded_file( $_FILES['file_1']['tmp_name'], $upload ) ) {
	print "アップロードに成功しました<br>";
	$_SESSION["mail_file"] = $target;
	print "<script>";
	print " parent.parent.$('#mail_file').val( \"{$target}\" );";
	print "</script>";
}
else {
	print "アップロードに失敗しました<br>";
}

// 5分以上経過したファイルの削除
$dir_handle = @opendir("./upload_file/");
if ( $dir_handle ) {
	$del_target = readdir( $dir_handle );
	while( $del_target !== false ) {

		if ( $del_target != "." && $del_target != ".." ) {
			print "{$del_target} をチェックします<br>";
			$astamp = stat($upload_dir . $del_target);
			$laststamp = $astamp[9];
			if ( $laststamp < time() - 300 ) {
				print "{$del_target} を削除します<br>";
				unlink( $upload_dir . $del_target );
			}
		}
		$del_target = readdir( $dir_handle );

	}
	
	closedir( $dir_handle );
}

print "</body>";
print "</html>";
?>








  mail.php



<?php
require_once("common.php");

$_SESSION['user'] = "sworc";
$_SESSION["mail_from"] = "lightbox@winofsql.sakura.ne.jp";

require_once("mail_view.php");
?>


common.php

<?php
session_start();

header( "Content-Type: text/html; Charset=utf-8" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );
?>


debug_value.php

<?php
print "<pre style='clear:left;font-size:20px;padding:20px;border:solid 2px #DE4600;'>";
print_r($_GET);
print_r($_POST);
print_r($_SESSION);
print "</pre>";
?>




  mail_view.php

<!doctype html>
<html lang="ja">
<head>
<title>sendmail</title>
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<script>

$(function(){
	$("#open_button").click(function () {
		$("#result").show();
	});
});
$(function(){
	$("#close_button").click(function () {
		$("#result").hide();
	});
});

</script>


<link type="text/css" rel="stylesheet" href="style.css">
<style>
body {
	margin: 20px;
}
textarea {
	width:600px;
	height:200px;
}
.input input {
	width:400px;
}

</style>
</head>

<!-- 画面表示部分 -->
<body>

<!-- フォーム開始 -->
<form method="post" action="mail_ex.php">
<div class="input">
宛先 <input type="text" name="to_address"><br>
件名 <input type="text" name="subj"><br>
</div>
<!-- テキストエリア -->
<textarea name="text"></textarea>
<br>
<!-- 送信ボタン -->
<input
	type="submit"
	name="send"
	value="送信"
	style='width:100px;'
>
</form>
<!-- フォーム終わり -->

<input
	type="button"
	id="close_button"
	value="閉じる"
	style='width:100px;'
>
<input
	type="button"
	id="open_button"
	value="開く"
	style='width:100px;'
>

<!-- アップロードするファイル名を JavaScript で表示 -->
アップロードしたファイル名 => 
<input
	type="text"
	id="mail_file"
	value="<?= $_SESSION["mail_file"] ?>"
	readonly
>


<br><br>
現在のセッション ID => <?= session_id() ?>
<br>
<!-- ファイルアップロード用の別ページ -->
<iframe
	src="form2.php"
	id="result"
	name="result"
	frameborder="1"
	scrolling="yes"
	style='width:600px;height:450px;display:none;'
></iframe>


<?php require_once("debug_value.php") ?>


</body>
</html>




  form2.php

<?php
require_once("common.php");

require_once("form2_view.php");
?>




  form2_view.php

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>初心者用ファイルアップロード用テンプレート</title>

<script>
function resetIframe() {
	var ifdoc = document.getElementById("result2").contentWindow.document;
	ifdoc.write("");
	ifdoc.close();
}
</script>

<style>
body {
	background-color: #D6C8A1;
	font-family: Arial, Helvetica, Verdana, "ヒラギノ角ゴPro W3", "Hiragino Kaku Gothic Pro", Osaka, "メイリオ", Meiryo, "MS Pゴシック", sans-serif;
	font-size:18px;
}
iframe {
	border: solid 1px; #000000;
	background-color: #ffffff;
}
</style>
</head>
<body>

<!-- アップロード【1】 -->
<form
	enctype="multipart/form-data"
	action="upload1.php"
	method="POST"
	target="result2"
	style='display:inline;'
>
	<input type="hidden" name="MAX_FILE_SIZE" value="100000">
	<input
		type="file"
		id="file_1"
		name="file_1"
		size="40"
	/>
	<br>
	<input
		type="submit"
		value="単フィールド単ファイルアップロード"
	/>
	<br />
	<br />
</form>
<hr>
<iframe
	id="result2"
	name="result2"
	frameborder="1"
	scrolling="yes"
	style='width:100%;height:700px;'
></iframe>

</body>
</html>




  mail_ex.php ( アップロードされたファイルを添付してメール送信 )

<?php
require_once("common.php");

require_once("html_head.php");

foreach( $_POST as $Key => $Value ) {
	$_POST[$Key] = str_replace("\\\\", "\\", $Value );
	$_POST[$Key] = str_replace("\\'", "'", $_POST[$Key] );
	$_POST[$Key] = str_replace("\\\"", "\"", $_POST[$Key] );
}


mb_language( "ja" );
mb_internal_encoding("utf-8");

$from = $_SESSION["mail_from"];
$fname = $_SESSION["mail_file"];
$mime = $_SESSION['mime'];

if ( $fname == "" ) {
	print "ファイルをアップロードして下さい <a href=\"mail.php\">戻る</a><br>";
	exit();
}

$uniqid = uniqid();

$headers = "From: {$from}\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"{$uniqid}\"\n";

$body  = "--{$uniqid}\n";
$body .= "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n";
$body .= "\n";
$body .= $_POST['text'] . "\n";
$body .= "--{$uniqid}\n";
$body .= "Content-Type: {$mime}; name=\"{$fname}\"\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "Content-Disposition: attachment; filename=\"{$fname}\"\n";
$body .= "\n";

$path = "upload_file/{$fname}";
$data = file_get_contents($path);
$encode = base64_encode($data);

$body .= chunk_split($encode);
$body .= "\n--{$uniqid}--\n";

$ret = mb_send_mail(
	$_POST['to_address'],
	$_POST['subj'],
	$body,
	$headers );

// アップロードして添付したファイルを削除
unlink($path);
$_SESSION["mail_file"] = "";

if ( $ret === TRUE ) {
	print "<a href=\"mail.php\">戻る</a> メール送信が終了しました。<br>";
}
else {
	print "<a href=\"mail.php\">戻る</a> メール送信に失敗しました。<br>";
}

print "</body>";
print "</html>";
?>














   SQLの窓    create:2013/10/10  update:2018/02/18   管理者用(要ログイン)





フリーフォントWEBサービス

SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ