From fddbca50b12f6288b80b9cadbf420e589b54d557 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 3 Mar 2015 18:33:53 +0100
Subject: [PATCH] Start to introduce MAIN_EMAIL_ADD_TRACK_ID option to test
 inclusion of tracking id into email.

---
 htdocs/core/class/CMailFile.class.php | 12 ++++++------
 htdocs/core/lib/functions2.lib.php    | 15 +++++++++++++++
 htdocs/societe/soc.php                |  9 +++++++--
 3 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php
index 5288dc635cb..573e768fb4b 100644
--- a/htdocs/core/class/CMailFile.class.php
+++ b/htdocs/core/class/CMailFile.class.php
@@ -36,12 +36,12 @@
  */
 class CMailFile
 {
-	var $subject;      // Topic:       Subject of email
-	var $addr_from;    // From:        Label of sender (name but can contains an email inside <>)
-	                   // Sender:      Who send the email ("Sender" has sent emails on behalf of "From").
-	                   //              Use it with an email from a sending host from is a SPF protected domain and sending host is not this domain.
-	                   // Return-Path: Email where to send bounds.
-	var $errors_to;    // Errors-To:   Email where to send errors.
+	var $subject;      	// Topic:       Subject of email
+	var $addr_from;    	// From:		Label and EMail of sender (must include '<>'). For example '<myemail@mydomain.com>' or 'John Doe <myemail@mydomain.com>' or '<myemail+trackingid@mydomain.com>')
+	                   	// Sender:      Who send the email ("Sender" has sent emails on behalf of "From").
+	                   	//              Use it when the "From" is an email of a domain that is a SPF protected domain, and sending smtp server is not this domain. In such case, use for Sender an email of the protected domain.
+	                   	// Return-Path: Email where to send bounds.
+	var $errors_to;		// Errors-To:	Email where to send errors.
 	var $addr_to;
 	var $addr_cc;
 	var $addr_bcc;
diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php
index 9a26c727bd4..5faf1c1c470 100644
--- a/htdocs/core/lib/functions2.lib.php
+++ b/htdocs/core/lib/functions2.lib.php
@@ -371,6 +371,21 @@ function dol_print_object_info($object)
     }
 }
 
+
+/**
+ *	Return an email formatted to include a tracking id
+ *  For example  myemail@mydomain.com becom myemail+trackingid@mydomain.com
+ *
+ *	@param	string	$email       	Email address (Ex: "toto@titi.com", "John Do <johndo@titi.com>")
+ *	@param	string	$trackingid    	Tracking id (Ex: thi123 for thirdparty with id 123)
+ *	@return boolean     			True if domain email is OK, False if KO
+ */
+function dolAddEmailTrackId($email, $trackingid)
+{
+	$tmp=explode('@',$email);
+	return $tmp[0].'+'.$trackingid.'@'.(isset($tmp[1])?$tmp[1]:'');
+}
+
 /**
  *	Return true if email has a domain name that can't be resolved
  *
diff --git a/htdocs/societe/soc.php b/htdocs/societe/soc.php
index 04f749dc32a..72dfc8931b8 100644
--- a/htdocs/societe/soc.php
+++ b/htdocs/societe/soc.php
@@ -259,9 +259,9 @@ if (empty($reshook))
 	                		$action = (($action=='add'||$action=='create')?'create':'edit');
 						}
 					}
-	
+
 					$idprof_mandatory ='SOCIETE_IDPROF'.($i).'_MANDATORY';
-	
+
 					if (! $vallabel && ! empty($conf->global->$idprof_mandatory))
 					{
 						$langs->load("errors");
@@ -2129,6 +2129,11 @@ else
 			$formmail->fromid   = $user->id;
 			$formmail->fromname = $user->getFullName($langs);
 			$formmail->frommail = $user->email;
+			if (! empty($conf->global->MAIN_EMAIL_ADD_TRACK_ID))
+			{
+				include DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
+				$formmail->frommail=dolAddEmailTrackId($formmail->frommail, 'thi'.$object->id);
+			}
 			$formmail->withfrom=1;
 			$formmail->withtopic=1;
 			$liste=array();
-- 
GitLab