Search code examples
phpfileemailsendmailattachment

Attach multiples files with sendmail PHP


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.


Solution

  • 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.