Cannot Open pdf, docx attached to contact form.

John__N

New member
Apr 1, 2009
434
3
0
hi guys,
I have this php script which emails the attachment which can be a pdf, docx, doc etc. The script emails fine but the problem is i am not being able to open the pdf and the docx file attached opens with some random characters, can someone please point out to the error or something i need to add.
Thanks

PHP:
<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
// Read POST request params into global vars
   
    $postappliedfor=$_POST['postapliedfor'];
    $from =($_POST['name']);
    $fathersname = $_POST['fathersname'];
    $mothersname = $_POST['mothersname'];
    $subject = "CV Attached";
    $presentaddress = stripslashes($_POST['presentaddress']);
    $phonenumber = $_POST['phonenumber'];
    $permanentaddress = stripslashes($_POST['permanentaddress']);
    $dateofbirth = $_POST['dateofbirth'];
    $place = $_POST['place'];
    $age = $_POST['age'];
    $height = $_POST['height'];
    $religion = $_POST['religion'];
    $maritalstatus = $_POST['maritalstatus'];
    $passportnumber = $_POST['passportnumber'];
    $placeofissue = $_POST['placeofissue'];
    $dateofissue = $_POST['dateofissue'];
    $dateofexpiry = $_POST['dateofexpiry'];
    $basicqualification = $_POST['basicqualification'];
    $technicalqualification = $_POST['technicalqualification'];
    $workexperienceindia = stripslashes($_POST['workexperienceindia']);
    $workexperienceabroad = stripslashes($_POST['workexperienceabroad']);
    $body = $_POST['name'];
    $drivinglicensenumberindian = $_POST['drivinglicensenumberindian'];
    $internationaldrivinglicensenumber = $_POST['internationaldrivinglicensenumber'];
    $tospeake = $_POST['tospeake'];
    $toreade = $_POST['toreade'];
    $towritee = $_POST['towritee'];
    $tospeakh = $_POST['tospeakh'];
    $toreadh = $_POST['toreadh'];
    $towriteh = $_POST['towriteh'];
    $tospeaka = $_POST['tospeaka'];
    $toreada = $_POST['toreada'];
    $towritea = $_POST['towritea'];
    $to = "email@domain.com";
    $message = "Post Applied For: $postappliedfor\n Name As Per Passport: $name\n Fathers Name: $fathersname\n Mothers Name: $mothersname\n Present Address: $presentaddress\n Phone: $phone\n Permanent Address: $permanentaddress\n Date of Birth: $dataofbirth\n Place: $place\n Age $age\n Height: $height\n Religion: $religion\n Marital Status: $maritalstatus\n Passport Number: $passportnumber\n Place of Issue: $placeofissue\n Date of Issue: $dateofissue\n Date of Expiry: $dateofexpiry\n Basic Qualification: $basicqualification\n Technical Qualification: $technicalqualification\n Work Experience in India: $workexperienceinindia\n Work Experience Abroad: $workexperienceabroad\n Indian Driving License Number: $drivinglicensenumberindian\n Can Speak English: $canspeake\n Can Read English: $canreade\n Can Write English: $canwritee\n Can Speak Hindi: $canspeake\n Can Read Hindi: $canreadh\n Can Write Hindi: $canwriteh\n Can Speak Arabic: $canspeaka \n Can Read Arabic: $canreade Can Write Arabic: $canwritea\n";

// Obtain file upload vars
$attachment = $_FILES['attachment']['tmp_name'];
$attachment_type = $_FILES['attachment']['type'];
$attachment_name = $_FILES['attachment']['name']; 

$headers = "From: $from";



if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
      $fp = fopen($attachment, "rb"); //Open it
      $data = fread($fp, filesize($attachment)); //Read it
      $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
        fclose($fp);

  // Generate a boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  // Add the headers for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

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

  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));

  // Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$attachment_type};\n" .
              " name=\"{$attachment_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$attachment_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}

// Send the message
 if (mail($to, $subject, $message, $headers))
 {
  echo "<p>Mail sent! Yay PHP!</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
</body>
</html>
 


1) You need to fix your variables and make their names more meaningful.
2) You need to use camel case and typeYourVariablesLikeThis to make them more readable.

As for your problem, my guess is that it's grabbing the files and encoding them with ASCII instead of Binary, which would lead to your word documents throwing up nasty characters.
 
I also believe you are missing the content type declaration for the attachment which should be the respective "text/doc" or "text/pdf"...
 
do var_dump($_FILES) somewhere after the POST and see what it says about the mime type