diff --git a/htdocs/admin/mails.php b/htdocs/admin/mails.php
index b507ac89a50dd1f6a97311089f01366a8ea8b183..a9a588ae4d70e1f308b6ba2345bdf32a62eb4041 100644
--- a/htdocs/admin/mails.php
+++ b/htdocs/admin/mails.php
@@ -83,147 +83,17 @@ if ($action == 'update' && empty($_POST["cancel"]))
 }
 
 
-/*
- * Add file in email form
- */
-if (GETPOST('addfile') || GETPOST('addfilehtml'))
-{
-	require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
-
-	// Set tmp user directory
-	$vardir=$conf->user->dir_output."/".$user->id;
-	$upload_dir = $vardir.'/temp';
-	dol_add_file_process($upload_dir,0,0);
-
-	if ($_POST['addfile'])     $action='test';
-	if ($_POST['addfilehtml']) $action='testhtml';
-}
-
-/*
- * Remove file in email form
- */
-if (! empty($_POST['removedfile']) || ! empty($_POST['removedfilehtml']))
-{
-	// Set tmp user directory
-	$vardir=$conf->user->dir_output."/".$user->id;
-	$upload_dir = $vardir.'/temp';
-
-	$keytodelete=isset($_POST['removedfile'])?$_POST['removedfile']:$_POST['removedfilehtml'];
-	$keytodelete--;
-
-	$listofpaths=array();
-	$listofnames=array();
-	$listofmimes=array();
-	if (! empty($_SESSION["listofpaths"])) $listofpaths=explode(';',$_SESSION["listofpaths"]);
-	if (! empty($_SESSION["listofnames"])) $listofnames=explode(';',$_SESSION["listofnames"]);
-	if (! empty($_SESSION["listofmimes"])) $listofmimes=explode(';',$_SESSION["listofmimes"]);
-
-	if ($keytodelete >= 0)
-	{
-		$pathtodelete=$listofpaths[$keytodelete];
-		$filetodelete=$listofnames[$keytodelete];
-		$result = dol_delete_file($pathtodelete,1);
-		if ($result)
-		{
-			setEventMessages(array($langs->trans("FileWasRemoved"), $filetodelete), null, 'mesgs');
-
-			include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
-			$formmail = new FormMail($db);
-			$formmail->remove_attached_files($keytodelete);
-		}
-	}
-	if ($_POST['removedfile'] || $action='send')     $action='test';
-	if ($_POST['removedfilehtml'] || $action='sendhtml') $action='testhtml';
-}
+// Actions to send emails
+$id=0;
+$actiontypecode='';
+$trigger_name='';
+$paramname='id';
+$mode='emailfortest';
+include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
 
-/*
- * Send mail
- */
-if (($action == 'send' || $action == 'sendhtml') && ! GETPOST('addfile') && ! GETPOST('addfilehtml') && ! GETPOST('removedfile') && ! GETPOST('cancel'))
-{
-	$error=0;
-
-	$email_from='';
-	if (! empty($_POST["fromname"])) $email_from=$_POST["fromname"].' ';
-	if (! empty($_POST["frommail"])) $email_from.='<'.$_POST["frommail"].'>';
-
-	$errors_to  = $_POST["errorstomail"];
-	$sendto     = $_POST["sendto"];
-	$sendtocc   = $_POST["sendtocc"];
-	$sendtoccc  = $_POST["sendtoccc"];
-	$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);
-	}
-
-	// Create form object
-	include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
-	$formmail = new FormMail($db);
+if ($action == 'presend' && GETPOST('trackid') == 'test')       $action='test';
+if ($action == 'presend' && GETPOST('trackid') == 'testhtml')   $action='testhtml';
 
-	$attachedfiles=$formmail->get_attached_files();
-	$filepath = $attachedfiles['paths'];
-	$filename = $attachedfiles['names'];
-	$mimetype = $attachedfiles['mimes'];
-
-	if (empty($_POST["frommail"]))
-	{
-		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("MailFrom")), null, 'errors');
-		$action='test';
-		$error++;
-	}
-	if (empty($sendto))
-	{
-		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("MailTo")), null, 'errors');
-		$action='test';
-		$error++;
-	}
-	if (! $error)
-	{
-		// Is the message in HTML?
-		$msgishtml=0;	// Message is not HTML
-		if ($action == 'sendhtml') $msgishtml=1;	// Force message to HTML
-
-		// Pratique les substitutions sur le sujet et message
-		$subject=make_substitutions($subject,$substitutionarrayfortest);
-		$body=make_substitutions($body,$substitutionarrayfortest);
-
-		require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
-        $mailfile = new CMailFile(
-            $subject,
-            $sendto,
-            $email_from,
-            $body,
-            $filepath,
-            $mimetype,
-            $filename,
-            $sendtocc,
-            $sendtoccc,
-            $deliveryreceipt,
-            $msgishtml,
-            $errors_to,
-        	'',
-        	$trackid
-        );
-
-		$result=$mailfile->sendfile();
-
-		if ($result)
-		{
-			setEventMessages($langs->trans("MailSuccessfulySent",$mailfile->getValidAddress($email_from,2),$mailfile->getValidAddress($sendto,2)), null, 'mesgs');
-		}
-		else
-		{
-			setEventMessages($langs->trans("ResultKo").'<br>'.$mailfile->error.' '.$result, null, 'errors');
-		}
-
-		$action='';
-	}
-}
 
 
 
@@ -747,7 +617,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->trackid=(($action == 'testhtml')?"testhtml":"test");
 		$formmail->withfromreadonly=0;
 		$formmail->withsubstit=0;
 		$formmail->withfrom=1;
@@ -767,7 +637,7 @@ else
 		// Tableau des substitutions
 		$formmail->substit=$substitutionarrayfortest;
 		// Tableau des parametres complementaires du post
-		$formmail->param["action"]=($action == 'testhtml'?"sendhtml":"send");
+		$formmail->param["action"]="send";
 		$formmail->param["models"]="body";
 		$formmail->param["mailid"]=0;
 		$formmail->param["returnurl"]=$_SERVER["PHP_SELF"];
@@ -778,7 +648,7 @@ else
 			$formmail->clear_attached_files();
 		}
 
-		print $formmail->get_form(($action == 'testhtml'?'addfilehtml':'addfile'),($action == 'testhtml'?'removefilehtml':'removefile'));
+		print $formmail->get_form('addfile','removefile');
 
 		print '<br>';
 	}
diff --git a/htdocs/core/actions_sendmails.inc.php b/htdocs/core/actions_sendmails.inc.php
index a5e6a29b50a98aff9381a3277dc6eca66adcecc7..5cbe973230a3d1fefb14eb2f2c7f84010d618071 100644
--- a/htdocs/core/actions_sendmails.inc.php
+++ b/htdocs/core/actions_sendmails.inc.php
@@ -21,11 +21,13 @@
  *  \brief			Code for actions on sending mails from object page
  */
 
-
+// $mysoc must be defined
 // $id must be defined
 // $actiontypecode must be defined
 // $paramname must be defined
 // $mode must be defined
+// $object and $uobject may be defined.
+
 
 /*
  * Add file in email form
@@ -107,51 +109,55 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
 
 	$subject='';$actionmsg='';$actionmsg2='';
 
-	$result=$object->fetch($id);
-
-	$sendtosocid=0;
-	if (method_exists($object,"fetch_thirdparty") && $object->element != 'societe')
-	{
-		$result=$object->fetch_thirdparty();
-		$thirdparty=$object->thirdparty;
-		$sendtosocid=$thirdparty->id;
-	}
-	else if ($object->element == 'societe')
+	if (is_object($object))
 	{
-		$thirdparty=$object;
-		if ($thirdparty->id > 0) $sendtosocid=$thirdparty->id;
-		elseif($conf->dolimail->enabled)
-		{
-			$dolimail = new Dolimail($db);
-			$possibleaccounts=$dolimail->get_societe_by_email($_POST['sendto'],"1");
-			$possibleuser=$dolimail->get_from_user_by_mail($_POST['sendto'],"1"); // suche in llx_societe and socpeople
-			if (!$possibleaccounts && !$possibleuser) 
-			{
-					setEventMessages($langs->trans('ErrorFailedToFindSocieteRecord',$_POST['sendto']), null, 'errors');
-			}
-			elseif (count($possibleaccounts)>1) 
-			{
-					$sendtosocid=$possibleaccounts[1]['id'];
-					$result=$object->fetch($sendtosocid);
-					
-					setEventMessages($langs->trans('ErrorFoundMoreThanOneRecordWithEmail',$_POST['sendto'],$object->name), null, 'mesgs');
-			}
-			else 
-			{
-				if($possibleaccounts){ 
-					$sendtosocid=$possibleaccounts[1]['id'];
-					$result=$object->fetch($sendtosocid);
-				}elseif($possibleuser){ 
-					$sendtosocid=$possibleuser[0]['id'];
-
-					$result=$uobject->fetch($sendtosocid);
-					$object=$uobject;
-				}
-				
-			}
-		}
+    	$result=$object->fetch($id);
+    
+    	$sendtosocid=0;
+    	if (method_exists($object,"fetch_thirdparty") && $object->element != 'societe')
+    	{
+    		$result=$object->fetch_thirdparty();
+    		$thirdparty=$object->thirdparty;
+    		$sendtosocid=$thirdparty->id;
+    	}
+    	else if ($object->element == 'societe')
+    	{
+    		$thirdparty=$object;
+    		if ($thirdparty->id > 0) $sendtosocid=$thirdparty->id;
+    		elseif($conf->dolimail->enabled)
+    		{
+    			$dolimail = new Dolimail($db);
+    			$possibleaccounts=$dolimail->get_societe_by_email($_POST['sendto'],"1");
+    			$possibleuser=$dolimail->get_from_user_by_mail($_POST['sendto'],"1"); // suche in llx_societe and socpeople
+    			if (!$possibleaccounts && !$possibleuser) 
+    			{
+    					setEventMessages($langs->trans('ErrorFailedToFindSocieteRecord',$_POST['sendto']), null, 'errors');
+    			}
+    			elseif (count($possibleaccounts)>1) 
+    			{
+    					$sendtosocid=$possibleaccounts[1]['id'];
+    					$result=$object->fetch($sendtosocid);
+    					
+    					setEventMessages($langs->trans('ErrorFoundMoreThanOneRecordWithEmail',$_POST['sendto'],$object->name), null, 'mesgs');
+    			}
+    			else 
+    			{
+    				if($possibleaccounts){ 
+    					$sendtosocid=$possibleaccounts[1]['id'];
+    					$result=$object->fetch($sendtosocid);
+    				}elseif($possibleuser){ 
+    					$sendtosocid=$possibleuser[0]['id'];
+    
+    					$result=$uobject->fetch($sendtosocid);
+    					$object=$uobject;
+    				}
+    				
+    			}
+    		}
+    	}
+    	else dol_print_error('','Use actions_sendmails.in.php for a type that is not supported');
 	}
-	else dol_print_error('','Use actions_sendmails.in.php for a type that is not supported');
+	else $thirdparty = $mysoc;
 
 	if ($result > 0)
 	{
diff --git a/htdocs/core/class/html.formmail.class.php b/htdocs/core/class/html.formmail.class.php
index 6832bc33f432f924f4e89a8ebb2684ba8d00dce5..72ad0102d7938a22399c4c4735489f13820b1a1d 100644
--- a/htdocs/core/class/html.formmail.class.php
+++ b/htdocs/core/class/html.formmail.class.php
@@ -269,6 +269,7 @@ class FormMail extends Form
         	$listofnames=array();
         	$listofmimes=array();
             $keytoavoidconflict = empty($this->trackid)?'':'-'.$this->trackid;   // this->trackid must be defined
+            
         	if (! empty($_SESSION["listofpaths".$keytoavoidconflict])) $listofpaths=explode(';',$_SESSION["listofpaths".$keytoavoidconflict]);
         	if (! empty($_SESSION["listofnames".$keytoavoidconflict])) $listofnames=explode(';',$_SESSION["listofnames".$keytoavoidconflict]);
         	if (! empty($_SESSION["listofmimes".$keytoavoidconflict])) $listofmimes=explode(';',$_SESSION["listofmimes".$keytoavoidconflict]);