Jump to content

Emailing an uploaded file, file corrupted


dmikester1

Recommended Posts

I need some help with emailing an uploaded file.  The file is always corrupted and just a few bytes, when it is normally a few KB.  I would prefer not to use the PHP mailer class so I can learn myself how to do the coding. 

Thanks

Mike

 

Here is the relevant code:

$uploaded = 0;
	$messages = array();
	if(isset($_FILES['uploadFiles']['name'])) {
		foreach ($_FILES['uploadFiles']['name'] as $i => $name) {
			if ($_FILES['uploadFiles']['error'][$i] == 4) {
				continue; 
			}
			if ($_FILES['uploadFiles']['error'][$i] == 0) {

				if ($_FILES['uploadFiles']['size'][$i] > 99439443) {
					$messages[] = "$name exceeded file limit.";
					continue;  
				}				

				$uploaded++;
			}
		}
		$files = $_FILES['uploadFiles']['tmp_name'];
	}
	echo "$uploaded receipt";
	if($uploaded != 1) {
		echo "s";
	}
	echo " uploaded.";

	foreach ($messages as $error) {
		echo $error;
	}

	// boundary 
	$semi_rand = md5(time()); 
	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

	// headers for attachment
	$eol = PHP_EOL;
	$headers = "From: ".$to.$eol;
	$headers .= "Reply-To: ".$to.$eol;
	$subject = "Expense Report - Period Ending $reportDate";
	$headers .= "MIME-Version: 1.0".$eol; 
	$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol.$eol;


	// no more headers after this, we start the body! //

	//$body = "--".$mime_boundary.$eol;
	//$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
	$body .= "This is a multi-part message in MIME format.".$eol;

	// message
	$body .= "--".$mime_boundary.$eol;
	$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
	$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
	$body .= $message.$eol.$eol;

	if($uploaded > 0) {  // if there are attachments add this line to the message, otherwise with no attachments this line will add an unwanted attachment
		//$message .= "--{$mime_boundary}\n";
	}


	if(isset($_FILES['uploadFiles']['name'])) {
		// 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));
			$name = $_FILES['uploadFiles']['name'][$x];
			/*$body .= "–{$mime_boundary}\n";
			$body .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . 
			"Content-Disposition: attachment;\n" . " filename=\"$name\"\n" . 
			"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
			//$body .="--{$mime_boundary}--\n";
			if ($x==count($files)-1) {
				//$message .="--{$mime_boundary}-\n";
			}
			else{
				//$message .="--{$mime_boundary}\n";
			}*/
			// attachment
			$body .= "--".$mime_boundary.$eol;
			$body .= "Content-Type: application/octet-stream; name=\"".$name."\"".$eol; 
			$body .= "Content-Transfer-Encoding: base64".$eol;
			$body .= "Content-Disposition: attachment".$eol.$eol;
			$body .= $data.$eol.$eol;
			$body .= "--".$mime_boundary."--";

		}
	}

	// send

	$ok = @mail($to, $subject, $body, $headers); 
	if ($ok) { 
		echo "<p>email sent to $to!</p>"; 
		} else { 
		echo "<p>email could not be sent!</p>"; 
	}
}
?>

Link to comment
Share on other sites

Heh, figured this one out on my own.  Turns out for my situation, I had to do a "move_uploaded_file" to a folder in my web root for it to work.  Maybe this isn't always the case, but it fixed my problem.  I also had to move the last "$body .= "--".$mime_boundary."--";" outside of the for loop to get it to work with multiple files.  Maybe this will help someone else out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.