Click here to Skip to main content
15,742,609 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello Friends,

I hava a problem to get attechment in Mail.
Mail is successfully send with attechment.
but attechment is in encrypt form not a real download file.
here is my code.

<<blockquote class="FQ"><div class="FQA">Quote:</div>!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
// if there is post
if(isset($_POST) && !empty($_POST))
	//if there is an attechment
		// Store in some variable
		//get the extension of the file
		$base = basename($file_name);
		$extension = substr($base,strlen($base)-4,strlen($base));
		// only these file type will be allowed
		$allowed_extension = array(".doc",".docx",".text",".txt",".pdf");
		// Check that this file type is allowd
			// Mail essenstials
			$from = $_POST['email'];
			$to = "";
			$subject="This is test Mail";
			$message="This is random message";
			//things you need
			$file = $temp_name;
			$content = file_get_contents($file);
			$uid = md5(uniqid(time()));
			//standard mail headers
			$header = "From: ".$from."\r\n";
			$header .= "MIME-Version: 1.0\r\n ";
			//declaring we have multiple kinds of email (i.e Plain text and attechment)
			$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
			$header .= "This is a multi-part message in MIME formate. \r\n";
			// Plain text part
			$header .= "--".$uid."\r\n";
			$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
			$header .= "Content-Transfer-Encoding: 7bit \r\n\r\n";
			$header .= $message."\r\n\r\n";
			//File attechment
			$header .= "--".$uid."\r\n";
			$header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
			$header .= "Content-Transfer-Encoding: base64 \r\n";
			$header .= "Content-Disposition: attechment; filename=\"".$file_name."\"\r\n";
			$header .= $content."\r\n\r\n";
			//send the mail(message is not here but in the header in multiple way)
				echo "Sucess";
				echo "Fail";
			echo "File type is not allowed";
		echo "no File posted";
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<form enctype="multipart/form-data" action="attechment.php" method="post">
<input type="text" name="email" value="from" />
<br />
<input type="file" name="attechment" />
<br />
<input type="submit" name="Send Mail" />
Updated 2-Sep-12 22:14pm
bbirajdar 3-Sep-12 8:19am    
"but attechment is in encrypt form not a real download file.".. What does this mean? My english is poor, can you explain it clearly ....?

1 solution

You appear to be putting the whole message and attachment in the headers, and then the attachment again as the message body.

You've also said that the attachment is base-64 encoded, but you haven't encoded it.

I suggest you read the PHP mail manual page[^] and follow the links on that page to the RFCs that cover email and MIME.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900