hi freinds,,

In my application i have to send email to a specified email, I have done this and its working fine , Now I want to send some word attachment in the same email id. The word attachment will select the user through the application as browse option just like resume sending..I want to send the selected word file as an attachement to an email id ..So anybody plz help...

hello...check this:

$to      = "somw@some.com";
$from    = $_POST['email'];
$subject = "Resume Details";
$mail_body = "message body";
 $fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

//echo $fileatt;
//echo "<br>".$fileatt_type;
//echo "<br>".$fileatt_name;
//exit;

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
  // Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  

        $semi_rand = md5( time() ); 
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
    
        $headers .= "\nMIME-Version: 1.0\n" . 
                    "Content-Type: multipart/mixed;\n" . 
                    " boundary=\"{$mime_boundary}\"";
    
        $mail_body = "This is a multi-part message in MIME format.\n\n" . 
                "--{$mime_boundary}\n" . 
                "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
                "Content-Transfer-Encoding: 7bit\n\n" . 
                $mail_body . "\n\n";
    
        $data = chunk_split( base64_encode( $data ) );
                 
        $mail_body .= "--{$mime_boundary}\n" . 
                 "Content-Type: {$fileatt_type};\n" . 
                 " name=\"{$fileatt_name}\"\n" . 
                 "Content-Disposition: attachment;\n" . 
                 " filename=\"{$fileatt_name}\"\n" . 
                 "Content-Transfer-Encoding: base64\n\n" . 
                 $data . "\n\n" . 
                 "--{$mime_boundary}--\n"; 
				 
				 $ok = @mail($to, $subject, $mail_body, $headers);
fclose($file);
}

thank u shanti its working fine....thnks a lot

Just edited, anyway, nice posted Shanti :)

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Send email with Attachment</title>
</head>

<body>

<h2>Send email with Attachment</h2>

<form action="process.php" method="post" enctype="multipart/form-data">
To:<input type="text" name="email" /><br />
File: <input type="file" name="file" id="file"  /> <br />
<input type="submit" name="submit" value="Sent"  />
</form>

</body>
</html>

process.php

<?php
$from = "you@domain.com";
$to = $_POST['email'];
$subject = "Uploaded file";
$mail_body = "Message body";
$fileatt = $_FILES['file']['tmp_name'];
$fileatt_type = $_FILES['file']['type'];
$fileatt_name = $_FILES['file']['name'];

$headers = "From: $from";
 
if (is_uploaded_file($fileatt)) 
{
	// Read the file to be attached ('rb' = read binary)
	$file = fopen($fileatt, 'rb');
	$data = fread($file, filesize($fileatt));

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

	$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

	$mail_body = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
	"Content-Transfer-Encoding: 7bit\n\n" . $mail_body . "\n\n";

	$data = chunk_split( base64_encode($data));

	$mail_body .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . 
	" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; 

	$ok = @mail($to, $subject, $mail_body, $headers);
	
	if($ok)
		echo "Email sent :)";
	else
		echo "Failed !!!";
	
	fclose($file);
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.