Error in mail send with attachment?

#1

This is the php code I am using to send email with attachment

         <?php 

       if(isset($_FILES['thisFile']) ) 
   { 

   $to = 'xyz@gmail.com';

 //sender
  $from = 'abc@gmail.com';
   $fromName = 'John doe';

   //email subject
   $subject = 'Attachment '; 

    //attachment file path
     $file = $_FILES['thisFile']['name'];

   //email body content
   $htmlContent = 'Your From Successfully Submitted';

      //header for sender info
       $headers = "From: $fromName"." <".$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 = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF- 
  8\"\n" .
  "Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n"; 

  //preparing attachment
  if(!empty($file) > 0){
     if(is_file($file)){
        $message .= "--{$mime_boundary}\n";
       $fp =    @fopen($file,"rb");
      $data =  @fread($fp,filesize($file));

    @fclose($fp);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: application/octet-stream; 
       name=\"".basename($file)."\"\n" . 
    "Content-Description: ".basename($file)."\n" .
    "Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; 
  size=".filesize($file).";\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
  }
 }
   $message .= "--{$mime_boundary}--";
   $returnpath = "-f" . $from;

 //send email

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

        //email sending status
         echo $mail?"<h1>Mail sent.</h1>":"<h1>Mail sending failed.</h1>";
  ?>                        

this is my html input type

            <input type="file" class="custom-file-input" id="thisFile" name="thisFile">

Where I went wrong in this code its displaying message like “Email sent failed”
What should I do ?

#2
$file = $_FILES['thisFile']['tmp_name'];

#3

hey now th email is sending but the data is showing blank after I downloaded the attachment

#4

Instead of all that manual coding to add your attachment, wouldn’t it be easier to use something like PHPMailer or one of the other libraries that will handle it for you, and give other advantages over mail() ?

#5

I assume, you need in your form attribute enctype=“multipart/form-data”.

#6

Without that, would it populate the $_FILES array at all?

#7

No. Files will be sended per HTTP only if to set this enctype.

#9

What I was driving at was - if the form didn’t specify the encoding, then the $_FILES array wouldn’t be present, so the email wouldn’t be sent at all. So as the email is sending but the attachment is blank, the encoding must be set.

#10

https://www.w3schools.com/html/html_forms.asp

Default data encoding is “url-encoded”. That shouldn’t be set explicitly.