Skip to content

Commit

Permalink
Collector : can now specify where the requester's email is found (#4554)
Browse files Browse the repository at this point in the history
'from' : current behavior
'reply-to' : use the email found in the 'reply-to' header
  • Loading branch information
AdrienClairembault authored and trasher committed Sep 6, 2018
1 parent 2e3aeb6 commit 20bc633
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
49 changes: 39 additions & 10 deletions inc/mailcollector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -297,6 +300,14 @@ function showForm($ID, $options = []) {
Dropdown::showYesNo("use_mail_date", $this->fields["use_mail_date"]);
echo "</td></tr>\n";

echo "<tr class='tab_bg_1'><td>" . __('Field for requester') . "</td>";
echo "<td>";
Dropdown::showFromArray("requester_field", [
self::REQUESTER_FIELD_FROM => __('From'),
self::REQUESTER_FIELD_REPLY_TO => __('Reply-To')
], ["value" => $this->fields['requester_field']]);
echo "</td></tr>\n";

echo "<tr class='tab_bg_1'><td>".__('Comments')."</td>";
echo "<td><textarea cols='45' rows='5' name='comment' >".$this->fields["comment"]."</textarea>";

Expand Down Expand Up @@ -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']);
Expand All @@ -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'])) {
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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";
}
}

}
1 change: 1 addition & 0 deletions install/mysql/glpi-empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`),
Expand Down
6 changes: 6 additions & 0 deletions install/update_93_94.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 20bc633

Please sign in to comment.