mail function sends an empty attachments with total size only no document

gcozba2023

New member
Hi Team

I need some help, i am trying to send an email attached with multiple files. Somehow when an email gets sends there is no attachment on the email only shows total size. How can i achieve this on my script below and i am using mail function to do this.

// mail function to send multiple files
Code:
<?php
function multi_attach_mail($to, $subject, $message, $senderEmail, $senderName, $received_email_send = array()){ 
    
    // Sender info  
    $from = $senderName." <".$senderEmail.">";  
    $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 = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";  
 
    // Preparing attachment 
    if(!empty($received_email_send)){ 
        for($i=0;$i<count($received_email_send);$i++){ 
            if(is_file($received_email_send[$i])){ 
                $file_name = basename($received_email_send[$i]); 
                $file_size = filesize($received_email_send[$i]); 
                 
                $message .= "--{$mime_boundary}\n"; 
                $fp =    @fopen($received_email_send[$i], "rb"); 
                $data =  @fread($fp, $file_size); 
                @fclose($fp); 
                $data = chunk_split(base64_encode($data)); 
                $message .= "Content-Type: application/octet-stream; name=\"".$file_name."\"\n" .  
                "Content-Description: ".$file_name."\n" . 
                "Content-Disposition: attachment;\n" . " filename=\"".$file_name."\"; size=".$file_size.";\n" .  
                "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
            } 
        } 
    } 
     
    $message .= "--{$mime_boundary}--"; 
    $returnpath = "-f" . $senderEmail; 
     
    // Send email 
    $mail = mail($to, $subject, $message, $headers, $returnpath);  
     
    // Return true if email sent, otherwise return false 
    if($mail){ 
        return true; 
    }else{ 
        return false; 
    } 
}


// Email configuration 
$to = 'gcobani.mkontwana@agilelimitless.org.za'; 
$from = 'ggcobani@gmail.com'; 
$fromName = 'ACI Finance Pty Ltd'; 
 
$subject = 'Application Loan with ACI Finance Pty Ltd';  
 
// Attachment files 
$received_email_send = array( 
    'received_email_send/bankstatement.pdf', 
    'received_email_send/id.pdf',
    'received_email_send/payslip.pdf'
); 
 
$htmlContent = ' 
    <h3></h3> 
    <h4></h4> 
    <p><b>Total Attachments:</b> '.count($received_email_send).'</p>'; 
 
// Call function and pass the required arguments 
$sendEmail = multi_attach_mail($to, $subject, $htmlContent, $from, $fromName, $received_email_send); 
 
// Email sending status 
if($sendEmail){ 
    echo 'The email is sent successfully.'; 
}else{ 
    echo 'Email sending failed!'; 
}
?>

// application for a loan this calls first and thereafter calls first one on top for attachment(but this becames second email with no attachment)
 
    require_once 'send-email-attachment.php';
    
    $logo = 'assets/img/logo.png';
    $link = 'https://acifinance.co.za/';
	$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>ACI Finance</title></head><body>";
	$body .= "<table style='width: 100%;'>";
	$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
	$body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
	$body .= "</td></tr></thead><tbody><tr>";
	$body .= "<td style='border:none;'><strong>Name:</strong> {$fname} {$lname}</td>";
	$body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
	$body .= "</tr>";
	$body .= "<tr><td style='border:none;'><strong>CellNumber:</strong> {$number}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'><strong>Loan Amount:</strong> {$amount}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'><strong>Monthly income:</strong> {$income}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'><strong>Town:</strong> {$town}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'><strong>Upload bankstatement </strong> {$bankstatement}</td></tr>";

	// attachment
 $body .= "--" . $separator . $eol;
 $body .= "Content-Type: application/pdf; name=\"" . $fileName . "\"" . $eol;
 $body .= "Content-Transfer-Encoding: base64" . $eol;
 $body .= "Content-Disposition: attachment" . $eol;
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: application/pdf; charset=iso-8859-1';
$body .= $content . $eol;
$body .= "--" . $separator . "--";
$body .= "</tbody></table>";
	
$body .= "</body></html>";
	
    
    $send = mail($to, $subject, $body, $headers);
 
Back
Top