954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sending Email with Attachment

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...

jithusdani
Light Poster
47 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

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 "".$fileatt_type;
//echo "".$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);
}
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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

jithusdani
Light Poster
47 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

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" />
File: <input type="file" name="file" id="file"  /> 
<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);
}
?>
dean8710
Junior Poster in Training
54 posts since Jul 2011
Reputation Points: 7
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You