Home > Enterprise >  Unable to send email with multiple attachments in using mail
Unable to send email with multiple attachments in using mail

Time:10-18

I am trying to send four attachments in my email, which I am receiving from POST API, I have using form data in postman. I am receiving only last image/file. Please help me on this

error_reporting(-1);
  ini_set('display_errors', 'On');

I used this for error reporting. Below is the code.

  if(isset($_POST) )
  {
      $name        = "Name goes here";
      $email       = "[email protected]";
      $to          = "$name <$email>";
      $from        = "Gyan-Shah ";
      $subject     = "Here is your attachment";
      $mainMessage = 'Hi,
                      Below are the Details:'.
                      "Name: '".$_POST['fullname']."' \n".
                      "Date Of Birth: '".$_POST['dob']."' \n".
                      "Phone Number: '".$_POST['phone_number']."' \n".
                      "Email: '".$_POST['email']."' \n".
                      "Address: '".$_POST['address']."' \n".
                      "Father Name: '".$_POST['father_name']."' \n".
                      "Mother Name: '".$_POST['mother_name']."' \n".
                      "Work Experience: '".$_POST['work_experience']."' \n".
                      "Education: '".$_POST['education']."' \n".
                      "Skills: '".$_POST['skills']."' \n";
      /*$fileatt     = "files/test.pdf"; //file location
      $fileatttype = "application/pdf";
      $fileattname = "newname.pdf";*/ //name that you want to use to send or you can use the same name
      $headers = "From: $from";

/* start for photo */

  $tmp_name = $_FILES['photo']['tmp_name']; // get the temporary file name of the file on the server
  $name     = $_FILES['photo']['name']; // get the name of the file
  $size     = $_FILES['photo']['size']; // get size of the file for size validation
  $type     = $_FILES['photo']['type']; // get type of the file
  $error    = $_FILES['photo']['error']; // get the error (if any)
  
  $handle = fopen($tmp_name, "r"); // set the file handle only for reading the file
  $content = fread($handle, $size); // reading the file
  fclose($handle);    

  
  // This attaches the 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}\"";
    $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" .
    $mainMessage  . "\n\n";
  
  //$data = chunk_split(base64_encode($data));
  $data = chunk_split(base64_encode($content));
  $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$type};\n" .
    " name=\"{$name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
  $data . "\n\n" .
  "--{$mime_boundary}--\n";
  
  

/* end for photo */

/* start passport front */

  $tmp_name_p_f = $_FILES['passport_front']['tmp_name']; // get the temporary file name of the file on the server
  $name_p_f     = $_FILES['passport_front']['name']; // get the name of the file
  $size_p_f     = $_FILES['passport_front']['size']; // get size of the file for size validation
  $type_p_f     = $_FILES['passport_front']['type']; // get type of the file
  $error_p_f    = $_FILES['passport_front']['error']; // get the error (if any)
  
  $handle_p_f = fopen($tmp_name_p_f, "r"); // set the file handle only for reading the file
  $content_p_f = fread($handle_p_f, $size_p_f); // reading the file
  fclose($handle_p_f);    

  
  // This attaches the file
  $semi_rand_p_f     = md5(time());
  $mime_boundary_p_f = "==Multipart_Boundary_x{$semi_rand_p_f}x";
  $headers      .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary_p_f}\"";
    $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" .
    $mainMessage  . "\n\n";
  
  //$data = chunk_split(base64_encode($data));
  $data_p_f = chunk_split(base64_encode($content_p_f));
  $message = "--{$mime_boundary_p_f}\n" .
    "Content-Type: {$type_p_f};\n" .
    " name=\"{$name_p_f}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$name_p_f}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
  $data_p_f . "\n\n" .
  "--{$mime_boundary_p_f}--\n";
  
  /* end passport front */
  
  
  // Send the email
  if(mail($to, $subject, $message, $headers)) {
  
    echo "The email was sent.";
  
  }
  else {
  
    echo "There was an error sending the mail.";
  }


 }

I see no errors in error log. Above is all the code I am using.

CodePudding user response:

Please replace your code with the one I am writing below, I tested it, It's working.

$name        = "Job Application App";
    $email       = "[email protected]"; // Please change this to your email ID
    $to          = "$name <$email>";
    $from        = "[email protected]"; // Please change this to your email ID
    $subject     = "New CV Submission";
    $mainMessage = "Hi, \n\nYou Have received new application. Below are the Details: \n\nName: ".$_POST['fullname']." \nDate Of Birth: ".$_POST['dob']." \nPhone Number: ".$_POST['phone_number']." \nEmail: ".$_POST['email']." \nAddress: ".$_POST['address']."\nFather Name: ".$_POST['father_name']."\nMother Name: ".$_POST['mother_name']."\nWork Experience: ".$_POST['work_experience']."\nEducation: ".$_POST['education']."\nSkills: ".$_POST['skills']."\n";
                
$headers = "From: $from";

$tmp_name = $_FILES['photo']['tmp_name']; // get the temporary file name of the file on the server
$name     = $_FILES['photo']['name']; // get the name of the file
$size     = $_FILES['photo']['size']; // get size of the file for size validation
$type     = $_FILES['photo']['type']; // get type of the file
$error    = $_FILES['photo']['error']; // get the error (if any)

$handle = fopen($tmp_name, "r"); // set the file handle only for reading the file
$content = fread($handle, $size); // reading the file
fclose($handle); 

// This attaches the 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}\"";
  
  $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 .= "--{$mime_boundary}\n";

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

$message .= "Content-Type: {".$type."};\n" . 
            " name=\"$name\"\n" . 
            "Content-Disposition: attachment;\n" . 
            " filename=\"$name\"\n" . 
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";

$mainMessage .= "Photo: ".$_FILES['photo']['name']  . "\n";

$tmp_name_p_f = $_FILES['passport_front']['tmp_name']; // get the temporary file name of the file on the server
$name_p_f     = $_FILES['passport_front']['name']; // get the name of the file
$size_p_f     = $_FILES['passport_front']['size']; // get size of the file for size validation
$type_p_f     = $_FILES['passport_front']['type']; // get type of the file
$error_p_f    = $_FILES['passport_front']['error']; // get the error (if any)

$handle_p_f = fopen($tmp_name_p_f, "r"); // set the file handle only for reading the file
$content_p_f = fread($handle_p_f, $size_p_f); // reading the file
fclose($handle_p_f);    

// This attaches the file
$semi_rand_p_f     = md5(time());
$mime_boundary_p_f = "==Multipart_Boundary_x{$semi_rand_p_f}x";

$data_p_f = chunk_split(base64_encode($content_p_f));

 $message .= "--{$mime_boundary_p_f}\n" .
             "Content-Type: {".$type_p_f."};\n" . 
            " name=\"$name_p_f\"\n" . 
            "Content-Disposition: attachment;\n" . 
            " filename=\"$name_p_f\"\n" . 
            "Content-Transfer-Encoding: base64\n\n" . $data_p_f . "\n\n";
    $message .= "--{$mime_boundary_p_f}\n";
    
$mainMessage .= "Passpost Front: ".$_FILES['passport_front']['name']  . "\n";

$message .= $mainMessage  . "\n\n";

// Send the email
if(mail($to, $subject, $message, $headers)) {
    echo "The email was sent.";
}
else {

  echo "There was an error sending the mail.";
}
  • Related