Hey All,
I trying to write a script to attach file uploads to an email ... im using the following code:
<?php
$to = 'email@domain.com';
$subject = 'Im a subject';
$target_path = 'uploads/';
if (is_uploaded_file($_FILES['file1']['tmp_name'])) {
$firstname = $_FILES['file1']['name'];
$firsttype = $_FILES['file1']['type'];
$ext = substr($firstname, strpos($firstname,'.'), strlen($firstname)-1);
$rndname = md5(microtime() * mktime());
$rndname = substr($rndname,0,16);
$rndname = $rndname . $ext;
$firstfile = $target_path . $rndname;
move_uploaded_file($_FILES['file1']['tmp_name'], $firstfile);
$attachment = chunk_split(base64_encode(file_get_contents($firstfile)));
}
if (is_uploaded_file($_FILES['file2']['tmp_name'])) {
$secondname = $_FILES['file2']['name'];
$secondtype = $_FILES['file2']['type'];
$secondext = substr($secondname, strpos($secondname,'.'), strlen($secondname)-1);
$rndnamed = md5(microtime() * mktime());
$rndnamed = substr($rndnamed,0,16);
$rndnamed = $rndnamed . $secondext;
$secondfile = $target_path . $rndnamed;
move_uploaded_file($_FILES['file2']['tmp_name'], $secondfile);
$attachmentt = chunk_split(base64_encode(file_get_contents($secondfile)));
}
if (is_uploaded_file($_FILES['file3']['tmp_name'])) {
$thirdname = $_FILES['file3']['name'];
$thirdtype = $_FILES['file3']['type'];
$thirdext = substr($thirdname, strpos($thirdname,'.'), strlen($thirdname)-1);
$rndnamedd = md5(microtime() * mktime());
$rndnamedd = substr($rndnamedd,0,16);
$rndnamedd = $rndnamedd . $thirdext;
$thirdfile = $target_path . $rndnamedd;
move_uploaded_file($_FILES['file3']['tmp_name'], $thirdfile);
$attachmenttt = chunk_split(base64_encode(file_get_contents($thirdfile)));
}
$random_hash = md5(date('r', time()));
$headers = "From: email@domain.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hey, im not a html email
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>HTML Email contents go here</h2>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: <?php echo $firsttype; ?>; name="<?php echo $rndname; ?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: <?php echo $secondtype; ?>; name="<?php echo $rndnamed; ?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachmentt; ?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: <?php echo $thirdtype; ?>; name="<?php echo $rndnamedd; ?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachmenttt; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
$message = ob_get_contents();
ob_end_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );
header("Location: thank-you.htm");
?>
The only problem is that only 2 attachments are coming through .. I have no idea why 3 arent working, no errors, just 2 files.
Any help would be greatly appreciated.
Cheers.