Attach file to mail using php

Posted by ktsixit on Stack Overflow See other posts from Stack Overflow or by ktsixit
Published on 2010-06-07T12:03:40Z Indexed on 2010/06/07 12:42 UTC
Read the original article Hit count: 203

Filed under:
|
|
|
|

Hi all, I've created a form which contains an upload field file and some other text fields. I'm using php to send the form's data via email and attach the file.

This is the code I'm using but it's not working properly. The file is normally attached to the message but the rest of the data is not sent.

$body="bla bla bla";

$attachment = $_FILES['cv']['tmp_name'];
$attachment_name = $_FILES['cv']['name']; 
if (is_uploaded_file($attachment)) { 
  $fp = fopen($attachment, "rb"); 
  $data = fread($fp, filesize($attachment)); 
  $data = chunk_split(base64_encode($data)); 
    fclose($fp);
}

$headers = "From: $email<$email>\n";
$headers .= "Reply-To: <$email>\n"; 
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n"; 
$headers .= "X-Sender: $first_name $family_name<$email>\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; 
$headers .= "Return-Path: <$email>\n"; 
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n"; 
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; 

$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\n"; 
$message .= "Content-Transfer-Encoding: quoted-printable\n"; 
$message .= "\n"; 
$message .= "$body\n";
$message .= "\n"; 
$message .= "------=MIME_BOUNDRY_message_parts--\n"; 
$message .= "\n"; 
$message .= "------=MIME_BOUNDRY_main_message\n"; 
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n"; 
$message .= "------=MIME_BOUNDRY_main_message--\n";

$subject = 'bla bla bla';
$to="[email protected]";
mail($to,$subject,$message,$headers);

Why isn't the $body data not sent? Can you help me fix it?

© Stack Overflow or respective owner

Related posts about php

Related posts about file