在电子邮件附件中只获取一个文件而不是多个附件

Getting only one file in email attachment instead of multiple attachment

我必须附加多个文件并将所有文件与附件一起发送到电子邮件中。我在电子邮件附件中只有一个文件。

这是我的代码:-

<?php

       $sName = $_POST['txtName'];
       $sTimetocall = $_POST['txtTimetocall'];
       $sPhone = $_POST['txtPhone'];
       $sEmail = $_POST['txtEmail'];

       $sBrand1 = $_POST['txtBrand1'];
       $sDescription1 = $_POST['txtDescription1'];
       $sCondition1 = $_POST['radio11'];

       $sBrand2 = $_POST['txtBrand2'];
       $sDescription2 = $_POST['txtDescription2'];
       $sCondition2 = $_POST['radio12'];

       $sBrand3 = $_POST['txtBrand3'];
       $sDescription3 = $_POST['txtDescription3'];
       $sCondition3 = $_POST['radio13'];

       $sBrand4 = $_POST['txtBrand4'];
       $sDescription4 = $_POST['txtDescription4'];
       $sCondition4 = $_POST['radio14'];

        $file_array = array();
        $content = array();
        $upload_array = array();
        $total = count($_FILES['file']['name']);
        for( $i=0 ; $i < $total ; $i++ ) {
            $file=$_FILES['file']['name'][$i]; 
            $file_array[] = $file;
            $fileArr=explode('.',$file); 
            $rand=rand(10000,99999);
            $cur_dir = getcwd();
            $newFileName=$fileArr[0].$rand.'.'.$fileArr[1];
            $uploadPath= $cur_dir."/uploads/".$newFileName;
            $upload_array[] = $uploadPath;
            $path = $cur_dir."/uploads/";
            // $isUploaded=move_uploaded_file($_FILES["file"]["tmp_name"],$uploadPath);
            $file_type = $_FILES['file']['type'][$i];
            $file_tmp =$_FILES['file']['tmp_name'][$i];

            if (move_uploaded_file($file_tmp,$path.$newFileName))
                {
                    $newFileName;
                }
            $content[$i] = file_get_contents($uploadPath);
            $content[$i] = chunk_split(base64_encode($content[$i]));
        }
        $file_name = implode(", ",$file_array);

        $separator = md5(time());
        $eol = PHP_EOL;

       $sComment = $_POST['txtComments'];
       $captcha=$_POST['g-recaptcha-response'];



        $secretKey = "6LdG*****";

        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha);
        $responseKeys = json_decode($response,true);

        if(intval($responseKeys["success"]) !== 1) {
          echo '<h2>Invalid Captcha</h2>';
        } else {

       $body = "<html>
        <head>
        <title></title>
        </head>
        <body>
        <p>Name:" . $sName . "</p>
        <p>Phone:" . $sPhone . "</p>
        <br>
        <br>
        <h2>Equipment Details</h2>
        <br>
        <br>
        <p>Brand:" . $sBrand1 . "</p>
        <p>Product:" .  $sDescription1 . "</p>
        <p>Condition:" .  $sCondition1 . "</p>
        <p>Images:".  $file_name  .  "</p>
        ---------------------------------------------------------------------------------
        <p>Brand:" . $sBrand2 . "</p>
        <p>Product:" .  $sDescription2 . "</p>
        <p>Condition:" .  $sCondition2 . "</p>
        <p>Images:".  $file_name  .  "</p>
        ---------------------------------------------------------------------------------
        <p>Brand:" . $sBrand3 . "</p>
        <p>Product:" .  $sDescription3 . "</p>
        <p>Condition:" .  $sCondition3 . "</p>
        <p>Images:".  $file_name  .  "</p>
        ---------------------------------------------------------------------------------
        <p>Brand:" . $sBrand4 . "</p>
        <p>Product:" .  $sDescription4 . "</p>
        <p>Condition:" .  $sCondition4 . "</p>
        <p>Images:".  $file_name  .  "</p>
        ---------------------------------------------------------------------------------
        <p>Comments:" .  $sComment . "</p>
        </body>
        </html>";



       // we'll begin by assigning the To address and message subject

          $from = $sEmail;

          $to="test@gmail.com;".$sEmail;
          $subject="Used Equipment Inquiry from ".$sName;


          $headers = "MIME-Version: 1.0" . "\r\n";
          $headers .= "Content-type:multipart/mixed;boundary=\"" . $separator . "\"";
        // $headers .= "Content-Transfer-Encoding: 7bit" . $eol;

        $message = "--".$separator.$eol;
        $message .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
        $message .= "Content-Transfer-Encoding: 8bit" . $eol.$eol;
        $message .= $body.$eol;


        // print_r($upload_array);
        foreach($upload_array as $key => $fl) {
            $content = file_get_contents($fl);
            $content = chunk_split(base64_encode($content));
            echo $fl.'<br/>';

            $message .= "--".$separator.$eol;
            $message .= "Content-Type:image/jpeg; name=\"" .$fl. "\"" .$eol;
            $message .= "Content-Transfer-Encoding: base64" . $eol;
            $message .= "Content-Disposition: attachment; filename=\"".$fl."\"".$eol.$eol;
            $message .= $content . $eol;
            $message .= "--".$separator."--";

        }
          // now we just send the message
          if (@mail($to, $subject, $message, $headers))
          {
            ?>
                <script>
                    alert("Mail Sent Successfully.");
                    document.location = "../../../../query_submission_thanks";
                </script>
            <?php }

        else
            {
            ?>
                <script>
                    alert("Message Not Sent, Please Try Again.");
                    document.location = "../../../../../UsedEquipmentQuote2";
                </script>
        <?php }}
?>

文件附件代码是这样的:-

foreach($upload_array as $key => $fl) {
            $content = file_get_contents($fl);
            $content = chunk_split(base64_encode($content));
            echo $fl.'<br/>';

            $message .= "--".$separator.$eol;
            $message .= "Content-Type:image/jpeg; name=\"" .$fl. "\"" .$eol;
            $message .= "Content-Transfer-Encoding: base64" . $eol;
            $message .= "Content-Disposition: attachment; filename=\"".$fl."\"".$eol.$eol;
            $message .= $content . $eol;
            $message .= "--".$separator."--";

        }

我在此 $upload_array 循环中显示所有文件名。但是在电子邮件中有单个文件显示。

有谁知道怎么解决吗?提前致谢。

可能问题是您在 foreach 循环中使用了 $separator 变量,所以

只需在循环外尝试 $separator 变量,例如

foreach($upload_array as $key => $fl) {
     $type=$_FILES['file']['name'][$key];

    $content = file_get_contents($fl);
    $content = chunk_split(base64_encode($content));
    echo $fl.'<br/>';

    $message .= "--".$separator.$eol;
    $message .= "Content-Type:".$type."; name=\"" .$fl. "\"" .$eol;
    $message .= "Content-Transfer-Encoding: base64" . $eol;
    $message .= "Content-Disposition: attachment; filename=\"".$fl."\"".$eol.$eol;
    $message .= $content . $eol;
}

$message .= "--".$separator."--";

希望对你有帮助