36 if (
GETPOST(
'addfile',
'alpha')) {
37 $trackid =
GETPOST(
'trackid',
'aZ09');
39 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
42 $vardir = $conf->user->dir_output.
"/".$user->id;
43 $upload_dir_tmp = $vardir.
'/temp';
53 $trackid =
GETPOST(
'trackid',
'aZ09');
55 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
58 $vardir = $conf->user->dir_output.
"/".$user->id;
59 $upload_dir_tmp = $vardir.
'/temp';
70 if (
GETPOST(
'removAll',
'alpha')) {
71 $trackid =
GETPOST(
'trackid',
'aZ09');
73 $listofpaths = array();
74 $listofnames = array();
75 $listofmimes = array();
76 $keytoavoidconflict = empty($trackid) ?
'' :
'-'.$trackid;
77 if (!empty($_SESSION[
"listofpaths".$keytoavoidconflict])) {
78 $listofpaths = explode(
';', $_SESSION[
"listofpaths".$keytoavoidconflict]);
80 if (!empty($_SESSION[
"listofnames".$keytoavoidconflict])) {
81 $listofnames = explode(
';', $_SESSION[
"listofnames".$keytoavoidconflict]);
83 if (!empty($_SESSION[
"listofmimes".$keytoavoidconflict])) {
84 $listofmimes = explode(
';', $_SESSION[
"listofmimes".$keytoavoidconflict]);
87 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
89 $formmail->trackid = $trackid;
91 foreach ($listofpaths as $key => $value) {
92 $pathtodelete = $value;
93 $filetodelete = $listofnames[$key];
96 $langs->load(
"other");
97 setEventMessages($langs->trans(
"FileWasRemoved", $filetodelete),
null,
'mesgs');
99 $formmail->remove_attached_files($key);
106 if (($action ==
'send' || $action ==
'relance') && !
GETPOST(
'addfile') && !
GETPOST(
'removAll') && !
GETPOST(
'removedfile') && !
GETPOST(
'cancel') && !
GETPOST(
'modelselected')) {
107 if (empty($trackid)) {
108 $trackid =
GETPOST(
'trackid',
'aZ09');
115 $langs->load(
'mails');
117 if (is_object($object)) {
118 $result = $object->fetch($id);
121 if (method_exists($object,
"fetch_thirdparty") && !in_array($object->element, array(
'member',
'user',
'expensereport',
'societe',
'contact'))) {
122 $resultthirdparty = $object->fetch_thirdparty();
123 $thirdparty = $object->thirdparty;
124 if (is_object($thirdparty)) {
125 $sendtosocid = $thirdparty->id;
127 } elseif ($object->element ==
'member' || $object->element ==
'user') {
128 $thirdparty = $object;
129 if ($object->socid > 0) {
130 $sendtosocid = $object->socid;
132 } elseif ($object->element ==
'expensereport') {
133 $tmpuser =
new User($db);
134 $tmpuser->fetch($object->fk_user_author);
135 $thirdparty = $tmpuser;
136 if ($object->socid > 0) {
137 $sendtosocid = $object->socid;
139 } elseif ($object->element ==
'societe') {
140 $thirdparty = $object;
141 if (is_object($thirdparty) && $thirdparty->id > 0) {
142 $sendtosocid = $thirdparty->id;
144 } elseif ($object->element ==
'contact') {
146 if ($contact->id > 0) {
147 $contact->fetch_thirdparty();
148 $thirdparty = $contact->thirdparty;
149 if (is_object($thirdparty) && $thirdparty->id > 0) {
150 $sendtosocid = $thirdparty->id;
154 dol_print_error(
'',
"Use actions_sendmails.in.php for an element/object '".$object->element.
"' that is not supported");
157 if (is_object($hookmanager)) {
158 $parameters = array();
159 $reshook = $hookmanager->executeHooks(
'initSendToSocid', $parameters, $object, $action);
162 $thirdparty = $mysoc;
170 $sendtouserid = array();
171 $sendtoccuserid = array();
174 $receiver =
GETPOST(
'receiver',
'alphawithlgt');
175 if (!is_array($receiver)) {
176 if ($receiver ==
'-1') {
179 $receiver = array($receiver);
184 if (trim($_POST[
'sendto'])) {
186 $tmparray[] = trim(
GETPOST(
'sendto',
'alphawithlgt'));
189 if (trim($_POST[
'tomail'])) {
191 $tmparray[] = trim(
GETPOST(
'tomail',
'alphawithlgt'));
194 if (count($receiver) > 0) {
196 foreach ($receiver as $key => $val) {
197 if ($val ==
'thirdparty') {
198 $tmparray[] =
dol_string_nospecial($thirdparty->getFullName($langs),
' ', array(
",")).
' <'.$thirdparty->email.
'>';
199 } elseif ($val ==
'contact') {
200 $tmparray[] =
dol_string_nospecial($contact->getFullName($langs),
' ', array(
",")).
' <'.$contact->email.
'>';
201 $sendtoid[] = $contact->id;
203 $tmparray[] = $thirdparty->contact_get_property((
int) $val,
'email');
204 $sendtoid[] = ((int) $val);
209 if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
210 $receiveruser =
GETPOST(
'receiveruser',
'alphawithlgt');
211 if (is_array($receiveruser) && count($receiveruser) > 0) {
212 $fuserdest =
new User($db);
213 foreach ($receiveruser as $key => $val) {
214 $tmparray[] = $fuserdest->user_get_property($val,
'email');
215 $sendtouserid[] = $val;
220 $sendto = implode(
',', $tmparray);
223 $receivercc =
GETPOST(
'receivercc',
'alphawithlgt');
224 if (!is_array($receivercc)) {
225 if ($receivercc ==
'-1') {
226 $receivercc = array();
228 $receivercc = array($receivercc);
232 if (trim($_POST[
'sendtocc'])) {
233 $tmparray[] = trim(
GETPOST(
'sendtocc',
'alphawithlgt'));
235 if (count($receivercc) > 0) {
236 foreach ($receivercc as $key => $val) {
237 if ($val ==
'thirdparty') {
239 $tmparray[] =
dol_string_nospecial($thirdparty->name,
' ', array(
",")).
' <'.$thirdparty->email.
'>';
240 } elseif ($val ==
'contact') {
245 $tmparray[] = $thirdparty->contact_get_property((
int) $val,
'email');
250 if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
251 $receiverccuser =
GETPOST(
'receiverccuser',
'alphawithlgt');
253 if (is_array($receiverccuser) && count($receiverccuser) > 0) {
254 $fuserdest =
new User($db);
255 foreach ($receiverccuser as $key => $val) {
256 $tmparray[] = $fuserdest->user_get_property($val,
'email');
257 $sendtoccuserid[] = $val;
261 $sendtocc = implode(
',', $tmparray);
265 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
266 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
269 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
271 $langs->load(
"commercial");
274 $fromtype =
GETPOST(
'fromtype',
'alpha');
275 if ($fromtype ===
'robot') {
276 $from =
dol_string_nospecial($conf->global->MAIN_MAIL_EMAIL_FROM,
' ', array(
",")).
' <'.$conf->global->MAIN_MAIL_EMAIL_FROM.
'>';
277 } elseif ($fromtype ===
'user') {
278 $from =
dol_string_nospecial($user->getFullName($langs),
' ', array(
",")).
' <'.$user->email.
'>';
279 } elseif ($fromtype ===
'company') {
280 $from =
dol_string_nospecial($conf->global->MAIN_INFO_SOCIETE_NOM,
' ', array(
",")).
' <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.
'>';
281 } elseif (preg_match(
'/user_aliases_(\d+)/', $fromtype, $reg)) {
282 $tmp = explode(
',', $user->email_aliases);
283 $from = trim($tmp[($reg[1] - 1)]);
284 } elseif (preg_match(
'/global_aliases_(\d+)/', $fromtype, $reg)) {
285 $tmp = explode(
',', $conf->global->MAIN_INFO_SOCIETE_MAIL_ALIASES);
286 $from = trim($tmp[($reg[1] - 1)]);
287 } elseif (preg_match(
'/senderprofile_(\d+)_(\d+)/', $fromtype, $reg)) {
288 $sql =
'SELECT rowid, label, email FROM '.MAIN_DB_PREFIX.
'c_email_senderprofile';
289 $sql .=
' WHERE rowid = '.(int) $reg[1];
290 $resql = $db->query($sql);
291 $obj = $db->fetch_object(
$resql);
300 $message =
GETPOST(
'message',
'restricthtml');
301 $subject =
GETPOST(
'subject',
'restricthtml');
307 $message = preg_replace(
'/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/',
'\1'.$urlwithroot.
'/viewimage.php\2modulepart=medias\3file=\4\5', $message);
309 $sendtobcc =
GETPOST(
'sendtoccc');
312 if (!empty($autocopy)) {
313 $sendtobcc .= (empty($conf->global->$autocopy) ?
'' : (($sendtobcc ?
", " :
"").$conf->global->$autocopy));
316 $deliveryreceipt =
GETPOST(
'deliveryreceipt');
318 if ($action ==
'send' || $action ==
'relance') {
333 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
335 $formmail->trackid = $trackid;
337 $attachedfiles = $formmail->get_attached_files();
338 $filepath = $attachedfiles[
'paths'];
339 $filename = $attachedfiles[
'names'];
340 $mimetype = $attachedfiles[
'mimes'];
344 $substitutionarray[
'__EMAIL__'] = $sendto;
345 $substitutionarray[
'__CHECK_READ__'] = (is_object($object) && is_object($object->thirdparty)) ?
'<img src="'.DOL_MAIN_URL_ROOT.
'/public/emailing/mailing-read.php?tag='.urlencode($object->thirdparty->tag).
'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).
'" width="1" height="1" style="width:1px;height:1px" border="0"/>' :
'';
347 $parameters = array(
'mode'=>
'formemail');
353 if (is_object($object) && method_exists($object,
'makeSubstitution')) {
354 $subject = $object->makeSubstitution($subject);
355 $message = $object->makeSubstitution($message);
359 if (empty($sendcontext)) {
360 $sendcontext =
'standard';
362 $mailfile =
new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1,
'',
'', $trackid,
'', $sendcontext);
364 if ($mailfile->error) {
368 $result = $mailfile->sendfile();
371 if (is_object($object)) {
372 if (empty($actiontypecode)) {
373 $actiontypecode =
'AC_OTH_AUTO';
376 $object->socid = $sendtosocid;
377 $object->sendtoid = $sendtoid;
378 $object->actiontypecode = $actiontypecode;
379 $object->actionmsg = $message;
380 $object->actionmsg2 = $actionmsg2;
382 $object->trackid = $trackid;
383 $object->fk_element = $object->id;
384 $object->elementtype = $object->element;
385 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
386 $object->attachedfiles = $attachedfiles;
388 if (is_array($sendtouserid) && count($sendtouserid) > 0 && !empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
389 $object->sendtouserid = $sendtouserid;
392 $object->email_msgid = $mailfile->msgid;
393 $object->email_from = $from;
394 $object->email_subject = $subject;
395 $object->email_to = $sendto;
396 $object->email_tocc = $sendtocc;
397 $object->email_tobcc = $sendtobcc;
398 $object->email_subject = $subject;
399 $object->email_msgid = $mailfile->msgid;
402 if (!empty($triggersendname) || !empty($trigger_name)) {
404 $result = $object->call_trigger(empty($triggersendname) ? $trigger_name : $triggersendname, $user);
418 $mesg = $langs->trans(
'MailSuccessfulySent', $mailfile->getValidAddress($from, 2), $mailfile->getValidAddress($sendto, 2));
422 if (isset($paramname2) || isset($paramval2)) {
423 $moreparam .=
'&'.($paramname2 ? $paramname2 :
'mid').
'='.$paramval2;
425 header(
'Location: '.$_SERVER[
"PHP_SELF"].
'?'.($paramname ? $paramname :
'id').
'='.(is_object($object) ? $object->id :
'').$moreparam);
428 $langs->load(
"other");
429 $mesg =
'<div class="error">';
430 if ($mailfile->error) {
432 $mesg .=
'<br>'.$mailfile->error;
435 if (!empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
436 $mesg .=
'<br>Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
438 $mesg .=
'<br>Unkown Error, please refers to your administrator';
448 $langs->load(
"errors");
449 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"MailTo")),
null,
'warnings');
450 dol_syslog(
'Try to send email with no recipient defined', LOG_WARNING);
454 $langs->load(
"errors");
455 setEventMessages($langs->trans(
'ErrorFailedToReadObject', $object->element),
null,
'errors');
456 dol_syslog(
'Failed to read data of object id='.$object->id.
' element='.$object->element);