Получение только одного файла во вложении электронной почты вместо нескольких вложений - PullRequest
1 голос
/ 16 июня 2020

Мне нужно прикрепить несколько файлов и отправить все файлы по электронной почте с вложением. У меня только один файл во вложении к письму.

Вот мой код: -

<?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 л oop. Но в электронном письме отображается один файл.

Кто-нибудь знает как решить? Заранее спасибо.

1 Ответ

1 голос
/ 16 июня 2020

Возможно, проблема в том, что вы использовали $separator переменную внутри foreach l oop поэтому

просто попробуйте $separator переменную вне l oop как

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

Надежда это помогает вам

...