Reputation: 85
Hi ladies and gentlemen I am hoping you will be able to help me with this.
I have created a web form where people can enter registration information and upload an image to our server for a photo contest. The uploader works great and an email gets sent with the registration information but I am having a hard time getting the uploaded image to attach to the email. I was hoping someone would be able to help me figure this out. The form and processor page are located in the same directory and the image get placed in a subfolder called upload. Here is what I have so far which does not work for attaching image. It give me an error:
<?php
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
echo "\nFile submitted.";
}
}
}
else
{
echo "Invalid file";
}
// DO THE MAILING HERE
$fields = array('name','address','city','state','zip','phone','photo','location');
//$to = "[email protected]";
$to2 = "[email protected]";
$subject = "Sustainability Photo Contest";
$headers = "MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
$headers .='Content-type: image/jpeg; charset=iso-8859-1' . PHP_EOL;
$headers .= "From: [email protected]\r\n" . "X-Mailer: php";
$bound_text = "DACCreport";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$message = "If you can see this MIME than your client doesn't accept MIME types!\r\n"
.$bound;
$message .= "Content-Type: image/jpeg; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."Photo Contest entry\r\n"
.$bound;
$file = file_get_contents($_FILES["file"]["name"]);
$message .= "Content-Type: image/jpeg; name="($_FILES["file"]["name"])"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-disposition: attachment; file="($_FILES["file"]["name"])"\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;
$greet = "The following was submitted on " . date("F j, Y, g:i a") . "<p>";
$body = $greet;
//$cn = 1;
foreach($fields as $efield) {
if(isset($_POST[$efield])) {
if($efield == "name") {
$body.= "<strong>Name: </strong>" . $_POST[$efield] . "<p>";
} elseif($efield == "address") {
$body.= "<strong>Address:</strong> " . $_POST[$efield] . "<p>";
} elseif($efield == "city") {
$body.= "<strong>City:</strong> " . $_POST[$efield] . "<p>";
} elseif ($efield == "state") {
$body.= "<strong>State: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "phone") {
$body.= "<strong>Daytime Phone Number: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "photo") {
$body.= "<strong>Name of Photo: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "location") {
$body.= "<strong>Location where Photo was taken:</strong> " . $_POST[$efield] . "<p>";
}
}
}
//mail($to, $subject, $body, $headers);
mail($to2, $subject, $body, $headers);
?>
Upvotes: 0
Views: 8070
Reputation: 85
Here is the solution I came up with. It took a little finessing based on the upload code and attaching files information. Plus I ran into a problem with the headers, but I managed to get it to work and it works well.
<?php
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
echo "<br> File submitted.";
}
}
}
else
{
echo "Invalid file";
}
// DO THE MAILING HERE
$filename = "upload/" . $_FILES["file"]["name"];
;
$to3 =
$to4 =
$subject = 'Sustainability Photo Contest';
$bound_text = "dacc1231";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$headers = "MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"$bound_text\"". PHP_EOL;
$headers .= "From: [email protected]\r\n";
$fields = array('name','address','city','state','zip','phone','photo','location');
$greet = "The following was submitted on " . date("F j, Y, g:i a") . "<p>";
$body = $greet;
//$cn = 1;
foreach($fields as $efield) {
if(isset($_POST[$efield])) {
if($efield == "name") {
$body.= "<strong>Name: </strong>" . $_POST[$efield] . "<p>";
} elseif($efield == "address") {
$body.= "<strong>Address:</strong> " . $_POST[$efield] . "<p>";
} elseif($efield == "city") {
$body.= "<strong>City:</strong> " . $_POST[$efield] . "<p>";
} elseif ($efield == "state") {
$body.= "<strong>State: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "phone") {
$body.= "<strong>Daytime Phone Number: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "photo") {
$body.= "<strong>Name of Photo: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "location") {
$body.= "<strong>Location where Photo was taken:</strong> " . $_POST[$efield] . "<p>";
}
}
}
$message = "If you can see this MIME than your client doesn't accept MIME types!\r\n"
.$bound;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."Here is a Submission for the Photo Contest\r\n" .$body ."\r\n"
.$bound;
$file = file_get_contents($filename);
$message .= "Content-Type: image/jpg; name=\"".$filename."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-disposition: attachment; file=\"".$filename."\"\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;
if(mail($to3, $subject, $message, $headers)
&&
mail($to4, $subject, $message,$headers ))
{
echo '<br> MAIL SENT';
} else {
echo '<br> MAIL FAILED';
}
?>
Upvotes: 1
Reputation: 21
$files = $_FILES['filesToUpload']['tmp_name'];
$filename = $_FILES['filesToUpload']['name'];
$fp = fopen($files[$i],"rb");
$data = fread($fp,filesize($files[$i]));
fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($filename[$i])."\"\n" .
"Content-Description: ".basename($filename[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($filename[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
You can attach file without moving it to the server using this code.
Upvotes: 0