0

I'm testing the mail() function with sendmail. I'm working with xampp, and my website is on wordpress. My mail() function works when there isn't files attached to a message, but when i tried to attach multiple files in my message, i can see this content in the mail :

Content-Type: {"application/octet-stream"};
 name="/tmp/phpmXXXXX"
Content-Disposition: attachment;
 filename="/tmp/phpmXXXXX"
Content-Transfer-Encoding: base64

JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu [etc..]

I'm looking for a solution in my PHP code, there is the part about files attachments :

$files = array(); //Array pour les fichiers
                $filesSize = array(); //Array pour la taille total des fichiers

                $count = count(array_filter($_FILES['fichier']['name'])); //Compte le nombre de fichiers

                for($i=0;$i<$count;$i++){ //boucle sur chaque fichier

                    if($_FILES['fichier']['name'] != ".htaccess"){ //Vérifie que ce n'est pas un .htaccess
                        array_push($files, $_FILES['fichier']['tmp_name'][$i]); //insere le fichier dans l'array $files
                        array_push($filesSize, $_FILES['fichier']['size'][$i]); //insere le fichier dans l'array $filesSize (pour le calcul de la taille total)
                    }

                }



if(isset($_FILES["fichier"]) &&  $_FILES['fichier']['name'] != ""){


                $semi_rand = md5(time());
                $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

                // headers pour les pièces jointes
                $headers .= "MIME-Version: 1.0" . "Content-Type: multipart/mixed;" . " boundary=\"{$mime_boundary}\"";



                // preparation des pièces jointes
                for($x=0;$x<count($files);$x++){
                    $file = fopen($files[$x],"rb");
                    $data = fread($file,filesize($files[$x]));
                    fclose($file);
                    $data = chunk_split(base64_encode($data));
                    $message .= '----------------------------------------------------------' . "\r\n";
                    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
                    "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
                    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
                    $message .= "--{$mime_boundary}\n";
                }
            }

If there isn't any solutions, maybe i'll install PHPMailer.

Thanks guys.

Tweak
  • 167
  • 1
  • 3
  • 18
  • Please do yourself a favour and use [PHPMailer](https://github.com/PHPMailer/PHPMailer). I cannot but quote what has already been [written](https://stackoverflow.com/a/12302354/7659430) by another [user](https://stackoverflow.com/users/1069607/sdc) on this subject: PHPMailer **is** the easier option by a very large margin compared to trying to do it yourself with PHP's built-in `mail()` function. – simlev Jun 05 '18 at 07:45
  • You don't install PHPMailer, you just require it. – hungrykoala Jun 05 '18 at 07:45

1 Answers1

0

You're on the right track with PHPMailer. It makes all of this type of thing so much easier. There is no need to reinvent the wheel. In the time you spend fighting with your bug you could install PHPMailer and be sending email.

I've written a lot of email code just to avoid using libraries like PHPMailer. I always had one issue or another. I stopped wasting my time and haven't looked back.

Mr Glass
  • 1,186
  • 1
  • 6
  • 14
  • 1
    Ok, i understand, it seemed to me interesting to developped this kind of function. I will try PHPMailer then.. Thanks Mr Glass – Tweak Jun 05 '18 at 07:55