Question Locked

Sending email with multiple attachment is not working using php mail

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi Team

I really need some help, i have code that send multiple attachment its using php mail function. The email does gets send but the problem it only shows total size attachment. What could be the problem and need help

// 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 = 'xxx@yyy.org.za';
$from = 'zzz@gmail.com';
$fromName = "abcde";

5 replies

by luckieday11

I can help you with that! It seems like the problem is that the email is only showing the total size of the attachments instead of showing each attachment separately. One possible solution is to modify the Content-Disposition header in the attachment section of the code.

Try changing this line:
“Content-Disposition: attachment;\n” . ” filename=\””.$file_name.”\”; size=”.$file_size.”;\n”

to this line:
“Content-Disposition: attachment; filename=\””.$file_name.”\”\n” .
“Content-Length: “.$file_size.”\n” .
“Content-Type: application/octet-stream; name=\””.$file_name.”\”\n” .
“Content-Transfer-Encoding: base64\n\n” . $data . “\n\n”;

This should add the Content-Length header to each attachment, which will make the email client show each attachment separately with its own size. Let me know if it works for you!

by Gcobani Mkontwana

Ok i hear you, let me test it then will let you. Oh almost forgot, input data from the form will be send here? So i dont need to map again those fields from the form on the back end? Just the tought of submit correct values as attached document to the user

This discussion is closed to new replies.