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);
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');
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') {
281 } elseif ($fromtype ===
'user') {
282 $from =
dol_string_nospecial($user->getFullName($langs),
' ', array(
",")).
' <'.$user->email.
'>';
283 } elseif ($fromtype ===
'company') {
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)) {
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);
299 } elseif (preg_match(
'/from_template_(\d+)/', $fromtype, $reg)) {
300 $sql =
'SELECT rowid, email_from FROM '.MAIN_DB_PREFIX.
'c_email_templates';
301 $sql .=
' WHERE rowid = '.(int) $reg[1];
302 $resql = $db->query($sql);
303 $obj = $db->fetch_object($resql);
305 $from = $obj->email_from;
312 $message =
GETPOST(
'message',
'restricthtml');
313 $subject =
GETPOST(
'subject',
'restricthtml');
319 $message = preg_replace(
'/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/',
'\1'.$urlwithroot.
'/viewimage.php\2modulepart=medias\3file=\4\5', $message);
321 $sendtobcc =
GETPOST(
'sendtoccc');
324 if (!empty($autocopy)) {
325 $sendtobcc .= (empty($conf->global->$autocopy) ?
'' : (($sendtobcc ?
", " :
"") .
getDolGlobalString($autocopy)));
328 $deliveryreceipt =
GETPOST(
'deliveryreceipt');
330 if ($action ==
'send' || $action ==
'relance') {
345 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
347 $formmail->trackid = $trackid;
349 $attachedfiles = $formmail->get_attached_files();
350 $filepath = $attachedfiles[
'paths'];
351 $filename = $attachedfiles[
'names'];
352 $mimetype = $attachedfiles[
'mimes'];
356 $substitutionarray[
'__EMAIL__'] = $sendto;
357 $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"/>' :
'';
359 $parameters = array(
'mode'=>
'formemail');
365 if (is_object($object) && method_exists($object,
'makeSubstitution')) {
366 $subject = $object->makeSubstitution($subject);
367 $message = $object->makeSubstitution($message);
371 if (empty($sendcontext)) {
372 $sendcontext =
'standard';
374 $mailfile =
new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1,
'',
'', $trackid,
'', $sendcontext,
'', $upload_dir_tmp);
376 if (!empty($mailfile->error) || !empty($mailfile->errors)) {
380 $result = $mailfile->sendfile();
383 if (is_object($object)) {
384 if (empty($actiontypecode)) {
385 $actiontypecode =
'AC_OTH_AUTO';
388 $object->socid = $sendtosocid;
389 $object->sendtoid = $sendtoid;
390 $object->actiontypecode = $actiontypecode;
391 $object->actionmsg = $message;
392 $object->actionmsg2 = $actionmsg2;
394 $object->actionmsg2 = $subject;
397 $object->trackid = $trackid;
398 $object->fk_element = $object->id;
399 $object->elementtype = $object->element;
400 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
401 $object->attachedfiles = $attachedfiles;
403 if (is_array($sendtouserid) && count($sendtouserid) > 0 &&
getDolGlobalString(
'MAIN_MAIL_ENABLED_USER_DEST_SELECT')) {
404 $object->sendtouserid = $sendtouserid;
407 $object->email_msgid = $mailfile->msgid;
408 $object->email_from = $from;
409 $object->email_subject = $subject;
410 $object->email_to = $sendto;
411 $object->email_tocc = $sendtocc;
412 $object->email_tobcc = $sendtobcc;
413 $object->email_subject = $subject;
416 if (!empty($triggersendname) || !empty($trigger_name)) {
418 $result = $object->call_trigger(empty($triggersendname) ? $trigger_name : $triggersendname, $user);
432 $mesg = $langs->trans(
'MailSuccessfulySent', $mailfile->getValidAddress($from, 2), $mailfile->getValidAddress($sendto, 2));
436 if (isset($paramname2) || isset($paramval2)) {
437 $moreparam .=
'&'.($paramname2 ? $paramname2 :
'mid').
'='.$paramval2;
439 header(
'Location: '.$_SERVER[
"PHP_SELF"].
'?'.($paramname ? $paramname :
'id').
'='.(is_object($object) ? $object->id :
'').$moreparam);
442 $langs->load(
"other");
443 $mesg =
'<div class="error">';
444 if (!empty($mailfile->error) || !empty($mailfile->errors)) {
446 if (!empty($mailfile->error)) {
447 $mesg .=
'<br>'.$mailfile->error;
449 if (!empty($mailfile->errors) && is_array($mailfile->errors)) {
450 $mesg .=
'<br>'.implode(
'<br>', $mailfile->errors);
455 $mesg .=
'<br>Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
457 $mesg .=
'<br>Unkown Error, please refers to your administrator';
467 $langs->load(
"errors");
468 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"MailTo")),
null,
'warnings');
469 dol_syslog(
'Try to send email with no recipient defined', LOG_WARNING);
473 $langs->load(
"errors");
474 setEventMessages($langs->trans(
'ErrorFailedToReadObject', $object->element),
null,
'errors');
475 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', $nosalt=0)
Returns a hash (non reversible encryption) of a string.