0

I have a script that resizes up to 5 uploaded images. These are written into an array and I'd like to attach all of them to an email. I have the following script but it is only attaching 1 image, where do I need to loop to attach all of them?

$path = "./uploads/";
foreach ($filenameresized as $filename) {
    $file = $path.$filename;
}

$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$subject = "ENQUIRY - Evolve My Boiler (" . $name . " - " . $date . ")" ;
$from_name = "Evolve Maintenance Ltd";
//$from_mail = "info@evolvemaintenance.co.uk, mail@helenlee.co.uk";
$from_mail = "mail@helenlee.co.uk";
$replyto = "";
$cc = "";
$bcc = "";
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; // generate a random string to be used as the boundary marker
$file_count = count($filenameresized); //count total files attached
$boundary = md5("specialToken$4332"); // boundary token to be used

// header
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

// message body
$message = "<b>Hello</b>";

// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-Type: text/html;charset=utf-8\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\n\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";


if (mail($from_mail, $subject, $nmessage, $header)) {
    return true; // Or do something here
} else {
    echo 'Oops something went wrong - please <a href="find-your-boiler.php">try again</a>';
}
Vega
  • 27,856
  • 27
  • 95
  • 103
Helen Lee
  • 39
  • 7

1 Answers1

0

You haev to repeat this line for each file

$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";

So remove these lines off the top

$path = "./uploads/";
foreach ($filenameresized as $filename) {
    $file = $path.$filename;
}

$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));

And your // message & attachment block becomes

// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-Type: text/html;charset=utf-8\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\n\n";
$nmessage .= $message."\r\n\r\n";

$path = "./uploads/";
foreach ($filenameresized as $filename) {
    $nmessage .= "--".$uid."\r\n";
    $file = $path.$filename;
    $content = file_get_contents( $file);
    $content = chunk_split(base64_encode($content));
    $nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
    $nmessage .= "Content-Transfer-Encoding: base64\r\n";
    $nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $nmessage .= $content."\r\n\r\n";
    $nmessage .= "--".$uid."--";
}
Vinay
  • 7,442
  • 6
  • 25
  • 48
  • Thanks so much, that looks great but unfortunately the email downloads about the right size but there are no attachments at all ? – Helen Lee Jan 22 '19 at 09:27
  • May be try some suggestions over here https://stackoverflow.com/questions/22245082/how-to-attach-two-or-multiple-files-and-send-mail-in-php – Vinay Jan 22 '19 at 16:05