From 20bc633a867f9bf7728430bf93bc6899ac2cfb5f Mon Sep 17 00:00:00 2001 From: AdrienClairembault <42734840+AdrienClairembault@users.noreply.github.com> Date: Thu, 6 Sep 2018 10:50:15 +0200 Subject: [PATCH] Collector : can now specify where the requester's email is found (#4554) 'from' : current behavior 'reply-to' : use the email found in the 'reply-to' header --- inc/mailcollector.class.php | 49 ++++++++++++++++++++++++++++-------- install/mysql/glpi-empty.sql | 1 + install/update_93_94.php | 6 +++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/inc/mailcollector.class.php b/inc/mailcollector.class.php index be010482ea1..b7da77758c3 100644 --- a/inc/mailcollector.class.php +++ b/inc/mailcollector.class.php @@ -82,6 +82,9 @@ class MailCollector extends CommonDBTM { const REFUSED_FOLDER = 'refused'; const ACCEPTED_FOLDER = 'accepted'; + // Values for requester_field + const REQUESTER_FIELD_FROM = 0; + const REQUESTER_FIELD_REPLY_TO = 1; static function getTypeName($nb = 0) { return _n('Receiver', 'Receivers', $nb); @@ -297,6 +300,14 @@ function showForm($ID, $options = []) { Dropdown::showYesNo("use_mail_date", $this->fields["use_mail_date"]); echo "\n"; + echo "" . __('Field for requester') . ""; + echo ""; + Dropdown::showFromArray("requester_field", [ + self::REQUESTER_FIELD_FROM => __('From'), + self::REQUESTER_FIELD_REPLY_TO => __('Reply-To') + ], ["value" => $this->fields['requester_field']]); + echo "\n"; + echo "".__('Comments').""; echo ""; @@ -634,7 +645,7 @@ function collect($mailgateID, $display = 0) { $rejinput = []; $rejinput['mailcollectors_id'] = $mailgateID; if (!$tkt['_blacklisted']) { - $rejinput['from'] = $tkt['_head']['from']; + $rejinput['from'] = $tkt['_head'][$this->getRequesterField()]; $rejinput['to'] = $tkt['_head']['to']; $rejinput['users_id'] = $tkt['_users_id_requester']; $rejinput['subject'] = $this->textCleaner($tkt['_head']['subject']); @@ -653,7 +664,7 @@ function collect($mailgateID, $display = 0) { // entities_id set when new ticket / tickets_id when new followup if (isset($tkt['_refuse_email_with_response'])) { - $this->sendMailRefusedResponse($tkt['_head']['from'], $tkt['name']); + $this->sendMailRefusedResponse($tkt['_head'][$this->getRequesterField()], $tkt['name']); $delete_mail = self::REFUSED_FOLDER; $refused++; } else if (isset($tkt['_refuse_email_no_response'])) { @@ -792,12 +803,13 @@ function buildTicket($uid, $options = []) { Toolbox::logInFile('mailgate', sprintf(__('%s is not writable'), GLPI_TMP_DIR."/")); } } + // Who is the user ? - $tkt['_users_id_requester'] = User::getOrImportByEmail($head['from']); + $tkt['_users_id_requester'] = User::getOrImportByEmail($head[$this->getRequesterField()]); $tkt["_users_id_requester_notif"]['use_notification'][0] = 1; // Set alternative email if user not found / used if anonymous mail creation is enable if (!$tkt['_users_id_requester']) { - $tkt["_users_id_requester_notif"]['alternative_email'][0] = $head['from']; + $tkt["_users_id_requester_notif"]['alternative_email'][0] = $head[$this->getRequesterField()]; } // Fix author of attachment @@ -807,7 +819,7 @@ function buildTicket($uid, $options = []) { // Add to and cc as additional observer if user found if (count($head['ccs'])) { foreach ($head['ccs'] as $cc) { - if (($cc != $head['from']) + if (($cc != $head[$this->getRequesterField()]) && !Toolbox::inArrayCaseCompare($cc, $blacklisted_emails) // not blacklisted emails && (($tmp = User::getOrImportByEmail($cc)) > 0)) { $nb = (isset($tkt['_users_id_observer']) ? count($tkt['_users_id_observer']) : 0); @@ -820,7 +832,7 @@ function buildTicket($uid, $options = []) { if (count($head['tos'])) { foreach ($head['tos'] as $to) { - if (($to != $head['from']) + if (($to != $head[$this->getRequesterField()]) && !Toolbox::inArrayCaseCompare($to, $blacklisted_emails) // not blacklisted emails && (($tmp = User::getOrImportByEmail($to)) > 0)) { $nb = (isset($tkt['_users_id_observer']) ? count($tkt['_users_id_observer']) : 0); @@ -915,12 +927,12 @@ function buildTicket($uid, $options = []) { && ($job->fields['status'] != CommonITILObject::CLOSED) && ($CFG_GLPI['use_anonymous_followups'] || ($tkt['_users_id_requester'] > 0) - || $tu->isAlternateEmailForITILObject($tkt['tickets_id'], $head['from']) + || $tu->isAlternateEmailForITILObject($tkt['tickets_id'], $head[$this->getRequesterField()]) || ($tkt['_supplier_email'] = $st->isSupplierEmail($tkt['tickets_id'], - $head['from'])))) { + $head[$this->getRequesterField()])))) { if ($tkt['_supplier_email']) { - $tkt['content'] = sprintf(__('From %s'), $head['from'])."\n\n".$tkt['content']; + $tkt['content'] = sprintf(__('From %s'), $head[$this->getRequesterField()])."\n\n".$tkt['content']; } $content = explode("\n", $tkt['content']); @@ -1286,6 +1298,7 @@ function getHeaders($uid) { $sender = $mail_header->from[0]; $to = $mail_header->to[0]; + $reply_to = $mail_header->reply_to[0]; $date = date("Y-m-d H:i:s", strtotime($mail_header->date)); $mail_details = []; @@ -1318,7 +1331,8 @@ function getHeaders($uid) { $mail_details = ['from' => Toolbox::strtolower($sender->mailbox).'@'.$sender->host, 'subject' => $mail_header->subject, - 'to' => Toolbox::strtolower($to->mailbox).'@'.$to->host, + 'reply-to' => Toolbox::strtolower($reply_to->mailbox).'@'.$reply_to->host, + 'to' => Toolbox::strtolower($to->mailbox).'@'.$to->host, 'message_id' => $mail_header->message_id, 'tos' => $tos, 'ccs' => $ccs, @@ -1956,4 +1970,19 @@ static public function unsetUndisclosedFields(&$fields) { unset($fields['passwd']); } + /** + * Get the requester field + * + * @return string requester field + **/ + private function getRequesterField() { + switch ($this->fields['requester_field']) { + case self::REQUESTER_FIELD_REPLY_TO: + return "reply-to"; + + default: + return "from"; + } + } + } diff --git a/install/mysql/glpi-empty.sql b/install/mysql/glpi-empty.sql index 0bce619541f..316dcf749f1 100644 --- a/install/mysql/glpi-empty.sql +++ b/install/mysql/glpi-empty.sql @@ -4294,6 +4294,7 @@ CREATE TABLE `glpi_mailcollectors` ( `errors` int(11) NOT NULL DEFAULT '0', `use_mail_date` tinyint(1) NOT NULL DEFAULT '0', `date_creation` datetime DEFAULT NULL, + `requester_field` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `is_active` (`is_active`), KEY `date_mod` (`date_mod`), diff --git a/install/update_93_94.php b/install/update_93_94.php index 9a21d5b6f5d..2721ccb03f7 100644 --- a/install/update_93_94.php +++ b/install/update_93_94.php @@ -61,6 +61,12 @@ function update93to94() { } /** /Add default group for a user */ + /** Add requester field on glpi_mailcollectors */ + $migration->addField("glpi_mailcollectors", "requester_field", "integer", [ + 'value' => '0' + ]); + /** /Add requester field on glpi_mailcollectors */ + /** Add business rules on assets */ $rule = ['name' => 'Domain user assignation', 'is_active' => 1,