如何附加多张图片?

时间:2019-01-22 00:46:09

标签: php email

我有一个脚本,可以调整最多5张上载图像的大小。它们被写入一个数组,我想将它们都附加到电子邮件中。我有以下脚本,但它仅附加1张图像,我需要在哪里循环以附加所有图像?

$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>';
}

1 个答案:

答案 0 :(得分:0)

您希望对每个文件重复此行

$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";

因此,请从顶部删除这些行

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

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

并且您的 //邮件和附件块变为

// 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."--";
}