0

when I insert data to all of my fields in the formula ( except adding image in the upload file button) the email is sent but i recieve empty file in the email , and when 1 file or multi file is added to the form and submitted everything works fine and I recieve email with the multi file upload. so i you know how to prevent sending file when its empty tell me, I thought about putting the upload field for formula required , but it is not a solution , thanks alot

this is the html code for the form:

<div class="contact-form">
            <div id='message_post'></div>
            <?php echo $result; ?>
            <div class="col-lg-4"><input name="name" type="text" placeholder="Nom" class="forminput" required></div>
            <div class="col-lg-4"><input name="email" type="text" placeholder="Email" required></div>
            <div class="col-lg-4"><input name="phone" type="text" placeholder="Téléphone"></div>
            <div class="col-lg-12"><textarea name="comment" cols="1" rows="3" placeholder="Décrivez votre projet ici" required></textarea></div>
            <input type="file" name="fileToUpload[]" id="fileToUpload" style="display:none;" multiple/>
                <label for="fileToUpload" class="file-upload-label">Envoyez-nous vos photos</label>
                    <span id="file-chosen">Aucun fichier choisi</span>
            <input class="col-lg-12" id="website" name="website" type="text" value=""  />
            <div class="col-lg-12 buttons"><button class="btn" type='submit' value='Submit' name='submitf' id="submitf"><i class="fa fa-envelope"></i> Envoyer</button></div>
        </div>

this is the php code for sending email with multiupload

<?php
                
                error_reporting(E_ALL);
                ini_set('display_errors', 0);
                
                if(isset($_FILES) && (bool) $_FILES) {

                    $uname = $_POST['name'];
                    $uemail = $_POST['email'];
                    $uphone = $_POST['phone'];
                    $adminemail = "example@gmail.com";
                    $comments = stripslashes($_POST['comment']);
                    $person= 'Nom: ';
                    $email = 'Email: ';
                    $phone = 'Téléphone: ';
                    $es ='    ';
                    $msg = 'Message: ';

                    $AllowedExtensions = ["pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"];
                    $files = [];
                    $server_file = [];
                    foreach($_FILES as $name => $file) {
                        $file_name = $file["name"];
                        $file_temp = $file["tmp_name"];
                        foreach($file_name as $key) {
                            $path_parts = pathinfo($key);
                            $extension = strtolower($path_parts["extension"]);
                            if(!in_array($extension, $AllowedExtensions)) { echo '<div class="errorMessage"></div>'; }
                            $server_file[] = "upload/{$path_parts["basename"]}";
                        }
                        for($i = 0; $i<count($file_temp); $i++) { move_uploaded_file($file_temp[$i], $server_file[$i]); }
                    }
                    $to = $adminemail;
                    $from = $uemail;
                    $subject ="Contact Form: ";
                    $message = $person.$uname.$es.$email.$uemail.$es.$phone.$uphone.$es.$message.$comments;
                    $headers = "From: $from";
                    $semi_rand = md5(time());
                    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
                    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
                    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
                    $message .= "--{$mime_boundary}\n";
                    $FfilenameCount = 0;
                    for($i = 0; $i<count($server_file); $i++) {
                        $afile = fopen($server_file[$i],"rb");
                        $data = fread($afile,filesize($server_file[$i]));
                        fclose($afile);
                        $data = chunk_split(base64_encode($data));
                        $name = $file_name[$i];
                        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
                            "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
                            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
                        $message .= "--{$mime_boundary}\n";
                    }
                    if(mail($to, $subject, $message, $headers)) {
                        echo '<div class="successMessage">Message envoyé! Nous vous recontacterons dès que possible.</div>';
                    } else {
                        echo '<div class="errorMessage">ERREUR: Veuillez envoyer une autre fois !</div>';
                    }
                }   
                
                ?>
  • `isset($_FILES)` is a useless check and will always be true since that super global is always set and never null. – M. Eriksson Apr 28 '21 at 08:22
  • @MagnusEriksson do you have any tips how can I edit it ? so that when someone doesnt send file in the form it won't appear in the email – Zied Hadden Apr 29 '21 at 11:14
  • I wouldn't use the low level `mail()` function. I would rather use one of the tried an tested mail libraries, like PHPMailer, SwiftMailer or similar. They make it much easier to configure and send emails since you don't need to manually build the message (with boundaries etc), which removes a lot of the complexity (less places to make mistakes). – M. Eriksson Apr 29 '21 at 16:04

0 Answers0