1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

複数添付メール/メモ

Last updated at Posted at 2016-07-05

どこかのメモを流用して作成。
一部コードの出所はわかりません。

confirm.php
<?php

$email= $_POST['メールアドレス'];
$input01= $_POST['自由'];
$fname01= $_POST['添付1'];
$fname02= $_POST['添付2'];
$file01= "files/".$_FILES['upload1']["name"];
$file02= "files/".$_FILES['upload2']["name"];
var_dump($file01);

$count = 1;
while ($count < 3){
if (is_uploaded_file($_FILES["upload".$count]["tmp_name"])) {
  if (move_uploaded_file($_FILES["upload".$count]["tmp_name"], "files/" . $_FILES["upload".$count]["name"])) {
    chmod("files/" . $_FILES["upload".$count]["name"], 0644);
    echo $_FILES["upload".$count]["name"] . "をアップロードしました。";
  } else {
    echo "ファイルをアップロードできません。";
  }
} else {
  echo "ファイルが選択されていません。";
}
  $count++;
}

?>
<body>
<form method="post" action="main.php" enctype="multipart/form-data">

 
          <input type="hidden"  name="fname01" value="<?php echo($fname01); ?>">
          <input type="hidden"  name="file01" value="<?php echo($file01); ?>">
       
          <input type="hidden"  name="fname02" value="<?php echo($fname02); ?>">
          <input type="hidden"  name="file02" value="<?php echo($file02); ?>">
         
 
<input type="button" value="内容を修正する" onclick="history.back(-1)">
        <button type="submit" name="submit">送信する</button>
</form>
</body>
main.php
<?php

$email= $_POST['email'];

//$file1="files/".$_FILES["upload1"]["name"];
//$file2="files/".$_FILES["upload2"]["name"];
$file01 = $_POST['file01'];
$file02 = $_POST['file02'];
$fname01 = $_POST['fname01'];
$fname02 = $_POST['fname02'];

$attach_file01 = $file01;
$attach_file02 = $file02;

$to[] = 'testtest2@yahoo.co.jp';
$from_email= $email;
$from_name = '送信者名';
$subject = 'メールのタイトル';

$body = "

このメールは、自動で送信されるメールです。

資料を送信致します。
添付ファイルをご参考して下さい。

以上です。
";



// send the email
foreach( $to as $to_email ) {
sendmail_jpn($to_email, $subject, $body, $from_email,$from_name, $attach_file01, $attach_file02,$fname01,$fname02);
sleep(1);
}
echo ($attach_file01);
echo ($attach_file02);
echo "送信完了";

//関数-----------------------------------

function sendmail_jpn($to, $subject, $message, $from_email,$from_name, $filepath01, $filepath02,$fname01,$fname02)
{
$mime_type = "application/octet-stream";

// 添付ファイルのエンコード
//$filename01 = basename($filepath01);
//$filename02 = basename($filepath02);

$filename01 = $fname01;
$filename02 = $fname02;


// マルチパートなので、パートの区切り文字列を指定
$boundary = '----=_Boundary_' . uniqid(rand(1000,9999) . '_') . '_';

// 件名のエンコード
$subject = mb_convert_encoding($subject, 'ISO-2022-JP', 'auto');
$subject = mb_encode_mimeheader_ex($subject);

// 本文のエンコード
$message = mb_convert_encoding($message, 'ISO-2022-JP', 'auto');

// toをエンコード
// $to = mb_convert_encoding($mail['to']['name'], "auto", "auto");
$to = "=?ISO-2022-JP?B?" . base64_encode($to) . '?= <' . $to . '>';

// fromをエンコード
$from_name = mb_convert_encoding($from_name, 'ISO-2022-JP', 'auto');
$from = "=?ISO-2022-JP?B?" . base64_encode($from_name) . '?= <' . $from_email . '>';

// 添付ファイルのエンコード01
$filename01 = mb_convert_encoding($filename01, 'ISO-2022-JP', 'auto');
$filename01 = "=?ISO-2022-JP?B?" . base64_encode($filename01) . "?=";

// 添付ファイルのエンコード02
$filename02 = mb_convert_encoding($filename02, 'ISO-2022-JP', 'auto');
$filename02 = "=?ISO-2022-JP?B?" . base64_encode($filename02) . "?=";

// ヘッダーの指定
$head = "";
$head .= "From: {$from}\n";
$head .= "MIME-Version: 1.0\n";
$head .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"\n";
$head .= "Content-Transfer-Encoding: 7bit";

$body = "";

// 本文
$body .= "--{$boundary}\n";
$body .= "Content-Type: text/plain; charset=ISO-2022-JP;" .
"Content-Transfer-Encoding: 7bit\n";
$body .= "\n";
$body .= "{$message}\n";
$body .= "\n";

// 添付ファイルの処理01
$body .= "--{$boundary}\n";
$body .= "Content-Type: {$mime_type}; name=\"{$filename01}\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment; filename=\"{$filename01}\"\n";
$body .= "\n";

$fp = fopen( $filepath01, "r" );
$contents01 = fread( $fp, filesize($filepath01) );
fclose( $fp );
$f_encoded01 = chunk_split(base64_encode($contents01)); //添付ファイルをbase64エンコードする
$body .= "{$f_encoded01}\n\r";

// 添付ファイルの処理02
$body .= "--{$boundary}\n";
$body .= "Content-Type: {$mime_type}; name=\"{$filename02}\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment; filename=\"{$filename02}\"\n";
$body .= "\n";

$fp = fopen( $filepath02, "r" );
$contents02 = fread( $fp, filesize($filepath02) );
fclose( $fp );
$f_encoded02 = chunk_split(base64_encode($contents02)); //添付ファイルをbase64エンコードする


//var_dump($body);
$body .= "{$f_encoded02}\n";
$body .= "\n";



if (mail($to, $subject, $body, $head)) {
echo 'sendmail_jpn : OK.';
} else {
echo 'sendmail_jpn : FAILURE.';
}
}

// mb_encode_mimeheaderのバグ対策用
function mb_encode_mimeheader_ex($text, $split_count = 34) {
$position = 0;
$encorded = '';

while ($position < mb_strlen($text, 'ISO-2022-JP')) {
if ($encorded != '') {
$encorded .= "\r\n ";
}
$output_temp = mb_strimwidth($text, $position, $split_count, '', 'ISO-2022-JP');
$position = $position + mb_strlen($output_temp, 'ISO-2022-JP');
$encorded .= "=?ISO-2022-JP?B?" . base64_encode($output_temp) . "?=";
}

return $encorded;
}

?>
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?