From 4065b3f1dfc0514f22ff9231a029f679c941fad0 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Fri, 13 Nov 2015 00:19:11 +0100
Subject: [PATCH] Better management of the Dolibarr tracking id into email.
 This will allow an imap scanner to link email

---
 htdocs/admin/mails.php                |  8 ++++++--
 htdocs/core/actions_sendmails.inc.php | 18 +++++++++---------
 htdocs/core/class/CMailFile.class.php |  6 ++++--
 htdocs/core/class/smtps.class.php     |  6 ++++--
 htdocs/core/lib/functions.lib.php     |  4 ++++
 htdocs/theme/eldy/style.css.php       |  2 +-
 6 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/htdocs/admin/mails.php b/htdocs/admin/mails.php
index 271605830bc..b084f21164c 100644
--- a/htdocs/admin/mails.php
+++ b/htdocs/admin/mails.php
@@ -152,7 +152,8 @@ if (($action == 'send' || $action == 'sendhtml') && ! GETPOST('addfile') && ! GE
 	$subject    = $_POST['subject'];
 	$body       = $_POST['message'];
 	$deliveryreceipt= $_POST["deliveryreceipt"];
-
+	$trackid    = GETPOST('trackid');
+	
 	//Check if we have to decode HTML
 	if (!empty($conf->global->FCKEDITOR_ENABLE_MAILING) && dol_textishtml(dol_html_entity_decode($body, ENT_COMPAT | ENT_HTML401))) {
 		$body=dol_html_entity_decode($body, ENT_COMPAT | ENT_HTML401);
@@ -202,7 +203,9 @@ if (($action == 'send' || $action == 'sendhtml') && ! GETPOST('addfile') && ! GE
             $sendtoccc,
             $deliveryreceipt,
             $msgishtml,
-            $errors_to
+            $errors_to,
+        	'',
+        	$trackid	
         );
 
 		$result=$mailfile->sendfile();
@@ -693,6 +696,7 @@ else
 		$formmail = new FormMail($db);
 		$formmail->fromname = (isset($_POST['fromname'])?$_POST['fromname']:$conf->global->MAIN_MAIL_EMAIL_FROM);
 		$formmail->frommail = (isset($_POST['frommail'])?$_POST['frommail']:$conf->global->MAIN_MAIL_EMAIL_FROM);
+		$formmail->trackid='test';
 		$formmail->withfromreadonly=0;
 		$formmail->withsubstit=0;
 		$formmail->withfrom=1;
diff --git a/htdocs/core/actions_sendmails.inc.php b/htdocs/core/actions_sendmails.inc.php
index a4e83a0fd59..1f96d085bb8 100644
--- a/htdocs/core/actions_sendmails.inc.php
+++ b/htdocs/core/actions_sendmails.inc.php
@@ -221,11 +221,12 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
 			$filename = $attachedfiles['names'];
 			$mimetype = $attachedfiles['mimes'];
 
-			$trackid = GETPOST('trackid','aZ');
+			$trackid = GETPOST('trackid','aZ09');
 
-			if($conf->dolimail->enabled)
+			// Feature to push mail sent into Sent folder
+			if (! empty($conf->dolimail->enabled))
 			{
-				$mailfromid = explode ("#", $_POST['frommail'],3);
+				$mailfromid = explode("#", $_POST['frommail'],3);	// $_POST['frommail'] = 'aaa#Sent# <aaa@aaa.com>'	// TODO Use a better way to define Sent dir.
 				if (count($mailfromid)==0) $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>';
 				else
 				{
@@ -243,7 +244,7 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
 					{
 					
 						$folder=str_replace($ref, '', $mailboxconfig->folder_cache_key);
-						if (!$folder) $folder = "Sent";
+						if (!$folder) $folder = "Sent";	// Default Sent folder
 					
 						$mailboxconfig->mbox = imap_open($mailboxconfig->get_connector_url().$folder, $mailboxconfig->mailbox_imap_login, $mailboxconfig->mailbox_imap_password);
 						if (FALSE === $mailboxconfig->mbox) 
@@ -276,17 +277,16 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
 				if ($result)
 				{
 					$error=0;
-					if($conf->dolimail->enabled)
+					if (! empty($conf->dolimail->enabled))
 					{
-						$mid = (GETPOST('mid','int') ? GETPOST('mid','int') : 0);
+						$mid = (GETPOST('mid','int') ? GETPOST('mid','int') : 0);	// Original mail id is set ?
 						if ($mid)
 						{
-							// set imap flag answered if it is a answered mail
-							
+							// set imap flag answered if it is an answered mail
 							$dolimail=new DoliMail($db);
 							$dolimail->id = $mid;
 							$res=$dolimail->set_prop($user, 'answered',1);
-				  	}	
+				  		}	
 						if ($imap==1)
 						{
 							// write mail to IMAP Server
diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php
index b3da46ff366..82d1444ff73 100644
--- a/htdocs/core/class/CMailFile.class.php
+++ b/htdocs/core/class/CMailFile.class.php
@@ -708,8 +708,10 @@ class CMailFile
 		$trackid = $this->trackid;
 		if ($trackid)
 		{
-			$out.= 'Message-ID: <' . time() . '.phpmail-'.$trackid.'@' . $host . ">" . $this->eol2;
-			$out.= 'references: <' . time() . '.phpmail-'.$trackid.'@' . $host . ">" . $this->eol2;
+			// References is kept in response and Message-ID is returned into In-Reply-To:
+			$out.= 'Message-ID: <' . time() . '.phpmail-dolibarr-'.$trackid.'@' . $host . ">" . $this->eol2;	// Uppercase seems replaced by phpmail
+			$out.= 'References: <' . time() . '.phpmail-dolibarr-'.$trackid.'@' . $host . ">" . $this->eol2;
+			$out.= 'X-Dolibarr-TRACKID: '.$trackid. $this->eol2;
 		}
 		else
 		{
diff --git a/htdocs/core/class/smtps.class.php b/htdocs/core/class/smtps.class.php
index 51d9dd621dc..dd2726d2306 100644
--- a/htdocs/core/class/smtps.class.php
+++ b/htdocs/core/class/smtps.class.php
@@ -1133,8 +1133,10 @@ class SMTPs
 		$trackid = $this->getTrackId();
 		if ($trackid)
 		{
-			$_header .= 'Message-ID: <' . time() . '.SMTPs-'.$trackid.'@' . $host . ">\r\n";
-			$_header .= 'references: <' . time() . '.SMTPs-'.$trackid.'@' . $host . ">\r\n";
+			// References is kept in response and Message-ID is returned into In-Reply-To:
+			$_header .= 'Message-ID: <' . time() . '.SMTPs-dolibarr-'.$trackid.'@' . $host . ">\r\n";
+			$_header .= 'References: <' . time() . '.SMTPs-dolibarr-'.$trackid.'@' . $host . ">\r\n";
+			$_header .= 'X-Dolibarr-TRACKID: ' . $trackid . "\r\n";
 		}
 		else
 		{
diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index 013217a9f57..2eae6360ffa 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -263,6 +263,10 @@ function GETPOST($paramname,$check='',$method=0,$filter=NULL,$options=NULL)
 	            $out=trim($out);
 	            if (preg_match('/[^a-z]+/i',$out)) $out='';
 	            break;
+	        case 'aZ09':
+	            $out=trim($out);
+	            if (preg_match('/[^a-z0-9]+/i',$out)) $out='';
+	            break;
 	        case 'array':
 	            if (! is_array($out) || empty($out)) $out=array();
 	            break;
diff --git a/htdocs/theme/eldy/style.css.php b/htdocs/theme/eldy/style.css.php
index b57fa159b17..7f97637cf55 100644
--- a/htdocs/theme/eldy/style.css.php
+++ b/htdocs/theme/eldy/style.css.php
@@ -232,7 +232,7 @@ body {
 <?php } else { ?>
 	background: rgb(<?php print $colorbackbody; ?>);
 <?php } ?>
-	color: #101010;
+	color: rgb(<?php echo $colortext; ?>);
 	font-size: <?php print $fontsize ?>px;
 	font-family: <?php print $fontlist ?>;
     margin-top: 0;
-- 
GitLab