Search code examples
phpmimeemail-attachments

File attachment not being sent to email using PHP


I am trying to send an attachment from a form. The email produces an attachment just not the one Im trying to upload. I would like for it to upload and change the name to the $strNewFileName when its sent to the user.

html Code:

<input type="file" class="upload" name="uploaded_file[]" id="uploaded_file"></input>

php File:

<?php
$strTo = "me@example.com";
$strSubject = "Document Updated";  
$strMessage = nl2br($_GET["sqlCode"]);
$strNewFileName = $_GET["RevisedFileName"];

 if (is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {
echo "File ". $_FILES['uploaded_file']['name'] ." uploaded successfully.\n";
echo "Displaying contents\n";
readfile($_FILES['uploaded_file']['tmp_name']);
} else {
echo "Possible file upload attack: ";
echo "filename '". $_FILES['uploaded_file']['tmp_name'] . "'.";
}
 //*** Uniqid Session ***//  
$strSid = md5(uniqid(time()));   
$strHeader = "";  
$strHeader .= "From: Document Update\r\nReply-To: example@example.com";    
$strHeader .= "MIME-Version: 1.0\n";  
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
$strHeader .= "This is a multi-part message in MIME format.\n";   
$strHeader .= "--".$strSid."\n";  
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
$strHeader .= $strMessage."\n\n"; 
rename("$strFilesName", "$strNewFileName");  

  // *** Attachment *** //  
  $strFilesName = $_FILES["uploaded_file"]["name"]; 
  $strContent = chunk_split(base64_encode(file_get_contents($_FILES["uploaded_file"]["name"])));
  $strHeader .= "--".$strSid."\n";  
  $strHeader .= "Content-Type: application/pdf; name=\"".$strNewFileName."\"\r\n";
  $strHeader .= "Content-Transfer-Encoding: base64\n";  
  $strHeader .= "Content-Disposition: attachment;\n\n";  
  $strHeader .= $strContent."\n\n";

$flgSend = @mail($strTo,$strSubject,$strMessage,$strHeader);  

if($flgSend)  
{  
   echo "Approval Send Complete."; 
}  
else  
{  
   echo "Cannot send mail, Approval has not been processed.";  
 } 

 ?>

Solution

  • I think your problem is here:

    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["uploaded_file"]["name"])));
    

    _FILES["uploaded_file"]["name"] represents the original name of the uploaded file, not it's location on the server. You need to use $_FILES['uploaded_file']['tmp_name'] for this (as you do earlier in the script where you validate it is a properly uploaded file.

    Also for array access (because of use of uploaded_file[] as input name), you would need to do something like this to build an array of valid attachments:

    $valid_attachments = array();
    foreach ($_FILES['uploaded_file']['tmp_name'] as $key => $temp_file_name) {
        if (is_uploaded_file($temp_file_name)) {
            $valid_attachments[] = array(
                'path' => $temp_file_name,
                'filename' => $_FILES['uploaded_file']['name'][$key],
                'type' => $_FILES['uploaded_file']['type'][$key],
                'size' => $_FILES['uploaded_file']['size'][$key]
            );
        }
    }
    

    You would then need to loop through this array in your "Attachment" section of code and add the attachments to the email.