From b284bdd953d26abf3f351e2fcdacd16bea7dfd43 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Mon, 24 Oct 2016 02:02:30 +0200
Subject: [PATCH] NEW #5896 More complete data on event sent by email (name in
 title, emails list in details)

---
 htdocs/core/actions_sendmails.inc.php  | 14 +++++-----
 htdocs/core/class/CMailFile.class.php  | 36 +++++++++++++++++++-------
 htdocs/societe/class/societe.class.php |  2 +-
 test/phpunit/CMailFileTest.php         | 17 +++++++++++-
 4 files changed, 51 insertions(+), 18 deletions(-)

diff --git a/htdocs/core/actions_sendmails.inc.php b/htdocs/core/actions_sendmails.inc.php
index 297fe373c04..9ec3bfcb3b7 100644
--- a/htdocs/core/actions_sendmails.inc.php
+++ b/htdocs/core/actions_sendmails.inc.php
@@ -171,7 +171,7 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
 			// Recipient was provided from combo list
 			if ($_POST['receiver'] == 'thirdparty') // Id of third party
 			{
-				$sendto = $thirdparty->email;
+				$sendto = $thirdparty->name.' <'.$thirdparty->email.'>';
 				$sendtoid = 0;
 			}
 			else	// Id du contact
@@ -189,7 +189,7 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
 			// Recipient was provided from combo list
 			if ($_POST['receivercc'] == 'thirdparty')	// Id of third party
 			{
-				$sendtocc = $thirdparty->email;
+				$sendtocc = $thirdparty->name.' <'.$thirdparty->email.'>';
 			}
 			else	// Id du contact
 			{
@@ -199,6 +199,8 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
 
 		if (dol_strlen($sendto))
 		{
+			require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
+		    
 			$langs->load("commercial");
 
 			$fromtype = GETPOST('fromtype');
@@ -235,11 +237,12 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
 			if ($action == 'send' || $action == 'relance')
 			{
 				if (dol_strlen($_POST['subject'])) $subject = $_POST['subject'];
-				$actionmsg2=$langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
+				$actionmsg2=$langs->transnoentities('MailSentBy').' '.CMailFile::getValidAddress($from,4,0,1).' '.$langs->transnoentities('To').' '.CMailFile::getValidAddress($sendto,4,0,1);
 				if ($message)
 				{
-					$actionmsg=$langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
-					if ($sendtocc) $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc') . ": " . $sendtocc);
+					$actionmsg=$langs->transnoentities('MailFrom').': '.dol_escape_htmltag($from);
+					$actionmsg=dol_concatdesc($actionmsg, $langs->transnoentities('MailTo').': '.dol_escape_htmltag($sendto));
+					if ($sendtocc) $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc') . ": " . dol_escape_htmltag($sendtocc));
 					$actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic') . ": " . $subject);
 					$actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody') . ":");
 					$actionmsg = dol_concatdesc($actionmsg, $message);
@@ -300,7 +303,6 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
 			}
 
 			// Send mail
-			require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
 			$mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,$sendtobcc,$deliveryreceipt,-1,'','',$trackid);
 			if ($mailfile->error)
 			{
diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php
index 03f7e39bde8..317b84e0b29 100644
--- a/htdocs/core/class/CMailFile.class.php
+++ b/htdocs/core/class/CMailFile.class.php
@@ -690,7 +690,7 @@ class CMailFile
 	 * @param string $stringtoencode String to encode
 	 * @return string                string encoded
 	 */
-	function encodetorfc2822($stringtoencode)
+	static function encodetorfc2822($stringtoencode)
 	{
 		global $conf;
 		return '=?'.$conf->file->character_set_client.'?B?'.base64_encode($stringtoencode).'?=';
@@ -1220,15 +1220,17 @@ class CMailFile
 	/**
 	 * Return a formatted address string for SMTP protocol
 	 *
-	 * @param	string		$address		Example: 'John Doe <john@doe.com>, Alan Smith <alan@smith.com>' or 'john@doe.com, alan@smith.com'
-	 * @param	int			$format			0=auto, 1=emails with <>, 2=emails without <>, 3=auto + label between "
-	 * @param	int			$encode			1=Encode name to RFC2822
-	 * @return	string						If format 0: '<john@doe.com>' or 'John Doe <john@doe.com>' or '=?UTF-8?B?Sm9obiBEb2U=?= <john@doe.com>'
-	 * 										If format 1: '<john@doe.com>'
-	 *										If format 2: 'john@doe.com'
-	 *										If format 3: '<john@doe.com>' or '"John Doe" <john@doe.com>' or '"=?UTF-8?B?Sm9obiBEb2U=?=" <john@doe.com>'
+	 * @param	string		$address		     Example: 'John Doe <john@doe.com>, Alan Smith <alan@smith.com>' or 'john@doe.com, alan@smith.com'
+	 * @param	int			$format			     0=auto, 1=emails with <>, 2=emails without <>, 3=auto + label between "
+	 * @param	int			$encode			     0=No encode name, 1=Encode name to RFC2822
+	 * @param   int         $maxnumberofemail    0=No limit. Otherwise, maximum number of emails returned ($address may contains several email separated with ','). Add '...' if there is more. 
+	 * @return	string						     If format 0: '<john@doe.com>' or 'John Doe <john@doe.com>' or '=?UTF-8?B?Sm9obiBEb2U=?= <john@doe.com>'
+	 * 										     If format 1: '<john@doe.com>'
+	 *										     If format 2: 'john@doe.com'
+	 *										     If format 3: '<john@doe.com>' or '"John Doe" <john@doe.com>' or '"=?UTF-8?B?Sm9obiBEb2U=?=" <john@doe.com>'
+	 *                                           If format 4: 'John Doe' or 'john@doe.com' if no label exists
 	 */
-	function getValidAddress($address,$format,$encode='')
+	static function getValidAddress($address,$format,$encode=0,$maxnumberofemail=0)
 	{
 		global $conf;
 
@@ -1237,6 +1239,7 @@ class CMailFile
 		$arrayaddress=explode(',',$address);
 
 		// Boucle sur chaque composant de l'adresse
+		$i=0;
 		foreach($arrayaddress as $val)
 		{
 			if (preg_match('/^(.*)<(.*)>$/i',trim($val),$regs))
@@ -1252,7 +1255,13 @@ class CMailFile
 
 			if ($email)
 			{
+			    $i++;
+			    
 				$newemail='';
+				if ($format == 4)
+				{
+				    $newemail = $name?$name:$email;
+				}
 				if ($format == 2)
 				{
 					$newemail=$email;
@@ -1265,10 +1274,17 @@ class CMailFile
 				{
 					if (! empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL)) $newemail='<'.$email.'>';
 					elseif (! $name) $newemail='<'.$email.'>';
-					else $newemail=($format==3?'"':'').($encode?$this->encodetorfc2822($name):$name).($format==3?'"':'').' <'.$email.'>';
+					else $newemail=($format==3?'"':'').($encode?self::encodetorfc2822($name):$name).($format==3?'"':'').' <'.$email.'>';
 				}
 
 				$ret=($ret ? $ret.',' : '').$newemail;
+				
+				// Stop if we have too much records
+				if ($maxnumberofemail && $i >= $maxnumberofemail)
+				{
+				    if (count($arrayaddress) > $maxnumberofemail) $ret.='...';
+				    break;
+				}
 			}
 		}
 
diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php
index 1d8ac3ff98a..c2c75c44e66 100644
--- a/htdocs/societe/class/societe.class.php
+++ b/htdocs/societe/class/societe.class.php
@@ -2185,7 +2185,7 @@ class Societe extends CommonObject
      *
      *  @param	int		$rowid      id of contact
      *  @param  string	$mode       'email' or 'mobile'
-     *  @return string  			email of contact
+     *  @return string  			Email of contact with format: "Full name <email>"
      */
     function contact_get_property($rowid,$mode)
     {
diff --git a/test/phpunit/CMailFileTest.php b/test/phpunit/CMailFileTest.php
index 092d2e1f41c..06f24ba45e0 100755
--- a/test/phpunit/CMailFileTest.php
+++ b/test/phpunit/CMailFileTest.php
@@ -177,7 +177,22 @@ class CMailFileTest extends PHPUnit_Framework_TestCase
         $result=$localobject->getValidAddress($src,3,1);
         print __METHOD__." result=".$result."\n";
         $this->assertEquals($result,'"=?UTF-8?B?Sm9obiBEb2U=?=" <john@doe.com>');
-
+        
+        $src='John Doe <john@doe.com>';
+        $result=$localobject->getValidAddress($src,4);
+        print __METHOD__." result=".$result."\n";
+        $this->assertEquals($result,'John Doe');
+        
+        $src='John Doe <john@doe.com>, John Doe2 <john@doe3.com>, John Doe3 <john@doe2.com>';
+        $result=$localobject->getValidAddress($src,4);
+        print __METHOD__." result=".$result."\n";
+        $this->assertEquals($result,'John Doe,John Doe2,John Doe3');
+        
+        $src='John Doe <john@doe.com>, John Doe2 <john@doe3.com>, John Doe3 <john@doe2.com>';
+        $result=$localobject->getValidAddress($src,4,0,2);
+        print __METHOD__." result=".$result."\n";
+        $this->assertEquals($result,'John Doe,John Doe2...');
+        
         return $result;
     }
 
-- 
GitLab