Click here to Skip to main content
15,900,973 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.

PHP
<<blockquote class="FQ"><div class="FQA">Quote:</div>!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
// if there is post
if(isset($_POST) && !empty($_POST))
{
	//if there is an attechment
	if(!empty($_FILES['attechment']['name']))
	{
		// Store in some variable
		$file_name=$_FILES['attechment']['name'];
		$temp_name=$_FILES['attechment']['tmp_name'];
		$file_type=$_FILES['attechment']['type'];
		
		//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
		if(in_array($extension,$allowed_extension))
		{
			// Mail essenstials
			$from = $_POST['email'];
			$to = "dhavalvala11@gmail.com";
			$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)
			
			if(mail($to,$subject,$content,$header))
			{
				echo "Sucess";
			}
			else
			{
				echo "Fail";
			}
				
		}
		else
		{
			echo "File type is not allowed";
		}
	}
	else
	{
		echo "no File posted";
	}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>attechment</title>
</head>

<body>
<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" />
</form>
</body>
</html>
Posted
Updated 2-Sep-12 22:14pm
v2
Comments
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