Send files as attachment PHP

This post explains how to send files as attachment PHP. It sends all the files in folder as an attachment using PHP. Then how to move the files to an archive folder and delete the emailed files from the source or placeholder folder.

The following code can set as cron job as well. The cron that send multiple files as attachment together and moved the attached files from the original folder to the archive folder .

I will explain this step by step :

1) Set the base and destination paths.
2) Check the folder or directory to see if there any files exist.
3) Read the files
4) Prepare the emails and attachment
5) Send email using the mail() function
6) Copy the files from one folder to another
7) Delete the file from the source folder.

Set the base and destination paths.

$base = DIRECTORY_SEPARATOR.'home'.DIRECTORY_SEPARATOR.'devtub'.DIRECTORY_SEPARATOR.'public_html'.DIRECTORY_SEPARATOR.'var'.DIRECTORY_SEPARATOR.'export';
$path = $base.DIRECTORY_SEPARATOR.'place_order'.DIRECTORY_SEPARATOR;
$destination = $base.DIRECTORY_SEPARATOR .'archived'.DIRECTORY_SEPARATOR;

Check the folder or directory to see if there any files exist.

/* This function check is there any new order placed */
function checkFileExist( $path  ) {
    
    if (is_dir($path)) {
       
        $contents = scandir($path);
       
        if(count($contents) > 2) {
            return $contents;
        } else {
            return false;
        }
    }
    
}

Read the files

 if($contents) {
   foreach ($contents as $file) {
        if(strpos($file, '.') !== (int) 0) {
            sendMailAsAttachment( $file , $path);
            copyPlaceholderFileToArchived( $file, $path , $destination );
        }
    }
   }

.a) Prepare the emails using PHP

function pepareEmail() {
    
    // email fields: to, from, subject, and so on
    $to = "order@test.com ";
    $from = "admin@admin.com"; 
    $subject ="New Orders"; 
    $message = "New Orders";
    $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/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
    $message .= "--{$mime_boundary}\n";
    
    $emailData = array (
        'to' => $to,
        'from' => $from,
        'subject' => $subject,
        'headers' => $headers,
        'message' => $message
    );
    
    return $emailData;
    
}

.b) Read the file content using PHP and prepare the data to send as an attachment.

function pepareAttachment( $file , $path ) {
        
    $filename = $path.$file ;
    $file = fopen($filename,"rb");
    $data = fread($file,filesize($filename));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $attachContent .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$filename\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$filename\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $attachContent .= "--{$mime_boundary}\n";

    return $attachContent;
    
}

Send files as attachment PHP

Send email using the mail() function

/* Copy the file from the Placeholder folder to Archived folder and remove the file from placeholder file */
function copyPlaceholderFileToArchived($file, $path , $destination) {
    if (copy($path.$file, $destination.$file)) {
        unlink($path.$file);
    }
    
}

Copy the files from one folder to another using PHP copy() function

/* Copy the file from the Placeholder folder to Archived folder and remove the file from placeholder file */
function copyPlaceholderFileToArchived($file, $path , $destination) {
    if (copy($path.$file, $destination.$file)) {
        unlink($path.$file);
    }
    
}

Delete the file from the source folder using unlink() function

If you have any further queries please post a comment below. Please check here to download the full code.

Please click here to download the full code.

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*