' // TODO Use a better way to define Sent dir.
if (count($mailfromid)==0) $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>';
else
{
$mbid = $mailfromid[1];
// IMAP Postbox
$mailboxconfig = new IMAP($db);
$mailboxconfig->fetch($mbid);
if ($mailboxconfig->mailbox_imap_host) $ref=$mailboxconfig->get_ref();
$mailboxconfig->folder_id=$mailboxconfig->mailbox_imap_outbox;
$mailboxconfig->userfolder_fetch();
if ($mailboxconfig->mailbox_save_sent_mails == 1)
{
$folder=str_replace($ref, '', $mailboxconfig->folder_cache_key);
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)
{
$info = false;
$err = $langs->trans('Error3_Imap_Connection_Error');
setEventMessages($err,$mailboxconfig->element, null, 'errors');
}
else
{
$mailboxconfig->mailboxid=$_POST['frommail'];
$mailboxconfig->foldername=$folder;
$from = $mailfromid[0] . $mailfromid[2];
$imap=1;
}
}
}
}
*/
// Make substitution in email content
$substitutionarray=getCommonSubstitutionArray($langs, 0, null, $object);
$substitutionarray['__EMAIL__'] = $sendto;
$substitutionarray['__CHECK_READ__'] = (is_object($object) && is_object($object->thirdparty))?'
':'';
$parameters=array('mode'=>'formemail');
complete_substitutions_array($substitutionarray, $langs, $object, $parameters);
$subject=make_substitutions($subject, $substitutionarray);
$message=make_substitutions($message, $substitutionarray);
if (method_exists($object, 'makeSubstitution'))
{
$subject = $object->makeSubstitution($subject);
$message = $object->makeSubstitution($message);
}
// Send mail (substitutionarray must be done just before this)
if (empty($sendcontext)) $sendcontext = 'standard';
$mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,$sendtobcc,$deliveryreceipt,-1,'','',$trackid,'', $sendcontext);
if ($mailfile->error)
{
setEventMessages($mailfile->error, $mailfile->errors, 'errors');
$action='presend';
}
else
{
$result=$mailfile->sendfile();
if ($result)
{
// Two hooks are available into method $mailfile->sendfile, so dedicated code is no more required
/*
if (! empty($conf->dolimail->enabled))
{
$mid = (GETPOST('mid','int') ? GETPOST('mid','int') : 0); // Original mail id is set ?
if ($mid)
{
// 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
$movemail = $mailboxconfig->putMail($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,$folder,$deliveryreceipt,$mailfile);
if ($movemail) setEventMessages($langs->trans("MailMovedToImapFolder",$folder), null, 'mesgs');
else setEventMessages($langs->trans("MailMovedToImapFolder_Warning",$folder), null, 'warnings');
}
}*/
// Initialisation of datas of object to call trigger
if (is_object($object))
{
if (empty($actiontypecode)) $actiontypecode='AC_OTH_AUTO'; // Event insert into agenda automatically
$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 (@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;
if (is_array($attachedfiles) && count($attachedfiles)>0) {
$object->attachedfiles = $attachedfiles;
}
if (is_array($sendtouserid) && count($sendtouserid)>0 && !empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
$object->sendtouserid = $sendtouserid;
}
$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))
{
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($db);
$result=$interface->run_triggers($trigger_name,$object,$user,$langs,$conf);
if ($result < 0) {
setEventMessages($interface->error, $interface->errors, 'errors');
}
}
}
// Redirect here
// This avoid sending mail twice if going out and then back to page
$mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($from,2),$mailfile->getValidAddress($sendto,2));
setEventMessages($mesg, null, 'mesgs');
$moreparam='';
if (isset($paramname2) || isset($paramval2)) $moreparam.= '&'.($paramname2?$paramname2:'mid').'='.$paramval2;
header('Location: '.$_SERVER["PHP_SELF"].'?'.($paramname?$paramname:'id').'='.(is_object($object)?$object->id:'').$moreparam);
exit;
}
else
{
$langs->load("other");
$mesg='';
if ($mailfile->error)
{
$mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto);
$mesg.='
'.$mailfile->error;
}
else
{
$mesg.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
}
$mesg.='
';
setEventMessages($mesg, null, 'warnings');
$action = 'presend';
}
}
}
else
{
$langs->load("errors");
setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("MailTo")), null, 'warnings');
dol_syslog('Try to send email with no recipient defined', LOG_WARNING);
$action = 'presend';
}
}
else
{
$langs->load("other");
setEventMessages($langs->trans('ErrorFailedToReadObject',$object->element), null, 'errors');
dol_syslog('Failed to read data of object id='.$object->id.' element='.$object->element);
$action = 'presend';
}
}