diff --git a/htdocs/emailcollector/class/emailcollector.class.php b/htdocs/emailcollector/class/emailcollector.class.php index e2493a338fd..ab8d940192b 100644 --- a/htdocs/emailcollector/class/emailcollector.class.php +++ b/htdocs/emailcollector/class/emailcollector.class.php @@ -2267,6 +2267,14 @@ class EmailCollector extends CommonObject $errorforactions++; $this->error = 'Failed to create ticket: '.$langs->trans($tickettocreate->error); $this->errors = $tickettocreate->errors; + } else { + if ($attachments) { + $destdir = $conf->ticket->dir_output.'/'.$tickettocreate->ref; + if (!dol_is_dir($destdir)) { + dol_mkdir($destdir); + $this->getmsg($connection, $imapemail, $destdir); + } + } } } } @@ -2492,7 +2500,7 @@ class EmailCollector extends CommonObject * @param string $mid prefix * @return array Array with number and object */ - private function getmsg($mbox, $mid) + private function getmsg($mbox, $mid, $destdir = '') { // input $mbox = IMAP stream, $mid = message id // output all the following: @@ -2513,7 +2521,7 @@ class EmailCollector extends CommonObject } else { // multipart: cycle through each part foreach ($s->parts as $partno0 => $p) { - $this->getpart($mbox, $mid, $p, $partno0 + 1); + $this->getpart($mbox, $mid, $p, $partno0 + 1, $destdir); } } } @@ -2542,7 +2550,7 @@ class EmailCollector extends CommonObject * @param string $partno Partno * @return void */ - private function getpart($mbox, $mid, $p, $partno) + private function getpart($mbox, $mid, $p, $partno, $destdir = '') { // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple global $htmlmsg, $plainmsg, $charset, $attachments; @@ -2580,6 +2588,38 @@ class EmailCollector extends CommonObject $filename = ($params['filename']) ? $params['filename'] : $params['name']; // filename may be encoded, so see imap_mime_header_decode() $attachments[$filename] = $data; // this is a problem if two files have same name + + // Get file name (with extension) + $file_name_complete = $params['filename']; + + + $destination = $destdir.'/'.$file_name_complete; + + // Extract file extension + $extension = pathinfo($file_name_complete, PATHINFO_EXTENSION); + + // Extract file name without extension + $file_name = pathinfo($file_name_complete, PATHINFO_FILENAME); + + // Save an original file name variable to track while renaming if file already exists + $file_name_original = $file_name; + + // Increment file name by 1 + $num = 1; + + /** + * Check if the same file name already exists in the upload folder, + * append increment number to the original filename + */ + while (file_exists($destdir."/" . $file_name . "." . $extension)) { + $file_name = (string) $file_name_original . ' (' . $num . ')'; + $file_name_complete = $file_name . "." . $extension; + $destination = $destdir.'/'.$file_name_complete; + $num++; + } + + + file_put_contents($destination, $data); } // TEXT