I am new in PHP programming. I have a html / Javascript form that needs to submit multiple (6) Picture attachments.
When I submit the form, the form is submitted but no attachments arrive on the email. I am using arrays to accept the
attachments. The PHP code below is only trying to comprehend 2 attachments to start with but even this does not arrive on
the email. I do receive all the fields from the HTML / Javascript form on the email. Only the attachments do not arrive.
Can anyone assist with this PHP coding? I would highly appreciate anyones help and ideas.
<?php
// Read POST request params into global vars
$to = $_POST['to'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$name = $_POST['name'];
$phnnumb = $_POST['phnnumb'];
$cell = $_POST['cell'];
$email = $_POST['email'];
$address = $_POST['address'];
ETC...
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// array with filenames to be sent as attachment
$files = array(2);
$fileatt_type = array(2);
$fileatt_name = array(2);
$data = array(2);
// Obtain file upload vars
$files[0] = $_FILES['fileatt']['tmp_name'];
$fileatt_type[0] = $_FILES['fileatt']['type'];
$fileatt_name[0] = $_FILES['fileatt']['name'];
// Obtain file secoond upload vars
$files[1] = $_FILES['fileatt2']['tmp_name'];
$fileatt_type[1] = $_FILES['fileatt2']['type'];
$fileatt_name[1] = $_FILES['fileatt2']['name'];
for ($x=0;$x<count($files);$x++)
{
if (is_uploaded_file($files[$x])) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($files[$x],'rb');
$data[$x] = fread($file,filesize($files[$x]));
fclose($file);
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type[$x]};\n" .
" name=\"{$fileatt_name[$x]}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name[$x]}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data[$x] . "\n\n" .
"--{$mime_boundary}--\n";
}
}
// Add the headers for a file attachment
$headers .= "\n MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Base64 encode the file data
for($j=0; $j<count($files); $j++)
$data[$j] = chunk_split(base64_encode($data[$j]));
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
"Name: " . $name . "\n\n" .
"Phone Number: " . $phnnumb . "\n\n" .
"Cell Phone: " . $cell . "\n\n" .
"Email Address: " . $email . "\n\n" .
"Physical Address: " . $address . "\n\n" .
ETC...
// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p align='center'><img src='Head_Banner2.jpg' width='520' height='85' /></p>
<p align='center'>Thank you <b>$name</b>
for posting with us! </p>";
} else {
echo "<p><font size='12'>Sorry, but there as an error. If the problem persists please email us</font></p>";
}
?>