I want to mail two attachments and choice file from two different input tags. I fine a code that send only one file. my code is
<?php
if(isset($_POST) && !empty($_POST))
{
if(!empty($_FILES['attachment']['name']))
{
$file_name = $_FILES['attachment']['name'];
$temp_name = $_FILES['attachment']['tmp_name'];
$file_type = $_FILES['attachment']['type'];
$base = basename($file_name);
$extension = substr($base, strlen($base)-4, strlen($base));
$allow_extension = array(".jpg",".pdf",".png");
if(in_array($extension,$allow_extension))
{
$form = $_POST['email'];
$to = "hassanh80@gmail.com";
$subject = "Subject Here";
$message = "Message Here";
$file = $temp_name;
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$header = "From: ". $form . "
";
$header .= "Replay-To: ". $to . "
";
$header .= "MIME-Version: 1.0
";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"
";
$header .= "This is multi-part message in MIME format.
";
$header .= "--".$uid."
";
$header .= "Content-type:text/plain; charset=iso-8859-1
";
$header .= "Content-Transfer-Encoding: 7bit
";
$header .= $message."
";
$header .= "--".$uid."
";
$header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"
";
$header .= "Content-Transfer-Encoding: base64
";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"
";
$header .= $content."
";
if(mail($to,$subject, "", $header))
{
echo "Successfull";
}
else
{
echo "Fail";
}
}
else
{
echo "File Type Not Allow...!";
}
}
else
{
echo "No File Posted...!";
}
}
?>
<form method="post" action="mail1.php" enctype="multipart/form-data">
<input type="email" name="email" />
<br />
<input type="file" name="attachment" />
<br />
<input type="submit" value="Send">
</form>
Now I want to put one more input file tag. I tried many time but not worked. kindly help me out.
This script does what you want. Gets the files from the input, put them in an array then loop through the file array when its time to attach. Your code and this can be combined smartly
<?php
// array with filenames to be sent as attachment
$files = array("file_1.ext","file_2.ext","file_3.ext",......);
// email fields: to, from, subject, and so on
$to = "mail@mail.com";
$from = "mail@mail.com";
$subject ="My subject";
$message = "My message";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.
" . "--{$mime_boundary}
" . "Content-Type: text/plain; charset=\"iso-8859-1\"
" . "Content-Transfer-Encoding: 7bit
" . $message . "
";
$message .= "--{$mime_boundary}
";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};
" . " name=\"$files[$x]\"
" .
"Content-Disposition: attachment;
" . " filename=\"$files[$x]\"
" .
"Content-Transfer-Encoding: base64
" . $data . "
";
$message .= "--{$mime_boundary}
";
}
// send
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
?>
Hope it helps
</div>