36if (
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';
70if (
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);
106if (($action ==
'send' || $action ==
'relance') && !
GETPOST(
'addfile') && !
GETPOST(
'removAll') && !
GETPOST(
'removedfile') && !
GETPOST(
'cancel') && !
GETPOST(
'modelselected')) {
107 if (empty($trackid)) {
108 $trackid =
GETPOST(
'trackid',
'aZ09');
112 $vardir = $conf->user->dir_output.
"/".$user->id;
113 $upload_dir_tmp = $vardir.
'/temp';
119 $langs->load(
'mails');
121 if (is_object($object)) {
122 $result = $object->fetch($id);
125 if (method_exists($object,
"fetch_thirdparty") && !in_array($object->element, array(
'member',
'user',
'expensereport',
'societe',
'contact'))) {
126 $resultthirdparty = $object->fetch_thirdparty();
127 $thirdparty = $object->thirdparty;
128 if (is_object($thirdparty)) {
129 $sendtosocid = $thirdparty->id;
131 } elseif ($object->element ==
'member' || $object->element ==
'user') {
132 $thirdparty = $object;
133 if ($object->socid > 0) {
134 $sendtosocid = $object->socid;
136 } elseif ($object->element ==
'expensereport') {
137 $tmpuser =
new User($db);
138 $tmpuser->fetch($object->fk_user_author);
139 $thirdparty = $tmpuser;
140 if ($object->socid > 0) {
141 $sendtosocid = $object->socid;
143 } elseif ($object->element ==
'societe') {
144 $thirdparty = $object;
145 if (is_object($thirdparty) && $thirdparty->id > 0) {
146 $sendtosocid = $thirdparty->id;
148 } elseif ($object->element ==
'contact') {
150 if ($contact->id > 0) {
151 $contact->fetch_thirdparty();
152 $thirdparty = $contact->thirdparty;
153 if (is_object($thirdparty) && $thirdparty->id > 0) {
154 $sendtosocid = $thirdparty->id;
158 dol_print_error(
'',
"Use actions_sendmails.in.php for an element/object '".$object->element.
"' that is not supported");
161 if (is_object($hookmanager)) {
162 $parameters = array();
163 $reshook = $hookmanager->executeHooks(
'initSendToSocid', $parameters, $object, $action);
166 $thirdparty = $mysoc;
174 $sendtouserid = array();
175 $sendtoccuserid = array();
178 $receiver =
GETPOST(
'receiver',
'alphawithlgt');
179 if (!is_array($receiver)) {
180 if ($receiver ==
'-1') {
183 $receiver = array($receiver);
188 if (trim($_POST[
'sendto'])) {
190 $tmparray[] = trim(
GETPOST(
'sendto',
'alphawithlgt'));
193 if (isset($_POST[
'tomail']) && trim($_POST[
'tomail'])) {
195 $tmparray[] = trim(
GETPOST(
'tomail',
'alphawithlgt'));
198 if (count($receiver) > 0) {
200 foreach ($receiver as $key => $val) {
201 if ($val ==
'thirdparty') {
202 $tmparray[] =
dol_string_nospecial($thirdparty->getFullName($langs),
' ', array(
",")).
' <'.$thirdparty->email.
'>';
203 } elseif ($val ==
'contact') {
204 $tmparray[] =
dol_string_nospecial($contact->getFullName($langs),
' ', array(
",")).
' <'.$contact->email.
'>';
205 $sendtoid[] = $contact->id;
207 $tmparray[] = $thirdparty->contact_get_property((
int) $val,
'email');
208 $sendtoid[] = ((int) $val);
213 if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
214 $receiveruser =
GETPOST(
'receiveruser',
'alphawithlgt');
215 if (is_array($receiveruser) && count($receiveruser) > 0) {
216 $fuserdest =
new User($db);
217 foreach ($receiveruser as $key => $val) {
218 $tmparray[] = $fuserdest->user_get_property($val,
'email');
219 $sendtouserid[] = $val;
224 $sendto = implode(
',', $tmparray);
227 $receivercc =
GETPOST(
'receivercc',
'alphawithlgt');
228 if (!is_array($receivercc)) {
229 if ($receivercc ==
'-1') {
230 $receivercc = array();
232 $receivercc = array($receivercc);
236 if (trim($_POST[
'sendtocc'])) {
237 $tmparray[] = trim(
GETPOST(
'sendtocc',
'alphawithlgt'));
239 if (count($receivercc) > 0) {
240 foreach ($receivercc as $key => $val) {
241 if ($val ==
'thirdparty') {
243 $tmparray[] =
dol_string_nospecial($thirdparty->name,
' ', array(
",")).
' <'.$thirdparty->email.
'>';
244 } elseif ($val ==
'contact') {
249 $tmparray[] = $thirdparty->contact_get_property((
int) $val,
'email');
254 if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
255 $receiverccuser =
GETPOST(
'receiverccuser',
'alphawithlgt');
257 if (is_array($receiverccuser) && count($receiverccuser) > 0) {
258 $fuserdest =
new User($db);
259 foreach ($receiverccuser as $key => $val) {
260 $tmparray[] = $fuserdest->user_get_property($val,
'email');
261 $sendtoccuserid[] = $val;
265 $sendtocc = implode(
',', $tmparray);
269 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
270 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
273 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
275 $langs->load(
"commercial");
278 $fromtype =
GETPOST(
'fromtype',
'alpha');
279 if ($fromtype ===
'robot') {
280 $from =
dol_string_nospecial($conf->global->MAIN_MAIL_EMAIL_FROM,
' ', array(
",")).
' <'.$conf->global->MAIN_MAIL_EMAIL_FROM.
'>';
281 } elseif ($fromtype ===
'user') {
282 $from =
dol_string_nospecial($user->getFullName($langs),
' ', array(
",")).
' <'.$user->email.
'>';
283 } elseif ($fromtype ===
'company') {
284 $from =
dol_string_nospecial($conf->global->MAIN_INFO_SOCIETE_NOM,
' ', array(
",")).
' <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.
'>';
285 } elseif (preg_match(
'/user_aliases_(\d+)/', $fromtype, $reg)) {
286 $tmp = explode(
',', $user->email_aliases);
287 $from = trim($tmp[($reg[1] - 1)]);
288 } elseif (preg_match(
'/global_aliases_(\d+)/', $fromtype, $reg)) {
289 $tmp = explode(
',', $conf->global->MAIN_INFO_SOCIETE_MAIL_ALIASES);
290 $from = trim($tmp[($reg[1] - 1)]);
291 } elseif (preg_match(
'/senderprofile_(\d+)_(\d+)/', $fromtype, $reg)) {
292 $sql =
'SELECT rowid, label, email FROM '.MAIN_DB_PREFIX.
'c_email_senderprofile';
293 $sql .=
' WHERE rowid = '.(int) $reg[1];
294 $resql = $db->query($sql);
295 $obj = $db->fetch_object($resql);
304 $message =
GETPOST(
'message',
'restricthtml');
305 $subject =
GETPOST(
'subject',
'restricthtml');
311 $message = preg_replace(
'/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/',
'\1'.$urlwithroot.
'/viewimage.php\2modulepart=medias\3file=\4\5', $message);
313 $sendtobcc =
GETPOST(
'sendtoccc');
316 if (!empty($autocopy)) {
317 $sendtobcc .= (empty($conf->global->$autocopy) ?
'' : (($sendtobcc ?
", " :
"").$conf->global->$autocopy));
320 $deliveryreceipt =
GETPOST(
'deliveryreceipt');
322 if ($action ==
'send' || $action ==
'relance') {
337 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
339 $formmail->trackid = $trackid;
341 $attachedfiles = $formmail->get_attached_files();
342 $filepath = $attachedfiles[
'paths'];
343 $filename = $attachedfiles[
'names'];
344 $mimetype = $attachedfiles[
'mimes'];
348 $substitutionarray[
'__EMAIL__'] = $sendto;
349 $substitutionarray[
'__CHECK_READ__'] = (is_object($object) && is_object($object->thirdparty)) ?
'<img src="'.DOL_MAIN_URL_ROOT.
'/public/emailing/mailing-read.php?tag=undefined&securitykey='.
dol_hash(
getDolGlobalString(
'MAILING_EMAIL_UNSUBSCRIBE_KEY').
"-undefined",
'md5').
'" width="1" height="1" style="width:1px;height:1px" border="0"/>' :
'';
351 $parameters = array(
'mode'=>
'formemail');
357 if (is_object($object) && method_exists($object,
'makeSubstitution')) {
358 $subject = $object->makeSubstitution($subject);
359 $message = $object->makeSubstitution($message);
363 if (empty($sendcontext)) {
364 $sendcontext =
'standard';
366 $mailfile =
new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1,
'',
'', $trackid,
'', $sendcontext,
'', $upload_dir_tmp);
368 if (!empty($mailfile->error) || !empty($mailfile->errors)) {
372 $result = $mailfile->sendfile();
375 if (is_object($object)) {
376 if (empty($actiontypecode)) {
377 $actiontypecode =
'AC_OTH_AUTO';
380 $object->socid = $sendtosocid;
381 $object->sendtoid = $sendtoid;
382 $object->actiontypecode = $actiontypecode;
383 $object->actionmsg = $message;
384 $object->actionmsg2 = $actionmsg2;
385 if (!empty($conf->global->MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT)) {
386 $object->actionmsg2 = $subject;
389 $object->trackid = $trackid;
390 $object->fk_element = $object->id;
391 $object->elementtype = $object->element;
392 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
393 $object->attachedfiles = $attachedfiles;
395 if (is_array($sendtouserid) && count($sendtouserid) > 0 && !empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
396 $object->sendtouserid = $sendtouserid;
399 $object->email_msgid = $mailfile->msgid;
400 $object->email_from = $from;
401 $object->email_subject = $subject;
402 $object->email_to = $sendto;
403 $object->email_tocc = $sendtocc;
404 $object->email_tobcc = $sendtobcc;
405 $object->email_subject = $subject;
406 $object->email_msgid = $mailfile->msgid;
409 if (!empty($triggersendname) || !empty($trigger_name)) {
411 $result = $object->call_trigger(empty($triggersendname) ? $trigger_name : $triggersendname, $user);
425 $mesg = $langs->trans(
'MailSuccessfulySent', $mailfile->getValidAddress($from, 2), $mailfile->getValidAddress($sendto, 2));
429 if (isset($paramname2) || isset($paramval2)) {
430 $moreparam .=
'&'.($paramname2 ? $paramname2 :
'mid').
'='.$paramval2;
432 header(
'Location: '.$_SERVER[
"PHP_SELF"].
'?'.($paramname ? $paramname :
'id').
'='.(is_object($object) ? $object->id :
'').$moreparam);
435 $langs->load(
"other");
436 $mesg =
'<div class="error">';
437 if (!empty($mailfile->error) || !empty($mailfile->errors)) {
439 if (!empty($mailfile->error)) {
440 $mesg .=
'<br>'.$mailfile->error;
442 if (!empty($mailfile->errors) && is_array($mailfile->errors)) {
443 $mesg .=
'<br>'.implode(
'<br>', $mailfile->errors);
447 if (!empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
448 $mesg .=
'<br>Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
450 $mesg .=
'<br>Unkown Error, please refers to your administrator';
460 $langs->load(
"errors");
461 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"MailTo")),
null,
'warnings');
462 dol_syslog(
'Try to send email with no recipient defined', LOG_WARNING);
466 $langs->load(
"errors");
467 setEventMessages($langs->trans(
'ErrorFailedToReadObject', $object->element),
null,
'errors');
468 dol_syslog(
'Failed to read data of object id='.$object->id.
' element='.$object->element);
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
static getValidAddress($address, $format, $encode=0, $maxnumberofemail=0)
Return a formatted address string for SMTP protocol.
Class to manage Dolibarr users.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_remove_file_process($filenb, $donotupdatesession=0, $donotdeletefile=1, $trackid='')
Remove an uploaded file (for example after submitting a new file a mail form).
dol_add_file_process($upload_dir, $allowoverwrite=0, $donotupdatesession=0, $varfiles='addedfile', $savingdocmask='', $link=null, $trackid='', $generatethumbs=1, $object=null)
Get and save an upload file (for example after submitting a new file a mail form).
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null, $include=null)
Return array of possible common substitutions.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
dol_hash($chain, $type='0')
Returns a hash (non reversible encryption) of a string.