dolibarr 25.0.0-alpha
actions_sendmails.inc.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
5* Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
6*
7* This program is free software; you can redistribute it and/or modify
8* it under the terms of the GNU General Public License as published by
9* the Free Software Foundation; either version 3 of the License, or
10* (at your option) any later version.
11*
12* This program is distributed in the hope that it will be useful,
13* but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15* GNU General Public License for more details.
16*
17* You should have received a copy of the GNU General Public License
18* along with this program. If not, see <https://www.gnu.org/licenses/>.
19* or see https://www.gnu.org/
20*/
21
48'
49@phan-var-force Societe $mysoc
50@phan-var-force CommonObject $object
51@phan-var-force int $error
52';
53
54
55/*
56 * Add file in email form
57 */
58if (GETPOST('addfile', 'alpha')) {
59 $trackid = GETPOST('trackid', 'aZ09');
60
61 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
62
63 // Set tmp user directory
64 $vardir = $conf->user->dir_output."/".$user->id;
65 $upload_dir_tmp = $vardir.'/temp'; // TODO Add $keytoavoidconflict in upload_dir path
66
67 dol_add_file_process($upload_dir_tmp, 1, 0, 'addedfile', '', null, $trackid, 0);
68 $action = 'presend';
69}
70
71/*
72 * Remove file in email form
73 */
74if (GETPOST('removedfile') && !GETPOST('removeAll')) {
75 $trackid = GETPOST('trackid', 'aZ09');
76
77 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
78
79 // Set tmp user directory
80 $vardir = $conf->user->dir_output."/".$user->id;
81 $upload_dir_tmp = $vardir.'/temp'; // TODO Add $keytoavoidconflict in upload_dir path
82
83 // TODO Delete only files that was uploaded from email form. This can be addressed by adding the trackid into the temp path then changing donotdeletefile to 2 instead of 1 to say "delete only if into temp dir"
84 // GETPOST('removedfile','alpha') is position of file into $_SESSION["listofpaths"...] array.
85 dol_remove_file_process(GETPOSTINT('removedfile'), 0, 1, $trackid); // We do not delete because if file is the official PDF of doc, we don't want to remove it physically
86 $action = 'presend';
87}
88
89/*
90 * Remove all files in email form
91 */
92if (GETPOST('removeAll', 'alpha')) {
93 $trackid = GETPOST('trackid', 'aZ09');
94
95 $listofpaths = array();
96 $listofnames = array();
97 $listofmimes = array();
98 $keytoavoidconflict = empty($trackid) ? '' : '-'.$trackid;
99 if (!empty($_SESSION["listofpaths".$keytoavoidconflict])) {
100 $listofpaths = explode(';', $_SESSION["listofpaths".$keytoavoidconflict]);
101 }
102 if (!empty($_SESSION["listofnames".$keytoavoidconflict])) {
103 $listofnames = explode(';', $_SESSION["listofnames".$keytoavoidconflict]);
104 }
105 if (!empty($_SESSION["listofmimes".$keytoavoidconflict])) {
106 $listofmimes = explode(';', $_SESSION["listofmimes".$keytoavoidconflict]);
107 }
108
109 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
110 $formmail = new FormMail($db);
111 $formmail->trackid = $trackid;
112
113 foreach ($listofpaths as $key => $value) {
114 $pathtodelete = $value;
115 $filetodelete = $listofnames[$key];
116 $result = dol_delete_file($pathtodelete, 1); // Delete uploaded Files
117
118 $langs->load("other");
119 setEventMessages($langs->trans("FileWasRemoved", $filetodelete), null, 'mesgs');
120
121 $formmail->remove_attached_files($key); // Update Session
122 }
123}
124
125/*
126 * Send mail
127 */
128if (($action == 'send' || $action == 'relance') && !GETPOST('addfile') && !GETPOST('removeAll') && !GETPOST('removedfile') && !GETPOST('cancel') && !GETPOST('modelselected')) {
129 if (empty($trackid)) {
130 $trackid = GETPOST('trackid', 'aZ09');
131 }
132
133 // Set tmp user directory (used to convert images embedded as img src=data:image)
134 $vardir = $conf->user->dir_output."/".$user->id;
135 $upload_dir_tmp = $vardir.'/temp'; // TODO Add $keytoavoidconflict in upload_dir path
136
137 $subject = '';
138 //$actionmsg = '';
139 $actionmsg2 = '';
140 $thirdparty = null;
141 $contact = null;
142 $result = 0;
143
144 $langs->load('mails');
145
146 $sendtosocid = 0; // Id of related thirdparty
147
148 if (is_object($object)) {
149 $result = $object->fetch($id);
150
151 if (method_exists($object, "fetch_thirdparty") && !in_array($object->element, array('member', 'user', 'expensereport', 'societe', 'contact'))) {
152 $object->fetch_thirdparty();
153 $thirdparty = $object->thirdparty;
154 if (is_object($thirdparty)) {
155 $sendtosocid = $thirdparty->id;
156 }
157 } elseif ($object->element == 'member' || $object->element == 'user') {
158 '@phan-var-force Adherent|User $object';
159 $thirdparty = $object;
160 if ($object->socid > 0) {
161 $sendtosocid = $object->socid;
162 }
163 } elseif ($object->element == 'expensereport') {
164 '@phan-var-force ExpenseReport $object';
165 $tmpuser = new User($db);
166 $tmpuser->fetch($object->fk_user_author);
167 $thirdparty = $tmpuser;
168 if ($object->socid > 0) {
169 $sendtosocid = $object->socid;
170 }
171 } elseif ($object->element == 'societe') {
172 '@phan-var-force Societe $object';
173 $thirdparty = $object;
174 if (is_object($thirdparty) && $thirdparty->id > 0) {
175 $sendtosocid = $thirdparty->id;
176 }
177 } elseif ($object->element == 'contact') {
178 '@phan-var-force Contact $object';
179 $contact = $object;
180 if ($contact->id > 0) {
181 $contact->fetch_thirdparty();
182 $thirdparty = $contact->thirdparty;
183 if (is_object($thirdparty) && $thirdparty->id > 0) {
184 $sendtosocid = $thirdparty->id;
185 }
186 }
187 } else {
188 dol_print_error(null, "Use actions_sendmails.in.php for an element/object '".$object->element."' that is not supported");
189 }
190
191 if (is_object($hookmanager)) {
192 $parameters = array();
193 $reshook = $hookmanager->executeHooks('initSendToSocid', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
194 }
195 } else {
196 $thirdparty = $mysoc;
197 $result = 1; // No object to fetch (e.g. Setup -> Emails -> send test email): consider OK
198 }
199
200 if ($result > 0) {
201 $email_from = '';
202 $sendto = '';
203 $sendtocc = '';
204 $sendtobcc = '';
205 $sendtoid = array();
206 $sendtouserid = array();
207 $sendtoccuserid = array();
208
209 // Define $sendto
210 $receiver = GETPOST('receiver', 'alphawithlgt');
211 if (!is_array($receiver)) {
212 if ($receiver == '-1') {
213 $receiver = array();
214 } else {
215 $receiver = array($receiver);
216 }
217 }
218
219 $tmparray = array();
220 if (trim(GETPOST('sendto', 'alphawithlgt'))) {
221 // Recipients are provided into free text field
222 $tmparray[] = trim(GETPOST('sendto', 'alphawithlgt'));
223 }
224
225 if (trim(GETPOST('tomail', 'alphawithlgt'))) {
226 // Recipients are provided into free hidden text field
227 $tmparray[] = trim(GETPOST('tomail', 'alphawithlgt'));
228 }
229
230 if (count($receiver) > 0) {
231 // Recipient was provided from combo list
232 foreach ($receiver as $key => $val) {
233 if ($val == 'thirdparty') { // Key selected means current third party ('thirdparty' may be used for current member or current user too)
234 $thirdpartyEmail = (is_object($thirdparty) && !empty($thirdparty->email)) ? (string) $thirdparty->email : '';
235 if ($thirdpartyEmail !== '') {
236 $thirdpartyLabel = '';
237 if (is_object($thirdparty)) {
238 if (method_exists($thirdparty, 'getFullName')) {
239 $thirdpartyLabel = (string) $thirdparty->getFullName($langs);
240 } elseif (!empty($thirdparty->name)) {
241 $thirdpartyLabel = (string) $thirdparty->name;
242 } elseif (!empty($thirdparty->nom)) {
243 $thirdpartyLabel = (string) $thirdparty->nom;
244 }
245 }
246 if ($thirdpartyLabel !== '') {
247 $tmparray[] = dol_string_nospecial($thirdpartyLabel, ' ', array(",")).' <'.$thirdpartyEmail.'>';
248 } else {
249 $tmparray[] = $thirdpartyEmail;
250 }
251 }
252 } elseif ($val == 'contact') { // Key selected means current contact
253 $tmparray[] = dol_string_nospecial($contact->getFullName($langs), ' ', array(",")).' <'.$contact->email.'>';
254 $sendtoid[] = $contact->id;
255 } elseif ($val && $object->element == 'project' && empty($object->socid)) { // $val is the Id of a contact
256 $contact = new Contact($db);
257 $ret = $contact->fetch((int) $val);
258 if ($ret > 0 && !empty($contact->socid)) {
259 $thirdparty = new Societe($db);
260 $thirdparty->fetch($contact->socid);
261 $tmparray[] = $thirdparty->contact_get_property((int) $val, 'email');
262 $sendtoid[] = ((int) $val);
263 }
264 } elseif (is_numeric($val) && $val > 0) { // $val is the Id of a contact
265 $tmparray[] = $thirdparty->contact_get_property((int) $val, 'email');
266 $sendtoid[] = ((int) $val);
267 } elseif ($val !== '') { // $val is a free-typed "Name <email>" or "email" tag
268 $tmparray[] = sanitizeVal($val, 'alphawithlgt');
269 }
270 }
271 }
272
273 if (getDolGlobalString('MAIN_MAIL_ENABLED_USER_DEST_SELECT')) {
274 $receiveruser = GETPOST('receiveruser', 'alphawithlgt');
275 if (is_array($receiveruser) && count($receiveruser) > 0) {
276 $fuserdest = new User($db);
277 foreach ($receiveruser as $key => $val) {
278 $tmparray[] = $fuserdest->user_get_property($val, 'email');
279 $sendtouserid[] = $val;
280 }
281 }
282 }
283
284 $sendto = implode(',', $tmparray);
285
286 // Define $sendtocc
287 $receivercc = GETPOST('receivercc', 'alphawithlgt');
288 if (!is_array($receivercc)) {
289 if ($receivercc == '-1') {
290 $receivercc = array();
291 } else {
292 $receivercc = array($receivercc);
293 }
294 }
295 $tmparray = array();
296 if (trim(GETPOST('sendtocc', 'alphawithlgt'))) {
297 $tmparray[] = trim(GETPOST('sendtocc', 'alphawithlgt'));
298 }
299 if (count($receivercc) > 0) {
300 foreach ($receivercc as $key => $val) {
301 if ($val == 'thirdparty') { // Key selected means current thirdparty (may be usd for current member or current user too)
302 // Recipient was provided from combo list
303 $tmparray[] = dol_string_nospecial((string) $thirdparty->name, ' ', array(",")).' <'.$thirdparty->email.'>';
304 } elseif ($val == 'contact') { // Key selected means current contact
305 // Recipient was provided from combo list
306 $tmparray[] = dol_string_nospecial((string) $contact->name, ' ', array(",")).' <'.$contact->email.'>';
307 //$sendtoid[] = $contact->id; TODO Add also id of contact in CC ?
308 } elseif (is_numeric($val) && $val > 0) { // $val is the Id of a contact
309 $tmparray[] = $thirdparty->contact_get_property((int) $val, 'email');
310 //$sendtoid[] = ((int) $val); TODO Add also id of contact in CC ?
311 } elseif ($val !== '') { // $val is a free-typed "Name <email>" or "email" tag
312 $tmparray[] = sanitizeVal($val, 'alphawithlgt');
313 }
314 }
315 }
316 if (getDolGlobalString('MAIN_MAIL_ENABLED_USER_DEST_SELECT')) {
317 $receiverccuser = GETPOST('receiverccuser', 'alphawithlgt');
318
319 if (is_array($receiverccuser) && count($receiverccuser) > 0) {
320 $fuserdest = new User($db);
321 foreach ($receiverccuser as $key => $val) {
322 $tmparray[] = $fuserdest->user_get_property($val, 'email');
323 $sendtoccuserid[] = $val;
324 }
325 }
326 }
327 $sendtocc = implode(',', $tmparray);
328
329 if (dol_strlen($sendto)) {
330 // Define $urlwithroot
331 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
332 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
333 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
334
335 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
336
337 $langs->load("commercial");
338
339 $reg = array();
340 $fromtype = GETPOST('fromtype', 'alpha');
341 $emailsendersignature = '';
342 if ($fromtype === 'robot') {
343 $email_from = dol_string_nospecial(getDolGlobalString('MAIN_MAIL_EMAIL_FROM'), ' ', array(",")).' <' . getDolGlobalString('MAIN_MAIL_EMAIL_FROM').'>';
344 } elseif ($fromtype === 'user') {
345 $email_from = dol_string_nospecial($user->getFullName($langs), ' ', array(",")).' <'.$user->email.'>';
346 } elseif ($fromtype === 'company') {
347 $email_from = dol_string_nospecial(getDolGlobalString('MAIN_INFO_SOCIETE_NOM'), ' ', array(",")).' <' . getDolGlobalString('MAIN_INFO_SOCIETE_MAIL').'>';
348 } elseif (preg_match('/global_aliases_(\d+)/', $fromtype, $reg)) {
349 $tmp = explode(',', getDolGlobalString('MAIN_INFO_SOCIETE_MAIL_ALIASES'));
350 $email_from = trim($tmp[((int) $reg[1] - 1)]);
351 } elseif (preg_match('/senderprofile_(\d+)_(\d+)/', $fromtype, $reg)) {
352 $sql = "SELECT rowid, label, email, signature FROM ".MAIN_DB_PREFIX."c_email_senderprofile";
353 $sql .= " WHERE rowid = ".(int) $reg[1];
354 $resql = $db->query($sql);
355 $obj = $db->fetch_object($resql);
356 if ($obj) {
357 $email_from = dol_string_nospecial($obj->label, ' ', array(",")).' <'.$obj->email.'>';
358 $emailsendersignature = $obj->signature;
359 }
360 } elseif (preg_match('/from_template_(\d+)/', $fromtype, $reg)) {
361 $sql = "SELECT rowid, email_from FROM ".MAIN_DB_PREFIX."c_email_templates";
362 $sql .= " WHERE rowid = ".(int) $reg[1];
363 $resql = $db->query($sql);
364 $obj = $db->fetch_object($resql);
365 if ($obj) {
366 $email_from = $obj->email_from;
367 }
368 } else {
369 $email_from = dol_string_nospecial(GETPOST('fromname'), ' ', array(",")).' <'.GETPOST('frommail').'>';
370 }
371
372 $replyto = '';
373 if (GETPOST('replytomail')) {
374 $replyto = dol_string_nospecial(GETPOST('replytoname'), ' ', array(","));
375 $replyto .= ($replyto ? ' ' : '').'<'.GETPOST('replytomail').'>';
376 }
377
378 $message = GETPOST('message', 'restricthtml');
379 $subject = GETPOST('subject', 'restricthtml');
380
381 // Make a change into HTML code to allow to include images from medias directory with an external reabable URL.
382 // <img alt="" src="/dolibarr_dev/htdocs/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
383 // become
384 // <img alt="" src="'.$urlwithroot.'viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
385 $message = preg_replace('/(<img.*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^\/]*\/>)/', '\1'.$urlwithroot.'/viewimage.php\2modulepart=medias\3file=\4\5', $message);
386
387 $tmparrayccc = array();
388 if (trim(GETPOST('sendtoccc', 'alphawithlgt'))) {
389 $tmparrayccc[] = trim(GETPOST('sendtoccc', 'alphawithlgt'));
390 }
391 $receiverccc = GETPOST('receiverccc', 'alphawithlgt');
392 if (!is_array($receiverccc)) {
393 $receiverccc = ($receiverccc && $receiverccc != '-1') ? array($receiverccc) : array();
394 }
395 foreach ($receiverccc as $val) {
396 if ($val == 'thirdparty') { // Key selected means current thirdparty
397 $tmparrayccc[] = dol_string_nospecial((string) $thirdparty->name, ' ', array(",")).' <'.$thirdparty->email.'>';
398 } elseif ($val == 'contact') { // Key selected means current contact
399 $tmparrayccc[] = dol_string_nospecial((string) $contact->name, ' ', array(",")).' <'.$contact->email.'>';
400 } elseif (is_numeric($val) && $val > 0) { // $val is the Id of a contact
401 $tmparrayccc[] = $thirdparty->contact_get_property((int) $val, 'email');
402 } elseif ($val !== '') { // $val is a free-typed "Name <email>" or "email" tag
403 $tmparrayccc[] = sanitizeVal($val, 'alphawithlgt');
404 }
405 }
406 $sendtobcc = implode(',', $tmparrayccc);
407 // Autocomplete the $sendtobcc
408 // $autocopy can be MAIN_MAIL_AUTOCOPY_PROPOSAL_TO, MAIN_MAIL_AUTOCOPY_ORDER_TO, MAIN_MAIL_AUTOCOPY_INVOICE_TO, MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO...
409 if (!empty($autocopy)) {
410 $sendtobcc .= (getDolGlobalString($autocopy) ? (($sendtobcc ? ", " : "") . getDolGlobalString($autocopy)) : '');
411 }
412
413 $deliveryreceipt = GETPOSTINT('deliveryreceipt') ? 1 : 0;
414
415 if ($action == 'send' || $action == 'relance') {
416 $actionmsg2 = $langs->transnoentities('MailSentByTo', CMailFile::getValidAddress($email_from, 4, 0, 1), CMailFile::getValidAddress($sendto, 4, 0, 1));
417 /*if ($message) {
418 $actionmsg = $langs->transnoentities('MailFrom').': '.dol_escape_htmltag($email_from);
419 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTo').': '.dol_escape_htmltag($sendto));
420 if ($sendtocc) {
421 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".dol_escape_htmltag($sendtocc));
422 }
423 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
424 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
425 $actionmsg = dol_concatdesc($actionmsg, $message);
426 }*/
427 }
428
429 // Create form object
430 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
431 $formmail = new FormMail($db);
432 $formmail->trackid = $trackid; // $trackid must be defined
433
434 $attachedfiles = $formmail->get_attached_files();
435 $filepath = $attachedfiles['paths'];
436 $filename = $attachedfiles['names'];
437 $mimetype = $attachedfiles['mimes'];
438
439 // Make substitution in email content
440 $substitutionarray = getCommonSubstitutionArray($langs, 0, null, $object);
441
442 $substitutionarray['__SENDEREMAIL_SIGNATURE__'] = (empty($emailsendersignature) ? $user->signature : $emailsendersignature);
443 $substitutionarray['__EMAIL__'] = $sendto;
444 $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"/>' : '';
445
446 $parameters = array('mode' => 'formemail');
447 complete_substitutions_array($substitutionarray, $langs, $object, $parameters);
448
449 $subject = make_substitutions($subject, $substitutionarray);
450 $message = make_substitutions($message, $substitutionarray);
451
452 if (is_object($object) && method_exists($object, 'makeSubstitution')) {
453 $subject = $object->makeSubstitution($subject);
454 $message = $object->makeSubstitution($message);
455 }
456
457 // Send mail (substitutionarray must be done just before this)
458 if (empty($sendcontext)) {
459 $sendcontext = 'standard';
460 }
461 $mailfile = new CMailFile($subject, $sendto, $email_from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1, '', '', $trackid, '', $sendcontext, $replyto, $upload_dir_tmp);
462
463 if (!empty($mailfile->error) || !empty($mailfile->errors)) {
464 setEventMessages($mailfile->error, $mailfile->errors, 'errors');
465 $action = 'presend';
466 } else {
467 $result = $mailfile->sendfile();
468 if ($result) {
469 // Initialisation of datas of object to call trigger
470 if (is_object($object)) {
471 $db->begin(); // Transaction for post action must start after sending email to avoid lock when sending email that may be long
472
473 if (empty($actiontypecode)) {
474 $actiontypecode = 'AC_EMAIL'; // Event inserted into agenda automatically
475 }
476
477 $object->socid = $sendtosocid; // To link to a company
478 $object->sendtoid = $sendtoid; // To link to contact-addresses. This is an array.
479 $object->actiontypecode = $actiontypecode; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
480 $object->actionmsg = $message; // Long text
481 $object->actionmsg2 = $actionmsg2; // Short text ($langs->transnoentities('MailSentByTo')...);
482 if (getDolGlobalString('MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT')) {
483 $object->actionmsg2 = $subject; // Short text
484 }
485
486 $object->trackid = $trackid;
487 $object->fk_element = $object->id;
488 $object->elementtype = $object->element;
489 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
490 $object->attachedfiles = $attachedfiles;
491 }
492 if (is_array($sendtouserid) && count($sendtouserid) > 0 && getDolGlobalString('MAIN_MAIL_ENABLED_USER_DEST_SELECT')) {
493 $object->sendtouserid = $sendtouserid;
494 }
495
496 // TODO Fix this: Such properties does not exists on all objects
497 $object->context['email_msgid'] = $mailfile->msgid;
498 $object->context['email_from'] = $email_from;
499 $object->context['email_subject'] = $subject;
500 $object->context['email_to'] = $sendto;
501 $object->context['email_tocc'] = $sendtocc;
502 $object->context['email_tobcc'] = $sendtobcc;
503
504 $object->email_msgid = $mailfile->msgid; // @todo Set msgid into $mailfile after sending
505 $object->email_from = $email_from;
506 $object->email_subject = $subject;
507 $object->email_to = $sendto;
508 $object->email_tocc = $sendtocc;
509 $object->email_tobcc = $sendtobcc;
510
511 // Call of triggers (you should have set $triggersendname to execute trigger.
512 if (!empty($triggersendname)) {
513 if ($triggersendname == 'BILL_SENTBYMAIL' && $object instanceof Facture) {
514 /* @var Facture $object */
515
516 // If sending email for invoice, we increase the counter of invoices sent by email
517 $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET email_sent_counter = email_sent_counter + 1";
518 $sql .= " WHERE rowid = ".((int) $object->id);
519
520 $resql = $db->query($sql);
521 if ($resql) {
522 $object->email_sent_counter += 1;
523 }
524 }
525
526 $result = $object->call_trigger($triggersendname, $user); // @phan-suppress-current-line PhanPossiblyUndeclaredGlobalVariable
527 if ($result < 0) {
528 $error++;
529 }
530 if ($error) {
531 setEventMessages($object->error, $object->errors, 'errors');
532 }
533 }
534 // End call of triggers
535
536 if (!$error) {
537 $db->commit();
538 } else {
539 $db->rollback();
540 }
541 }
542
543 // Redirect here
544 // This avoid sending mail twice if going out and then back to page
545 $mesg = $langs->trans('MailSuccessfulySent', $mailfile->getValidAddress($email_from, 2), $mailfile->getValidAddress($sendto, 2));
546 setEventMessages($mesg, null, 'mesgs');
547
548 header('Location: '.$_SERVER["PHP_SELF"].'?'.($paramname ?? 'id').'='.(is_object($object) ? $object->id : ''));
549 exit;
550 } else {
551 $langs->load("other");
552 $mesg = '<div class="error">';
553 if (!empty($mailfile->error) || !empty($mailfile->errors)) {
554 $mesg .= $langs->transnoentities('ErrorFailedToSendMail', dol_escape_htmltag($email_from), dol_escape_htmltag($sendto));
555 if (!empty($mailfile->error)) {
556 $mesg .= '<br>'.$mailfile->error;
557 }
558 if (!empty($mailfile->errors) && is_array($mailfile->errors)) {
559 $mesg .= '<br>'.implode('<br>', $mailfile->errors);
560 }
561 } else {
562 $mesg .= $langs->transnoentities('ErrorFailedToSendMail', dol_escape_htmltag($email_from), dol_escape_htmltag($sendto));
563 if (getDolGlobalString('MAIN_DISABLE_ALL_MAILS')) {
564 $mesg .= '<br>Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
565 } else {
566 $mesg .= '<br>Unknown Error, please refer to your administrator';
567 }
568 }
569 $mesg .= '</div>';
570
571 setEventMessages($mesg, null, 'warnings');
572 $action = 'presend';
573 }
574 }
575 } else {
576 $langs->load("errors");
577 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("MailTo")), null, 'warnings');
578 dol_syslog('Try to send email with no recipient defined', LOG_WARNING);
579 $action = 'presend';
580 }
581 } else {
582 $langs->load("errors");
583 setEventMessages($langs->trans('ErrorFailedToReadObject', $object->element), null, 'errors');
584 dol_syslog('Failed to read data of object id='.$object->id.' element='.$object->element);
585 $action = 'presend';
586 }
587}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
global $dolibarr_main_url_root
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 contact/addresses.
Class to manage invoices.
Class to manage a HTML form to send a unitary email Usage: $formail = new FormMail($db) $formmail->pr...
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
global $mysoc
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_add_file_process($upload_dir, $allowoverwrite=0, $updatesessionordb=0, $keyforsourcefile='addedfile', $savingdocmask='', $link=null, $trackid='', $generatethumbs=1, $object=null, $forceFullTextIndexation='', $mode=0)
Get and save an upload file (for example after submitting a new file in a mail form).
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_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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
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...
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
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)
Definition html.lib.php:181
dol_hash($chain, $type='0', $nosalt=0, $mode=0)
Returns a hash (non reversible encryption) of a string.