actions_massactions_mail.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /* Copyright (C) 2015-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018-2021 Nicolas ZABOURI <info@inovea-conseil.com>
  4. * Copyright (C) 2018 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2019 Ferran Marcet <fmarcet@2byte.es>
  6. * Copyright (C) 2019-2021 Frédéric France <frederic.france@netlogic.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. * or see https://www.gnu.org/
  21. */
  22. /**
  23. * \file htdocs/core/actions_massactions.inc.php
  24. * \brief Code for actions done with massaction button (send by email, merge pdf, delete, ...)
  25. */
  26. // $massaction must be defined
  27. // $objectclass and $objectlabel must be defined
  28. // $parameters, $object, $action must be defined for the hook.
  29. // $permissiontoread, $permissiontoadd, $permissiontodelete, $permissiontoclose may be defined
  30. // $uploaddir may be defined (example to $conf->project->dir_output."/";)
  31. // $toselect may be defined
  32. // $diroutputmassaction may be defined
  33. // Protection
  34. if (empty($objectclass) || empty($uploaddir)) {
  35. dol_print_error(null, 'include of actions_massactions.inc.php is done but var $objectclass or $uploaddir was not defined');
  36. exit;
  37. }
  38. // Mass actions. Controls on number of lines checked.
  39. $maxformassaction = (empty($conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS) ? 1000 : $conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS);
  40. if (!empty($massaction) && is_array($toselect) && count($toselect) < 1) {
  41. $error++;
  42. setEventMessages($langs->trans("NoRecordSelected"), null, "warnings");
  43. }
  44. if (!$error && is_array($toselect) && count($toselect) > $maxformassaction) {
  45. setEventMessages($langs->trans('TooManyRecordForMassAction', $maxformassaction), null, 'errors');
  46. $error++;
  47. }
  48. if (!$error && $massaction == 'confirm_presend_attendees' && !GETPOST('sendmail')) { // If we do not choose button send (for example when we change template or limit), we must not send email, but keep on send email form
  49. $massaction = 'presend_attendees';
  50. }
  51. if (!$error && $massaction == 'confirm_presend_attendees') {
  52. $resaction = '';
  53. $nbsent = 0;
  54. $nbignored = 0;
  55. $langs->load("mails");
  56. include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  57. $listofobjectid = array();
  58. $listofobjectref = array();
  59. $oneemailperrecipient = (GETPOST('oneemailperrecipient', 'int') ? 1 : 0);
  60. if (!$error) {
  61. require_once DOL_DOCUMENT_ROOT . '/eventorganization/class/conferenceorboothattendee.class.php';
  62. $attendee = new ConferenceOrBoothAttendee($db);
  63. $listofselectedid = array();
  64. $listofselectedref = array();
  65. $objecttmp = new $objectclass($db);
  66. foreach ($toselect as $toselectid) {
  67. $result = $objecttmp->fetch($toselectid);
  68. if ($result > 0) {
  69. $attendees = $attendee->fetchAll();
  70. if (is_array($attendees) && count($attendees) > 0) {
  71. foreach ($attendees as $attmail) {
  72. if (!empty($attmail->email)) {
  73. $attmail->fetch_thirdparty();
  74. $listofselectedid[$attmail->email] = $attmail;
  75. $listofselectedref[$attmail->email] = $objecttmp;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. // Check mandatory parameters
  83. if (GETPOST('fromtype', 'alpha') === 'user' && empty($user->email)) {
  84. $error++;
  85. setEventMessages($langs->trans("NoSenderEmailDefined"), null, 'warnings');
  86. $massaction = 'presend_attendees';
  87. }
  88. $receiver = GETPOST('receiver', 'alphawithlgt');
  89. if (!is_array($receiver)) {
  90. if (empty($receiver) || $receiver == '-1') {
  91. $receiver = array();
  92. } else {
  93. $receiver = array($receiver);
  94. }
  95. }
  96. if (!trim($_POST['sendto']) && count($receiver) == 0 && count($listofselectedid) == 0) { // if only one recipient, receiver is mandatory
  97. $error++;
  98. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Recipient")), null, 'warnings');
  99. $massaction = 'presend_attendees';
  100. }
  101. if (!GETPOST('subject', 'restricthtml')) {
  102. $error++;
  103. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("MailTopic")), null, 'warnings');
  104. $massaction = 'presend_attendees';
  105. }
  106. if (!$error) {
  107. $objecttmp->fetch_thirdparty();
  108. foreach ($listofselectedid as $email => $attendees) {
  109. $sendto = '';
  110. $sendtocc = '';
  111. $sendtobcc = '';
  112. $sendtoid = array();
  113. // Define $sendto
  114. $sendto = $attendees->thirdparty->name . '<' . trim($attendees->email) . '>';
  115. // Define $sendtocc
  116. $receivercc = GETPOST('receivercc', 'alphawithlgt');
  117. if (!is_array($receivercc)) {
  118. if ($receivercc == '-1') {
  119. $receivercc = array();
  120. } else {
  121. $receivercc = array($receivercc);
  122. }
  123. }
  124. $tmparray = array();
  125. if (trim($_POST['sendtocc'])) {
  126. $tmparray[] = trim(GETPOST('sendtocc', 'alphawithlgt'));
  127. }
  128. $sendtocc = implode(',', $tmparray);
  129. $langs->load("commercial");
  130. $reg = array();
  131. $fromtype = GETPOST('fromtype');
  132. if ($fromtype === 'user') {
  133. $from = $user->getFullName($langs) . ' <' . $user->email . '>';
  134. } elseif ($fromtype === 'company') {
  135. $from = $conf->global->MAIN_INFO_SOCIETE_NOM . ' <' . $conf->global->MAIN_INFO_SOCIETE_MAIL . '>';
  136. } elseif (preg_match('/user_aliases_(\d+)/', $fromtype, $reg)) {
  137. $tmp = explode(',', $user->email_aliases);
  138. $from = trim($tmp[($reg[1] - 1)]);
  139. } elseif (preg_match('/global_aliases_(\d+)/', $fromtype, $reg)) {
  140. $tmp = explode(',', $conf->global->MAIN_INFO_SOCIETE_MAIL_ALIASES);
  141. $from = trim($tmp[($reg[1] - 1)]);
  142. } elseif (preg_match('/senderprofile_(\d+)_(\d+)/', $fromtype, $reg)) {
  143. $sql = "SELECT rowid, label, email FROM " . MAIN_DB_PREFIX . "c_email_senderprofile WHERE rowid = " . (int) $reg[1];
  144. $resql = $db->query($sql);
  145. $obj = $db->fetch_object($resql);
  146. if ($obj) {
  147. $from = dol_string_nospecial($obj->label, ' ', array(",")) . ' <' . $obj->email . '>';
  148. }
  149. } else {
  150. $from = dol_string_nospecial(GETPOST('fromname'), ' ', array(",")) . ' <' . GETPOST('frommail') . '>';
  151. }
  152. $replyto = $from;
  153. $subject = GETPOST('subject', 'restricthtml');
  154. $message = GETPOST('message', 'restricthtml');
  155. $sendtobcc = GETPOST('sendtoccc', 'alphawithlgt');
  156. // $objecttmp is a real object or an empty object if we choose to send one email per thirdparty instead of one per object
  157. // Make substitution in email content
  158. $substitutionarray = getCommonSubstitutionArray($langs, 0, null, $attendees);
  159. if (!empty($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY)) {
  160. $urlwithouturlroot = preg_replace('/' . preg_quote(DOL_URL_ROOT, '/') . '$/i', '', trim($dolibarr_main_url_root));
  161. $urlwithroot = $urlwithouturlroot . DOL_URL_ROOT;
  162. $url_link = $urlwithroot . '/public/agenda/agendaexport.php?format=ical' . ($conf->entity > 1 ? "&entity=" . $conf->entity : "");
  163. $url_link .= '&exportkey=' . ($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY ? urlencode($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY) : '...');
  164. $url_link .= "&project=" . $listofselectedref[$email]->fk_project . '&module=' . urlencode('@eventorganization') . '&status=' . ConferenceOrBooth::STATUS_CONFIRMED;
  165. $html_link = '<a href="' . $url_link . '">' . $langs->trans('DownloadICSLink') . '</a>';
  166. }
  167. $substitutionarray['__EVENTORGANIZATION_ICS_LINK__'] = $html_link;
  168. $substitutionarray['__EVENTORGANIZATION_URL_LINK__'] = $url_link;
  169. $substitutionarray['__CHECK_READ__'] = '<img src="' . DOL_MAIN_URL_ROOT . '/public/emailing/mailing-read.php?tag=' . urlencode($attendees->thirdparty->tag) . '&securitykey=' . urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY) . '" width="1" height="1" style="width:1px;height:1px" border="0"/>';
  170. $parameters = array('mode' => 'formemail');
  171. if (!empty($listofobjectref)) {
  172. $parameters['listofobjectref'] = $listofobjectref;
  173. }
  174. complete_substitutions_array($substitutionarray, $langs, $attendees, $parameters);
  175. $subjectreplaced = make_substitutions($subject, $substitutionarray);
  176. $messagereplaced = make_substitutions($message, $substitutionarray);
  177. if (empty($sendcontext)) {
  178. $sendcontext = 'standard';
  179. }
  180. // Send mail (substitutionarray must be done just before this)
  181. require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
  182. $mailfile = new CMailFile($subjectreplaced, $sendto, $from, $messagereplaced, array(), array(), array(), $sendtocc, $sendtobcc, $deliveryreceipt, -1, '', '', "attendees_".$attendees->id, '', $sendcontext);
  183. if ($mailfile->error) {
  184. $resaction .= '<div class="error">' . $mailfile->error . '</div>';
  185. } else {
  186. $result = $mailfile->sendfile();
  187. if ($result) {
  188. $resaction .= $langs->trans('MailSuccessfulySent', $mailfile->getValidAddress($from, 2), $mailfile->getValidAddress($sendto, 2)) . '<br>'; // Must not contain "
  189. $error = 0;
  190. dol_syslog("Try to insert email event into agenda for objid=" . $attendees->id . " => objectobj=" . get_class($attendees));
  191. $actionmsg = $langs->transnoentities('MailSentBy') . ' ' . $from . ' ' . $langs->transnoentities('To') . ' ' . $sendto;
  192. if ($message) {
  193. if ($sendtocc) {
  194. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc') . ": " . $sendtocc);
  195. }
  196. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic') . ": " . $subjectreplaced);
  197. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody') . ":");
  198. $actionmsg = dol_concatdesc($actionmsg, $messagereplaced);
  199. }
  200. $actionmsg2 = '';
  201. $objectobj2 = $listofselectedref[$email];
  202. // Initialisation donnees
  203. $objectobj2->actionmsg = $actionmsg; // Long text
  204. $objectobj2->actionmsg2 = $actionmsg2; // Short text
  205. $objectobj2->fk_element = $objectobj2->id;
  206. $objectobj2->elementtype = $objectobj2->element;
  207. $triggername = 'CONFERENCEORBOOTHATTENDEE_SENTBYMAIL';
  208. if (!empty($triggername)) {
  209. // Call trigger
  210. $result = $objectobj2->call_trigger($triggername, $user);
  211. if ($result < 0) {
  212. $error++;
  213. }
  214. // End call triggers
  215. if ($error) {
  216. setEventMessages($db->lasterror(), $objectobj2->errors, 'errors');
  217. dol_syslog("Error in trigger " . $triggername . ' ' . $db->lasterror(), LOG_ERR);
  218. }
  219. }
  220. $nbsent++; // Nb of object sent
  221. } else {
  222. $langs->load("other");
  223. if ($mailfile->error) {
  224. $resaction .= $langs->trans('ErrorFailedToSendMail', $from, $sendto);
  225. $resaction .= '<br><div class="error">' . $mailfile->error . '</div>';
  226. } elseif (!empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
  227. $resaction .= '<div class="warning">No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS</div>';
  228. } else {
  229. $resaction .= $langs->trans('ErrorFailedToSendMail', $from, $sendto) . '<br><div class="error">(unhandled error)</div>';
  230. }
  231. }
  232. }
  233. }
  234. }
  235. $resaction .= ($resaction ? '<br>' : $resaction);
  236. $resaction .= '<strong>' . $langs->trans("ResultOfMailSending") . ':</strong><br>' . "\n";
  237. $resaction .= $langs->trans("NbSelected") . ': ' . count($toselect) . "\n<br>";
  238. $resaction .= $langs->trans("NbIgnored") . ': ' . ($nbignored ? $nbignored : 0) . "\n<br>";
  239. $resaction .= $langs->trans("NbSent") . ': ' . ($nbsent ? $nbsent : 0) . "\n<br>";
  240. if ($nbsent) {
  241. $action = ''; // Do not show form post if there was at least one successfull sent
  242. //setEventMessages($langs->trans("EMailSentToNRecipients", $nbsent.'/'.count($toselect)), null, 'mesgs');
  243. setEventMessages($langs->trans("EMailSentForNElements", $nbsent . '/' . count($toselect)), null, 'mesgs');
  244. setEventMessages($resaction, null, 'mesgs');
  245. } else {
  246. //setEventMessages($langs->trans("EMailSentToNRecipients", 0), null, 'warnings'); // May be object has no generated PDF file
  247. setEventMessages($resaction, null, 'warnings');
  248. }
  249. $action = 'list';
  250. $massaction = '';
  251. }
  252. $parameters['toselect'] = $toselect;
  253. $parameters['uploaddir'] = $uploaddir;
  254. $parameters['massaction'] = $massaction;
  255. $parameters['diroutputmassaction'] = isset($diroutputmassaction) ? $diroutputmassaction : null;
  256. $reshook = $hookmanager->executeHooks('doMassActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  257. if ($reshook < 0) {
  258. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  259. }