hi frnds..

Here i m trying to attach the file from my webpage..it is working..but when i checking on the mail, the attachment is corrupted and it is not giving any file format

<?php
$e_mail=$result['EmailAddress'];				
$fileatt = $path; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // Filename that will be used for the file as the attachment
$email_from = $_SESSION['temp_name']; // Who the email is from
$email_subject = "Hello Hi"; // The Subject of the email
$email_txt = "My Name is XYZ"; // Message that the email has in it
$email_to = $e_mail; // Who the email is too
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

$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}\"";
$email_message .= "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" .$email_message . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$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";
ini_set("sendmail_from",$email_from);
$ok = @mail($email_to, $email_subject, $email_message, $headers);
	  }
	  
if($ok)
{
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}   
?>

can any one please provide the solution

thanks in advance

Please see it works, just change the email address from and to.

<?php
$e_mail=$result['EmailAddress'];                
$fileatt = $path='/var/www/obo/images/app-hover.png'; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = "myimage"; // Filename that will be used for the file as the attachment
$email_from = $_SESSION['temp_name']='hi@helo.com'; // Who the email is from
$email_subject = "Hello Hi"; // The Subject of the email
$email_txt = "My Name is XYZ"; // Message that the email has in it
$email_to = $e_mail='email@email.com'; // Who the email is too
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

$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}\"";
$email_message .= "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" .$email_message . "\n\n";

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

$email_message .= "--{$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";
 ini_set("sendmail_from",$email_from);
 $ok = @mail($email_to, $email_subject, $email_message, $headers);
//}

if($ok)
{
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}   
?>
Member Avatar for muaazab

Please modify this working script according to your needs. Send as many attachments as you like by putting file name in $files array.

<?php

    if($_POST['txtfname'] == '')
      $errorMsg = "Sorry First Name Missing"; 

    if($_POST['txtlname'] == '')
      $errorMsg = "Sorry Last Name Missing"; 

    if($_POST['txtemail'] == '')
      $errorMsg = "Sorry Email Missing"; 

    if($_POST['txtemail'] == '')
      $errorMsg = "Sorry Email Missing"; 

    if($_POST['optgender'] == '')
      $errorMsg = "Sorry Gender Missing"; 


     if($_POST['txtaddress'] == '')
      $errorMsg = "Please Enter Your Address"; 

    if($_POST['txtmobile'] == '')
      $errorMsg = "Please Enter Your Cell No"; 

    if($_POST['txtphoneno'] == '')
      $errorMsg = "Please Enter Your Phone No"; 

    if($_POST['txtdegreelevel'] == '')
      $errorMsg = "Please Enter a Degree Title"; 


    if($_POST['comjobapplycategory'] == '')
      $errorMsg = "Please Fill Job Applied For Field"; 

    if($errorMsg == '') 
    {



  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "";
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo "";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "";
      }
    }


$type = $_FILES["file"]["type"]; 
$send = $_FILES["file"]["name"]; 



  if ($_FILES["file1"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file1"]["error"] . "<br />";
    }
  else
    {
    echo "";
    if (file_exists("upload/" . $_FILES["file1"]["name"]))
      {
      echo "";
      }
    else
      {
      move_uploaded_file($_FILES["file1"]["tmp_name"],
      "upload/" . $_FILES["file1"]["name"]);
      echo "";
      }
    }


$type1 = $_FILES["file1"]["type"]; 
$send1 = $_FILES["file1"]["name"]; 



$rtxtfname=$_REQUEST["txtfname"];
$rtxtlname=$_REQUEST["txtlname"];
$rtxtemail=$_REQUEST["txtemail"];
$roptgender=$_REQUEST["optgender"];
$rcombmonth=$_REQUEST["combmonth"];
$rcomday=$_REQUEST["comday"];
$rcomyear=$_REQUEST["comyear"];
$rcomcountry=$_REQUEST["comcountry"];
$rcomcity=$_REQUEST["comcity"];
$rtxtaddress=$_REQUEST["txtaddress"];
$rtxtmobile=$_REQUEST["txtmobile"];
$rtxtphoneno=$_REQUEST["txtphoneno"];
$rcomdegree=$_REQUEST["comdegree"];
$rtxtdegreelevel=$_REQUEST["txtdegreelevel"];
$rtxtinstitute=$_REQUEST["txtinstitutename"];
$roptjob=$_REQUEST["optjob"];
$rtxttotalexp=$_REQUEST["txttotalexp"];
$rtxtcurrentjob=$_REQUEST["txtcurrentjob"];
$rtxtcompanyname=$_REQUEST["txtcompname"];
$roptjoin=$_REQUEST["optjoin"];
$rtxtsalary=$_REQUEST["txtsalary"];
$rchkngo=$_REQUEST["chkngo"];
$rcomjobapplycategory=$_REQUEST["comjobapplycategory"];


echo "<br><br>";


      // array with filenames to be sent as attachment

      $files = array("upload/$send","upload/$send1");

      // email fields: to, from, subject, and so on

      $to = "hr@kreshsolutions.com";

      $from = "fromcandiate@kreshsolutions.com";

      $subject ="Application For Employment";

      $message = <<<EOF

<table width="644" height="699" border="1" cellspacing=0 cellpadding=0>
  <tr>
    <td colspan="4"><img src="http://www.kreshsolutions.com/images/Banner.jpg"></td>
  </tr>
  <tr>
    <td colspan="4"><b>Personal Information</td>
  </tr>
  <tr>
    <td width="331">First Name</td>
    <td width="690">  $rtxtfname   </td>
  </tr>
  <tr>
    <td>Last Name</td>
    <td colspan="3"> $rtxtlname </td>
  </tr>
  <tr>
    <td>Email </td>
    <td colspan="3">  $rtxtemail </td>
  </tr>
  <tr>
    <td>Gender</td>
    <td colspan="4"> $roptgender </td>
  </tr>
  <tr>
    <td>Date of Birth</td>
    <td width="145">Month:   $rcombmonth   </td>
    <td width="59">Day :     $rcomday   </td>
    <td width="118">Year:     $rcomyear   </td>
  </tr>
  <tr>
    <td>Location</td>
    <td>Country :     $rcomcountry   </td>
    <td colspan="2">City :     $rcomcity   </td>
    </tr>
  <tr>
    <td>Address</td>
    <td colspan="3">    $rtxtaddress   </td>
    </tr>
  <tr>
    <td>Mobile </td>
    <td colspan="3">    $rtxtmobile   </td>
    </tr>
  <tr>
    <td>Phone No</td>
    <td colspan="3">    $rtxtphoneno   </td>
    </tr>
  <tr>
    <td colspan="4"><b>Academic Information</td>
    </tr>
  <tr>
    <td>Degree Level</td>
    <td colspan="3">    $rcomdegree    </td>
    </tr>
  <tr>
    <td>Degree Title</td>
    <td colspan="3">    $rtxtdegreelevel   </td>
    </tr>
  <tr>
    <td>Institute Name</td>
    <td colspan="3">    $rtxtinstitute   </td>
    </tr>
  <tr>
    <td colspan="4"><b>Professional Information</td>
    </tr>
  <tr>
    <td>On Job</td>
    <td colspan="3">    $roptjob   </td>
  </tr>
  <tr>
    <td>Total Experience</td>
    <td colspan="3">    $rtxttotalexp   </td>
    </tr>
  <tr>
    <td>Experience on Current Job</td>
    <td colspan="3">    $rtxtcurrentjob   </td>
    </tr>
  <tr>
    <td>Company Name</td>
    <td colspan="3">    $rtxtcompanyname   </td>
    </tr>
  <tr>
    <td>Joining</td>
    <td colspan="3">    $roptjoin   </td>
  </tr>
  <tr>
    <td>Expected Salary</td>
    <td>   $rtxtsalary  </td>
    <td colspan="2">   $rchkngo  </td>
  </tr>
  <tr>
    <td>Position Applied for</td>
    <td colspan="3">  $rcomjobapplycategory </td>

  </tr>

</table>


EOF;

      $headers = "From: $from";

     // boundary

      $semi_rand = md5(time());

      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";


      // headers for attachment

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

     // multipart boundary

      $message = "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" . $message . "\n\n";

      $message .= "--{$mime_boundary}\n";

     // preparing attachments

      for($x=0;$x<count($files);$x++){

          $file = fopen($files[$x],"rb");

          $data = fread($file,filesize($files[$x]));

          fclose($file);

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

          $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .

          "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .

          "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";

          $message .= "--{$mime_boundary}\n";

      }

      // send

      $ok = @mail($to, $subject, $message, $headers);

      if ($ok) {

          echo "<p>mail sent to $to!</p>";

      } else {

          echo "<p>mail could not be sent!</p>";

      }

$to=$rtxtemail;
$from="HR@kreshsolutions.com";
$subject= "Job Submission Confirmation";
$msg="Thanks  ".$rtxtfname .$rtxtlname ." for Your Application for the post of " . $rcomjobapplycategory . " If You are shortlisted we will contact you Soon";
$headers = "From: $from";
mail($to,$subject,$msg,$headers);
}

echo "<br><br>";

if($errorMsg != '')
     echo "<center><font face=verdana size=2><b>$errorMsg</font><br><b><font face=verdana size=2>Please <a href=index.php>go back</a> and try again.</font>";

echo "<br><br>";

unlink("upload/$send");
unlink("upload/$send1");
echo "<br><br>";

echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=0;URL=http://kreshsolutions.com>";
?>
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.