diff --git a/htdocs/admin/emailcollector_card.php b/htdocs/admin/emailcollector_card.php
index 08656a9ee27..3cc79623342 100644
--- a/htdocs/admin/emailcollector_card.php
+++ b/htdocs/admin/emailcollector_card.php
@@ -466,7 +466,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print '
';
$arrayoftypes=array('recordevent'=>'RecordEvent');
if ($conf->projet->enabled) $arrayoftypes['project']='CreateLeadAndThirdParty';
- print $form->selectarray('operationtype', $arrayoftypes, '', 0, 0, 0, '', 1);
+ print $form->selectarray('operationtype', $arrayoftypes, '', 1, 0, 0, '', 1);
print ' | ';
print '';
print ' | ';
diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php
index 0e9921e8700..3d247bfd5ca 100644
--- a/htdocs/contact/class/contact.class.php
+++ b/htdocs/contact/class/contact.class.php
@@ -87,17 +87,17 @@ class Contact extends CommonObject
/**
* @deprecated
- * @see state_id
+ * @see $state_id
*/
public $fk_departement;
/**
* @deprecated
- * @see state_code
+ * @see $state_code
*/
public $departement_code;
/**
* @deprecated
- * @see state
+ * @see $state
*/
public $departement;
public $state_id; // Id of department
@@ -688,7 +688,7 @@ class Contact extends CommonObject
* @param string $email Email
* @return int -1 if KO, 0 if OK but not found, 1 if OK
*/
- function fetch($id, $user=0, $ref_ext='', $email='')
+ function fetch($id, $user=null, $ref_ext='', $email='')
{
global $langs;
diff --git a/htdocs/core/actions_sendmails.inc.php b/htdocs/core/actions_sendmails.inc.php
index 61c4b636758..9ffaceadb26 100644
--- a/htdocs/core/actions_sendmails.inc.php
+++ b/htdocs/core/actions_sendmails.inc.php
@@ -427,8 +427,9 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
$object->socid = $sendtosocid; // To link to a company
$object->sendtoid = $sendtoid; // To link to contact addresses. This is an array.
$object->actiontypecode = $actiontypecode; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
- $object->actionmsg = $actionmsg; // Long text
- $object->actionmsg2 = $actionmsg2; // Short text
+ $object->actionmsg = $actionmsg; // Long text (@TODO Replace this with $message, we already have details of email in dedicated properties)
+ $object->actionmsg2 = $actionmsg2; // Short text ($langs->transnoentities('MailSentBy')...);
+
$object->trackid = $trackid;
$object->fk_element = $object->id;
$object->elementtype = $object->element;
@@ -439,9 +440,14 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
$object->sendtouserid = $sendtouserid;
}
- // TODO Set object->email_xxx properties
+ $object->email_msgid = $mailfile->msgid; // @TODO Set msgid into $mailfile after sending
+ $object->email_from = $from;
+ $object->email_subject = $subject;
+ $object->email_to = $sendto;
+ $object->email_tocc = $sendtocc;
+ $object->email_tobcc = $sendtobcc;
+ $object->email_subject = $subject;
$object->email_msgid = $mailfile->msgid;
- //...
// Call of triggers
if (! empty($trigger_name))
diff --git a/htdocs/emailcollector/class/emailcollector.class.php b/htdocs/emailcollector/class/emailcollector.class.php
index 8769e0a70c9..ccf303949ec 100644
--- a/htdocs/emailcollector/class/emailcollector.class.php
+++ b/htdocs/emailcollector/class/emailcollector.class.php
@@ -23,8 +23,11 @@
// Put here all includes required by your class file
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
-//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
-//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
+require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
+require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
+require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
+require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
+
/**
* Class for EmailCollector
@@ -647,6 +650,25 @@ class EmailCollector extends CommonObject
}
+ /**
+ * Return the connectstring to use with IMAP connection function
+ *
+ * @return string
+ */
+ function getConnectStringIMAP()
+ {
+ // Connect to IMAP
+ $flags ='/service=imap'; // IMAP
+ $flags.='/ssl'; // '/tls'
+ $flags.='/novalidate-cert';
+ //$flags.='/readonly';
+ //$flags.='/debug';
+
+ $connectstringserver = '{'.$this->host.':993'.$flags.'}';
+
+ return $connectstringserver;
+ }
+
/**
* Action executed by scheduler
* CAN BE A CRON TASK. In such a case, paramerts come from the schedule job setup field 'Parameters'
@@ -757,13 +779,16 @@ class EmailCollector extends CommonObject
if ($fromdate > 0) $search.=($search?' ':'').'SINCE '.dol_print_date($fromdate - 1,'dayhourrfc');
}
dol_syslog("search string = ".$search);
- //var_dump($search);exit;
+ //var_dump($search);
$nbemailprocessed=0; $nbactiondone=0;
+ $projectstatic=new Project($this->db);
+ $thirdpartystatic=new Societe($this->db);
+ $contactstatic=new Contact($this->db);
// Scan IMAP inbox
$arrayofemail= imap_search($connection, $search);
- //var_dump($arrayofemail);
+ //var_dump($arrayofemail);exit;
// Loop on each email found
if (! empty($arrayofemail) && count($arrayofemail) > 0)
@@ -778,14 +803,126 @@ class EmailCollector extends CommonObject
$overview = imap_fetch_overview($connection, $imapemail, 0);
$header = imap_fetchheader($connection, $imapemail, 0);
- $message = imap_body($connection, $imapemail, 0);
- // imap_fetchstructure($connection, $imapemail, 0);
- // imap_fetchbody($connection, $imapemail, 1) may be text/plain, 2 may be text/html
+ //$message = imap_body($connection, $imapemail, 0);
+ $structure = imap_fetchstructure($connection, $imapemail, 0);
+ $partplain = $parthtml = -1;
+ // Loop to get part html and plain
+ foreach($structure->parts as $key => $part)
+ {
+ if ($part->subtype == 'HTML') $parthtml=$key;
+ if ($part->subtype == 'PLAIN') $partplain=$key;
+ }
- /*var_dump($overview);
- var_dump($header);
- var_dump($message);
- */
+ $matches=array();
+ preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $header, $matches);
+ $headers = array_combine($matches[1], $matches[2]);
+ //var_dump($headers);
+
+ $messagetext = imap_fetchbody($connection, $imapemail, ($parthtml >= 0 ? $parthtml : ($partplain >= 0 ? $partplain : 0)));
+
+ //var_dump($overview);
+ //var_dump($header);
+ //var_dump($message);
+ //var_dump($messagetext);
+ $fromstring=$overview[0]->from;
+ $sender=$overview[0]->sender;
+ $to=$overview[0]->to;
+ $sendtocc=$overview[0]->cc;
+ $sendtobcc=$overview[0]->bcc;
+ $date=$overview[0]->udate;
+ $msgid=$overview[0]->message_id;
+ $subject=$overview[0]->subject;
+
+ $reg=array();
+ if (preg_match('/^(.*)<(.*)>$/', $fromstring, $reg))
+ {
+ $from=$reg[1];
+ $fromtext=$reg[0];
+ }
+ else
+ {
+ $from = $fromstring;
+ $fromtext='';
+ }
+ $fk_element_id = 0; $fk_element_type = '';
+ $contactid = 0; $thirdpartyid = 0; $projectid = 0;
+
+ // Analyze TrackId
+ $reg=array();
+ if (! empty($headers['X-Dolibarr-TrackId']) && preg_match('/:\s*([a-z]+)([0-9]+)$/', $headers['X-Dolibarr-TrackId'], $reg))
+ {
+ $objectid = 0;
+ $objectemail = null;
+ if ($reg[0] == 'inv')
+ {
+ $objectid = $reg[1];
+ $objectemail = new Facture($this->db);
+ }
+ if ($reg[0] == 'proj')
+ {
+ $objectid = $reg[1];
+ $objectemail = new Project($this->db);
+ }
+ if ($reg[0] == 'con')
+ {
+ $objectid = $reg[1];
+ $objectemail = new Contact($this->db);
+ }
+ if ($reg[0] == 'thi')
+ {
+ $objectid = $reg[1];
+ $objectemail = new Societe($this->db);
+ }
+ if ($reg[0] == 'use')
+ {
+ $objectid = $reg[1];
+ $objectemail = new User($this->db);
+ }
+
+ $result = $objectemail->fetch($objectid);
+ if ($result > 0)
+ {
+ $fk_element_id = $objectemail->id;
+ $fk_element_type = $objectemail->element;
+ // Fix fk_element_type
+ if ($fk_element_type == 'facture') $fk_element_type = 'invoice';
+
+ $thirdpartyid = $objectemail->fk_soc;
+ $projectid = isset($objectemail->fk_project)?$objectemail->fk_project:$objectemail->fk_projet;
+ }
+ }
+
+ // Project
+ $projectstatic->id=0;
+ if ($projectid > 0)
+ {
+ $result = $projectstatic->fetch($projectid);
+ if ($result <= 0) $projectstatic->id = 0;
+ }
+ // Contact
+ $contactstatic->id=0;
+ if ($contactid > 0)
+ {
+ $result = $contactstatic->fetch($contactid);
+ if ($result <= 0) $contactstatic->id = 0;
+ }
+ else // Try to find contact using email
+ {
+ $contactstatic->fetch(0, null, '', $from);
+ }
+ // Thirdparty
+ $thirdpartystatic->id=0;
+ if ($thirdpartyid > 0)
+ {
+ $result = $thirdpartystatic->fetch($thirdpartyid);
+ if ($result <= 0) $thirdpartystatic->id = 0;
+ }
+ else // Try to find thirdparty using email
+ {
+ $thirdpartystatic->fetch(0, '', '', '', '', '', '', '', '', '', $from);
+ }
+
+ require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
// Do operationss
foreach($this->actions as $operation)
@@ -793,8 +930,60 @@ class EmailCollector extends CommonObject
if ($errorforactions) break;
if (empty($operation['status'])) continue;
- // Make Operation
+ // Make Operation
+ if ($operation['type'] == 'recordevent')
+ {
+ $actioncode = 'EMAIL_IN';
+ var_dump($structure);
+ // Insert record of emails sent
+ $actioncomm = new ActionComm($this->db);
+
+ $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
+ $actioncomm->code = 'AC_'.$actioncode;
+ $actioncomm->label = $subject;
+ $actioncomm->note = $messagetext;
+ $actioncomm->fk_project = 0;
+ $actioncomm->datep = $date;
+ $actioncomm->datef = $date;
+ $actioncomm->percentage = -1; // Not applicable
+ $actioncomm->socid = $thirdpartystatic->id;
+ $actioncomm->contactid = $contactstatic->id;
+ $actioncomm->authorid = $user->id; // User saving action
+ $actioncomm->userownerid = $user->id; // Owner of action
+ // Fields when action is en email (content should be added into note)
+ $actioncomm->email_msgid = $msgid;
+ $actioncomm->email_from = $fromstring;
+ $actioncomm->email_sender= $sender;
+ $actioncomm->email_to = $to;
+ $actioncomm->email_tocc = $sendtocc;
+ $actioncomm->email_tobcc = $sendtobcc;
+ $actioncomm->email_subject = $subject;
+ $actioncomm->errors_to = '';
+
+ $object->email_msgid = $mailfile->msgid; // @TODO Set msgid into $mailfile after sending to have it defined here
+ $object->email_from = $from;
+ $object->email_subject = $subject;
+ $object->email_to = $to;
+ $object->email_tocc = $sendtocc;
+ $object->email_tobcc = $sendtobcc;
+ $object->email_subject = $subject;
+
+
+ $actioncomm->fk_element = $fk_element_id;
+ $actioncomm->elementtype = $fk_element_type;
+
+ //$actioncomm->extraparams = $extraparams;
+
+ $result = $actioncomm->create($user);
+ if ($result <= 0)
+ {
+ $errorforactions++;
+ $this->errors = $actioncomm->errors;
+ }
+
+ }
+ var_dump($actioncomm);exit;
if (! $errorforactions)
@@ -817,6 +1006,8 @@ class EmailCollector extends CommonObject
$nbemailprocessed++;
+ unset($objectemail);
+
$this->db->commit();
}
@@ -840,23 +1031,4 @@ class EmailCollector extends CommonObject
return $error?-1:1;
}
-
- /**
- * Return the connectstring to use with IMAP connection function
- *
- * @return string
- */
- function getConnectStringIMAP()
- {
- // Connect to IMAP
- $flags ='/service=imap'; // IMAP
- $flags.='/ssl'; // '/tls'
- $flags.='/novalidate-cert';
- //$flags.='/readonly';
- //$flags.='/debug';
-
- $connectstringserver = '{'.$this->host.':993'.$flags.'}';
-
- return $connectstringserver;
- }
}