card.php 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2019 Laurent Destailleur <eldy@uers.sourceforge.net>
  4. * Copyright (C) 2005-2016 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2021 Waël Almoman <info@almoman.com>
  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. */
  20. /**
  21. * \file htdocs/comm/mailing/card.php
  22. * \ingroup mailing
  23. * \brief Fiche mailing, onglet general
  24. */
  25. if (!defined('NOSTYLECHECK')) {
  26. define('NOSTYLECHECK', '1');
  27. }
  28. // Load Dolibarr environment
  29. require '../../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/emailing.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  34. require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  38. // Load translation files required by the page
  39. $langs->loadLangs(array("mails", "admin"));
  40. $id = (GETPOST('mailid', 'int') ? GETPOST('mailid', 'int') : GETPOST('id', 'int'));
  41. $action = GETPOST('action', 'aZ09');
  42. $confirm = GETPOST('confirm', 'alpha');
  43. $cancel = GETPOST('cancel', 'aZ09');
  44. $urlfrom = GETPOST('urlfrom');
  45. $backtopageforcancel = GETPOST('backtopageforcancel');
  46. // Initialize technical objects
  47. $object = new Mailing($db);
  48. $extrafields = new ExtraFields($db);
  49. $hookmanager->initHooks(array('mailingcard', 'globalcard'));
  50. // Fetch optionals attributes and labels
  51. $extrafields->fetch_name_optionals_label($object->table_element);
  52. // Load object
  53. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  54. // Array of possible substitutions (See also file mailing-send.php that should manage same substitutions)
  55. $object->substitutionarray = FormMail::getAvailableSubstitKey('emailing');
  56. // Set $object->substitutionarrayfortest
  57. $signature = ((!empty($user->signature) && !getDolGlobalString('MAIN_MAIL_DO_NOT_USE_SIGN')) ? $user->signature : '');
  58. $targetobject = null; // Not defined with mass emailing
  59. $parameters = array('mode'=>'emailing');
  60. $substitutionarray = FormMail::getAvailableSubstitKey('emailing', $targetobject);
  61. $object->substitutionarrayfortest = $substitutionarray;
  62. // List of sending methods
  63. $listofmethods = array();
  64. //$listofmethods['default'] = $langs->trans('DefaultOutgoingEmailSetup');
  65. $listofmethods['mail'] = 'PHP mail function';
  66. //$listofmethods['simplemail']='Simplemail class';
  67. $listofmethods['smtps'] = 'SMTP/SMTPS socket library';
  68. if (version_compare(phpversion(), '7.0', '>=')) {
  69. $listofmethods['swiftmailer'] = 'Swift Mailer socket library';
  70. }
  71. // Security check
  72. if (!$user->hasRight('mailing', 'lire') || (!getDolGlobalString('EXTERNAL_USERS_ARE_AUTHORIZED') && $user->socid > 0)) {
  73. accessforbidden();
  74. }
  75. if (empty($action) && empty($object->id)) {
  76. accessforbidden('Object not found');
  77. }
  78. $upload_dir = $conf->mailing->dir_output."/".get_exdir($object->id, 2, 0, 1, $object, 'mailing');
  79. /*
  80. * Actions
  81. */
  82. $parameters = array();
  83. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  84. if ($reshook < 0) {
  85. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  86. }
  87. if (empty($reshook)) {
  88. $error = 0;
  89. $backurlforlist = DOL_URL_ROOT.'/comm/mailing/list.php';
  90. if (empty($backtopage) || ($cancel && empty($id))) {
  91. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  92. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  93. $backtopage = $backurlforlist;
  94. } else {
  95. $backtopage = DOL_URL_ROOT.'/comm/mailing/card.php?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
  96. }
  97. }
  98. }
  99. if ($cancel) {
  100. /*var_dump($cancel);var_dump($backtopage);var_dump($backtopageforcancel);exit;*/
  101. if (!empty($backtopageforcancel)) {
  102. header("Location: ".$backtopageforcancel);
  103. exit;
  104. } elseif (!empty($backtopage)) {
  105. header("Location: ".$backtopage);
  106. exit;
  107. }
  108. $action = '';
  109. }
  110. // Action clone object
  111. if ($action == 'confirm_clone' && $confirm == 'yes') {
  112. if (!GETPOST("clone_content", 'alpha') && !GETPOST("clone_receivers", 'alpha')) {
  113. setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
  114. } else {
  115. $result = $object->createFromClone($user, $object->id, GETPOST("clone_content", 'alpha'), GETPOST("clone_receivers", 'alpha'));
  116. if ($result > 0) {
  117. header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
  118. exit;
  119. } else {
  120. setEventMessages($object->error, $object->errors, 'errors');
  121. }
  122. }
  123. $action = '';
  124. }
  125. // Action send emailing for everybody
  126. if ($action == 'sendallconfirmed' && $confirm == 'yes') {
  127. if (!getDolGlobalString('MAILING_LIMIT_SENDBYWEB')) {
  128. // As security measure, we don't allow send from the GUI
  129. setEventMessages($langs->trans("MailingNeedCommand"), null, 'warnings');
  130. setEventMessages('<textarea cols="70" rows="'.ROWS_2.'" wrap="soft">php ./scripts/emailings/mailing-send.php '.$object->id.'</textarea>', null, 'warnings');
  131. setEventMessages($langs->trans("MailingNeedCommand2"), null, 'warnings');
  132. $action = '';
  133. } elseif (getDolGlobalInt('MAILING_LIMIT_SENDBYWEB') < 0) {
  134. setEventMessages($langs->trans("NotEnoughPermissions"), null, 'warnings');
  135. $action = '';
  136. } else {
  137. if ($object->statut == 0) {
  138. dol_print_error('', 'ErrorMailIsNotValidated');
  139. exit;
  140. }
  141. $id = $object->id;
  142. $subject = $object->sujet;
  143. $message = $object->body;
  144. $from = $object->email_from;
  145. $replyto = $object->email_replyto;
  146. $errorsto = $object->email_errorsto;
  147. // Is the message in html
  148. $msgishtml = -1; // Unknown by default
  149. if (preg_match('/[\s\t]*<html>/i', $message)) {
  150. $msgishtml = 1;
  151. }
  152. // Warning, we must not use begin-commit transaction here
  153. // because we want to save update for each mail sent.
  154. $nbok = 0;
  155. $nbko = 0;
  156. // We choose mails not already sent for this mailing (statut=0)
  157. // or sent in error (statut=-1)
  158. $sql = "SELECT mc.rowid, mc.fk_mailing, mc.lastname, mc.firstname, mc.email, mc.other, mc.source_url, mc.source_id, mc.source_type, mc.tag";
  159. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
  160. $sql .= " WHERE mc.statut < 1 AND mc.fk_mailing = ".((int) $object->id);
  161. $sql .= " ORDER BY mc.statut DESC"; // first status 0, then status -1
  162. dol_syslog("card.php: select targets", LOG_DEBUG);
  163. $resql = $db->query($sql);
  164. if ($resql) {
  165. $num = $db->num_rows($resql); // Number of possible recipients
  166. if ($num) {
  167. dol_syslog("comm/mailing/card.php: nb of targets = ".$num, LOG_DEBUG);
  168. $now = dol_now();
  169. // Positioning date of start sending
  170. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing SET date_envoi='".$db->idate($now)."' WHERE rowid=".((int) $object->id);
  171. $resql2 = $db->query($sql);
  172. if (!$resql2) {
  173. dol_print_error($db);
  174. }
  175. $thirdpartystatic = new Societe($db);
  176. // Loop on each email and send it
  177. $iforemailloop = 0;
  178. while ($iforemailloop < $num && $iforemailloop < $conf->global->MAILING_LIMIT_SENDBYWEB) {
  179. // Here code is common with same loop ino mailing-send.php
  180. $res = 1;
  181. $now = dol_now();
  182. $obj = $db->fetch_object($resql);
  183. // sendto en RFC2822
  184. $sendto = str_replace(',', ' ', dolGetFirstLastname($obj->firstname, $obj->lastname))." <".$obj->email.">";
  185. // Make substitutions on topic and body. From (AA=YY;BB=CC;...) we keep YY, CC, ...
  186. $other = explode(';', $obj->other);
  187. $tmpfield = explode('=', $other[0], 2);
  188. $other1 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
  189. $tmpfield = explode('=', $other[1], 2);
  190. $other2 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
  191. $tmpfield = explode('=', $other[2], 2);
  192. $other3 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
  193. $tmpfield = explode('=', $other[3], 2);
  194. $other4 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
  195. $tmpfield = explode('=', $other[4], 2);
  196. $other5 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
  197. $signature = ((!empty($user->signature) && !getDolGlobalString('MAIN_MAIL_DO_NOT_USE_SIGN')) ? $user->signature : '');
  198. $parameters = array('mode'=>'emailing');
  199. $substitutionarray = getCommonSubstitutionArray($langs, 0, array('object', 'objectamount'), $targetobject); // Note: On mass emailing, this is null because be don't know object
  200. // Array of possible substitutions (See also file mailing-send.php that should manage same substitutions)
  201. $substitutionarray['__ID__'] = $obj->source_id;
  202. if ($obj->source_type == "thirdparty") {
  203. $result = $thirdpartystatic->fetch($obj->source_id);
  204. if ($result > 0) {
  205. $substitutionarray['__THIRDPARTY_CUSTOMER_CODE__'] = $thirdpartystatic->code_client;
  206. } else {
  207. $substitutionarray['__THIRDPARTY_CUSTOMER_CODE__'] = '';
  208. }
  209. }
  210. $substitutionarray['__EMAIL__'] = $obj->email;
  211. $substitutionarray['__LASTNAME__'] = $obj->lastname;
  212. $substitutionarray['__FIRSTNAME__'] = $obj->firstname;
  213. $substitutionarray['__MAILTOEMAIL__'] = '<a href="mailto:'.$obj->email.'">'.$obj->email.'</a>';
  214. $substitutionarray['__OTHER1__'] = $other1;
  215. $substitutionarray['__OTHER2__'] = $other2;
  216. $substitutionarray['__OTHER3__'] = $other3;
  217. $substitutionarray['__OTHER4__'] = $other4;
  218. $substitutionarray['__OTHER5__'] = $other5;
  219. $substitutionarray['__USER_SIGNATURE__'] = $signature; // Signature is empty when ran from command line or taken from user in parameter)
  220. $substitutionarray['__SENDEREMAIL_SIGNATURE__'] = $signature; // Signature is empty when ran from command line or taken from user in parameter)
  221. $substitutionarray['__CHECK_READ__'] = '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag='.urlencode($obj->tag).'&securitykey='.dol_hash(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY').'-'.$obj->tag.'-'.$obj->email.'-'.$obj->rowid, "md5").'&email='.urlencode($obj->email).'&mtid='.((int) $obj->rowid).'" width="1" height="1" style="width:1px;height:1px" border="0"/>';
  222. $substitutionarray['__UNSUBSCRIBE__'] = '<a href="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-unsubscribe.php?tag='.urlencode($obj->tag).'&unsuscrib=1&securitykey='.dol_hash(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY').'-'.$obj->tag.'-'.$obj->email.'-'.$obj->rowid, "md5").'&email='.urlencode($obj->email).'&mtid='.((int) $obj->rowid).'" target="_blank" rel="noopener noreferrer">'.$langs->trans("MailUnsubcribe").'</a>';
  223. $substitutionarray['__UNSUBSCRIBE_URL__'] = DOL_MAIN_URL_ROOT.'/public/emailing/mailing-unsubscribe.php?tag='.urlencode($obj->tag).'&unsuscrib=1&securitykey='.dol_hash(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY').'-'.$obj->tag.'-'.$obj->email.'-'.$obj->rowid, "md5").'&email='.urlencode($obj->email).'&mtid='.((int) $obj->rowid);
  224. $onlinepaymentenabled = 0;
  225. if (isModEnabled('paypal')) {
  226. $onlinepaymentenabled++;
  227. }
  228. if (isModEnabled('paybox')) {
  229. $onlinepaymentenabled++;
  230. }
  231. if (isModEnabled('stripe')) {
  232. $onlinepaymentenabled++;
  233. }
  234. if ($onlinepaymentenabled && getDolGlobalString('PAYMENT_SECURITY_TOKEN')) {
  235. require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
  236. $substitutionarray['__ONLINEPAYMENTLINK_MEMBER__'] = getHtmlOnlinePaymentLink('member', $obj->source_id);
  237. $substitutionarray['__ONLINEPAYMENTLINK_DONATION__'] = getHtmlOnlinePaymentLink('donation', $obj->source_id);
  238. $substitutionarray['__ONLINEPAYMENTLINK_ORDER__'] = getHtmlOnlinePaymentLink('order', $obj->source_id);
  239. $substitutionarray['__ONLINEPAYMENTLINK_INVOICE__'] = getHtmlOnlinePaymentLink('invoice', $obj->source_id);
  240. $substitutionarray['__ONLINEPAYMENTLINK_CONTRACTLINE__'] = getHtmlOnlinePaymentLink('contractline', $obj->source_id);
  241. $substitutionarray['__SECUREKEYPAYMENT__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN'), 2);
  242. if (!getDolGlobalString('PAYMENT_SECURITY_TOKEN_UNIQUE')) {
  243. $substitutionarray['__SECUREKEYPAYMENT_MEMBER__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN'), 2);
  244. $substitutionarray['__SECUREKEYPAYMENT_DONATION__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN'), 2);
  245. $substitutionarray['__SECUREKEYPAYMENT_ORDER__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN'), 2);
  246. $substitutionarray['__SECUREKEYPAYMENT_INVOICE__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN'), 2);
  247. $substitutionarray['__SECUREKEYPAYMENT_CONTRACTLINE__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN'), 2);
  248. } else {
  249. $substitutionarray['__SECUREKEYPAYMENT_MEMBER__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . 'member'.$obj->source_id, 2);
  250. $substitutionarray['__SECUREKEYPAYMENT_DONATION__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . 'donation'.$obj->source_id, 2);
  251. $substitutionarray['__SECUREKEYPAYMENT_ORDER__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . 'order'.$obj->source_id, 2);
  252. $substitutionarray['__SECUREKEYPAYMENT_INVOICE__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . 'invoice'.$obj->source_id, 2);
  253. $substitutionarray['__SECUREKEYPAYMENT_CONTRACTLINE__'] = dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . 'contractline'.$obj->source_id, 2);
  254. }
  255. }
  256. if (getDolGlobalString('MEMBER_ENABLE_PUBLIC')) {
  257. $substitutionarray['__PUBLICLINK_NEWMEMBERFORM__'] = '<a target="_blank" rel="noopener noreferrer" href="'.DOL_MAIN_URL_ROOT.'/public/members/new.php'.((isModEnabled('multicompany')) ? '?entity='.$conf->entity : '').'">'.$langs->trans('BlankSubscriptionForm'). '</a>';
  258. }
  259. /* For backward compatibility, deprecated */
  260. if (isModEnabled('paypal') && getDolGlobalString('PAYPAL_SECURITY_TOKEN')) {
  261. $substitutionarray['__SECUREKEYPAYPAL__'] = dol_hash(getDolGlobalString('PAYPAL_SECURITY_TOKEN'), 2);
  262. if (!getDolGlobalString('PAYPAL_SECURITY_TOKEN_UNIQUE')) {
  263. $substitutionarray['__SECUREKEYPAYPAL_MEMBER__'] = dol_hash(getDolGlobalString('PAYPAL_SECURITY_TOKEN'), 2);
  264. } else {
  265. $substitutionarray['__SECUREKEYPAYPAL_MEMBER__'] = dol_hash(getDolGlobalString('PAYPAL_SECURITY_TOKEN') . 'membersubscription'.$obj->source_id, 2);
  266. }
  267. if (!getDolGlobalString('PAYPAL_SECURITY_TOKEN_UNIQUE')) {
  268. $substitutionarray['__SECUREKEYPAYPAL_ORDER__'] = dol_hash(getDolGlobalString('PAYPAL_SECURITY_TOKEN'), 2);
  269. } else {
  270. $substitutionarray['__SECUREKEYPAYPAL_ORDER__'] = dol_hash(getDolGlobalString('PAYPAL_SECURITY_TOKEN') . 'order'.$obj->source_id, 2);
  271. }
  272. if (!getDolGlobalString('PAYPAL_SECURITY_TOKEN_UNIQUE')) {
  273. $substitutionarray['__SECUREKEYPAYPAL_INVOICE__'] = dol_hash(getDolGlobalString('PAYPAL_SECURITY_TOKEN'), 2);
  274. } else {
  275. $substitutionarray['__SECUREKEYPAYPAL_INVOICE__'] = dol_hash(getDolGlobalString('PAYPAL_SECURITY_TOKEN') . 'invoice'.$obj->source_id, 2);
  276. }
  277. if (!getDolGlobalString('PAYPAL_SECURITY_TOKEN_UNIQUE')) {
  278. $substitutionarray['__SECUREKEYPAYPAL_CONTRACTLINE__'] = dol_hash(getDolGlobalString('PAYPAL_SECURITY_TOKEN'), 2);
  279. } else {
  280. $substitutionarray['__SECUREKEYPAYPAL_CONTRACTLINE__'] = dol_hash(getDolGlobalString('PAYPAL_SECURITY_TOKEN') . 'contractline'.$obj->source_id, 2);
  281. }
  282. }
  283. //$substitutionisok=true;
  284. complete_substitutions_array($substitutionarray, $langs);
  285. $newsubject = make_substitutions($subject, $substitutionarray);
  286. $newmessage = make_substitutions($message, $substitutionarray, null, 0);
  287. $moreinheader = '';
  288. if (preg_match('/__UNSUBSCRIBE_(_|URL_)/', $message)) {
  289. $moreinheader = "List-Unsubscribe: <__UNSUBSCRIBE_URL__>\n";
  290. $moreinheader = make_substitutions($moreinheader, $substitutionarray);
  291. }
  292. $arr_file = array();
  293. $arr_mime = array();
  294. $arr_name = array();
  295. $arr_css = array();
  296. $listofpaths = dol_dir_list($upload_dir, 'all', 0, '', '', 'name', SORT_ASC, 0);
  297. if (count($listofpaths)) {
  298. foreach ($listofpaths as $key => $val) {
  299. $arr_file[] = $listofpaths[$key]['fullname'];
  300. $arr_mime[] = dol_mimetype($listofpaths[$key]['name']);
  301. $arr_name[] = $listofpaths[$key]['name'];
  302. }
  303. }
  304. // Mail making
  305. $trackid = 'emailing-'.$obj->fk_mailing.'-'.$obj->rowid;
  306. $upload_dir_tmp = $upload_dir;
  307. $mail = new CMailFile($newsubject, $sendto, $from, $newmessage, $arr_file, $arr_mime, $arr_name, '', '', 0, $msgishtml, $errorsto, $arr_css, $trackid, $moreinheader, 'emailing', '', $upload_dir_tmp);
  308. if ($mail->error) {
  309. $res = 0;
  310. }
  311. /*if (! $substitutionisok)
  312. {
  313. $mail->error='Some substitution failed';
  314. $res=0;
  315. }*/
  316. // Send mail
  317. if ($res) {
  318. $res = $mail->sendfile();
  319. }
  320. if ($res) {
  321. // Mail successful
  322. $nbok++;
  323. dol_syslog("comm/mailing/card.php: ok for #".$iforemailloop.($mail->error ? ' - '.$mail->error : ''), LOG_DEBUG);
  324. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  325. $sql .= " SET statut=1, date_envoi = '".$db->idate($now)."' WHERE rowid=".((int) $obj->rowid);
  326. $resql2 = $db->query($sql);
  327. if (!$resql2) {
  328. dol_print_error($db);
  329. } else {
  330. //if check read is use then update prospect contact status
  331. if (strpos($message, '__CHECK_READ__') !== false) {
  332. //Update status communication of thirdparty prospect
  333. $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET fk_stcomm=2 WHERE rowid IN (SELECT source_id FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE rowid=".((int) $obj->rowid).")";
  334. dol_syslog("card.php: set prospect thirdparty status", LOG_DEBUG);
  335. $resql2 = $db->query($sql);
  336. if (!$resql2) {
  337. dol_print_error($db);
  338. }
  339. //Update status communication of contact prospect
  340. $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET fk_stcomm=2 WHERE rowid IN (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."socpeople AS sc INNER JOIN ".MAIN_DB_PREFIX."mailing_cibles AS mc ON mc.rowid=".((int) $obj->rowid)." AND mc.source_type = 'contact' AND mc.source_id = sc.rowid)";
  341. dol_syslog("card.php: set prospect contact status", LOG_DEBUG);
  342. $resql2 = $db->query($sql);
  343. if (!$resql2) {
  344. dol_print_error($db);
  345. }
  346. }
  347. }
  348. if (getDolGlobalString('MAILING_DELAY')) {
  349. dol_syslog("Wait a delay of MAILING_DELAY=".((float) $conf->global->MAILING_DELAY));
  350. usleep((float) $conf->global->MAILING_DELAY * 1000000);
  351. }
  352. //test if CHECK READ change statut prospect contact
  353. } else {
  354. // Mail failed
  355. $nbko++;
  356. dol_syslog("comm/mailing/card.php: error for #".$iforemailloop.($mail->error ? ' - '.$mail->error : ''), LOG_WARNING);
  357. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  358. $sql .= " SET statut=-1, error_text='".$db->escape($mail->error)."', date_envoi='".$db->idate($now)."' WHERE rowid=".((int) $obj->rowid);
  359. $resql2 = $db->query($sql);
  360. if (!$resql2) {
  361. dol_print_error($db);
  362. }
  363. }
  364. $iforemailloop++;
  365. }
  366. } else {
  367. setEventMessages($langs->transnoentitiesnoconv("NoMoreRecipientToSendTo"), null, 'mesgs');
  368. }
  369. // Loop finished, set global statut of mail
  370. if ($nbko > 0) {
  371. $statut = 2; // Status 'sent partially' (because at least one error)
  372. if ($nbok > 0) {
  373. setEventMessages($langs->transnoentitiesnoconv("EMailSentToNRecipients", $nbok), null, 'mesgs');
  374. } else {
  375. setEventMessages($langs->transnoentitiesnoconv("EMailSentToNRecipients", $nbok), null, 'mesgs');
  376. }
  377. } else {
  378. if ($nbok >= $num) {
  379. $statut = 3; // Send to everybody
  380. setEventMessages($langs->transnoentitiesnoconv("EMailSentToNRecipients", $nbok), null, 'mesgs');
  381. } else {
  382. $statut = 2; // Status 'sent partially' (because not send to everybody)
  383. setEventMessages($langs->transnoentitiesnoconv("EMailSentToNRecipients", $nbok), null, 'mesgs');
  384. }
  385. }
  386. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing SET statut=".((int) $statut)." WHERE rowid = ".((int) $object->id);
  387. dol_syslog("comm/mailing/card.php: update global status", LOG_DEBUG);
  388. $resql2 = $db->query($sql);
  389. if (!$resql2) {
  390. dol_print_error($db);
  391. }
  392. } else {
  393. dol_syslog($db->error());
  394. dol_print_error($db);
  395. }
  396. $object->fetch($id);
  397. $action = '';
  398. }
  399. }
  400. // Action send test emailing
  401. if ($action == 'send' && ! $cancel) {
  402. $error = 0;
  403. $upload_dir = $conf->mailing->dir_output."/".get_exdir($object->id, 2, 0, 1, $object, 'mailing');
  404. $object->sendto = GETPOST("sendto", 'alphawithlgt');
  405. if (!$object->sendto) {
  406. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("MailTo")), null, 'errors');
  407. $error++;
  408. }
  409. if (!$error) {
  410. // Is the message in html
  411. $msgishtml = -1; // Unknow = autodetect by default
  412. if (preg_match('/[\s\t]*<html>/i', $object->body)) {
  413. $msgishtml = 1;
  414. }
  415. $signature = ((!empty($user->signature) && !getDolGlobalString('MAIN_MAIL_DO_NOT_USE_SIGN')) ? $user->signature : '');
  416. $parameters = array('mode'=>'emailing');
  417. $substitutionarray = getCommonSubstitutionArray($langs, 0, array('object', 'objectamount'), $targetobject); // Note: On mass emailing, this is null because be don't know object
  418. // other are set at begin of page
  419. $substitutionarray['__EMAIL__'] = $object->sendto;
  420. $substitutionarray['__MAILTOEMAIL__'] = '<a href="mailto:'.$object->sendto.'">'.$object->sendto.'</a>';
  421. $substitutionarray['__CHECK_READ__'] = '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag=undefinedintestmode&securitykey='.dol_hash(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY')."-undefinedintestmode-".$obj->sendto."-0", 'md5').'&email='.urlencode($obj->sendto).'&mtid=0" width="1" height="1" style="width:1px;height:1px" border="0"/>';
  422. $substitutionarray['__UNSUBSCRIBE__'] = '<a href="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-unsubscribe.php?tag=undefinedintestmode&unsuscrib=1&securitykey='.dol_hash(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY')."-undefinedintestmode-".$obj->sendto."-0", 'md5').'&email='.urlencode($obj->sendto).'&mtid=0" target="_blank" rel="noopener noreferrer">'.$langs->trans("MailUnsubcribe").'</a>';
  423. $substitutionarray['__UNSUBSCRIBE_URL__'] = DOL_MAIN_URL_ROOT.'/public/emailing/mailing-unsubscribe.php?tag=undefinedintestmode&unsuscrib=1&securitykey='.dol_hash(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY')."-undefinedintestmode-".$obj->sendto."-0", 'md5').'&email='.urlencode($obj->sendto).'&mtid=0';
  424. // Subject and message substitutions
  425. complete_substitutions_array($substitutionarray, $langs, $targetobject);
  426. $tmpsujet = make_substitutions($object->sujet, $substitutionarray);
  427. $tmpbody = make_substitutions($object->body, $substitutionarray);
  428. $arr_file = array();
  429. $arr_mime = array();
  430. $arr_name = array();
  431. $arr_css = array();
  432. // Add CSS
  433. if (!empty($object->bgcolor)) {
  434. $arr_css['bgcolor'] = (preg_match('/^#/', $object->bgcolor) ? '' : '#').$object->bgcolor;
  435. }
  436. if (!empty($object->bgimage)) {
  437. $arr_css['bgimage'] = $object->bgimage;
  438. }
  439. // Attached files
  440. $listofpaths = dol_dir_list($upload_dir, 'all', 0, '', '', 'name', SORT_ASC, 0);
  441. if (count($listofpaths)) {
  442. foreach ($listofpaths as $key => $val) {
  443. $arr_file[] = $listofpaths[$key]['fullname'];
  444. $arr_mime[] = dol_mimetype($listofpaths[$key]['name']);
  445. $arr_name[] = $listofpaths[$key]['name'];
  446. }
  447. }
  448. $trackid = 'emailing-test';
  449. $upload_dir_tmp = $upload_dir;
  450. $mailfile = new CMailFile($tmpsujet, $object->sendto, $object->email_from, $tmpbody, $arr_file, $arr_mime, $arr_name, '', '', 0, $msgishtml, $object->email_errorsto, $arr_css, $trackid, '', 'emailing', '', $upload_dir_tmp);
  451. $result = $mailfile->sendfile();
  452. if ($result) {
  453. setEventMessages($langs->trans("MailSuccessfulySent", $mailfile->getValidAddress($object->email_from, 2), $mailfile->getValidAddress($object->sendto, 2)), null, 'mesgs');
  454. $action = '';
  455. } else {
  456. setEventMessages($langs->trans("ResultKo").'<br>'.$mailfile->error.' '.$result, null, 'errors');
  457. $action = 'test';
  458. }
  459. }
  460. }
  461. // Action add emailing
  462. if ($action == 'add') {
  463. $mesgs = array();
  464. $object->email_from = (string) GETPOST("from", 'alphawithlgt'); // Must allow 'name <email>'
  465. $object->email_replyto = (string) GETPOST("replyto", 'alphawithlgt'); // Must allow 'name <email>'
  466. $object->email_errorsto = (string) GETPOST("errorsto", 'alphawithlgt'); // Must allow 'name <email>'
  467. $object->title = (string) GETPOST("title");
  468. $object->sujet = (string) GETPOST("sujet");
  469. $object->body = (string) GETPOST("bodyemail", 'restricthtml');
  470. $object->bgcolor = preg_replace('/^#/', '', (string) GETPOST("bgcolor"));
  471. $object->bgimage = (string) GETPOST("bgimage");
  472. if (!$object->title) {
  473. $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailTitle"));
  474. }
  475. if (!$object->sujet) {
  476. $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailTopic"));
  477. }
  478. if (!$object->body) {
  479. $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailMessage"));
  480. }
  481. if (!count($mesgs)) {
  482. if ($object->create($user) >= 0) {
  483. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  484. exit;
  485. }
  486. $mesgs[] = $object->error;
  487. $mesgs = array_merge($mesgs, $object->errors);
  488. }
  489. setEventMessages('', $mesgs, 'errors');
  490. $action = "create";
  491. }
  492. // Action update description of emailing
  493. if ($action == 'settitle' || $action == 'setemail_from' || $action == 'setreplyto' || $action == 'setemail_errorsto' || $action == 'setevenunsubscribe') {
  494. $upload_dir = $conf->mailing->dir_output."/".get_exdir($object->id, 2, 0, 1, $object, 'mailing');
  495. if ($action == 'settitle') {
  496. $object->title = trim(GETPOST('title', 'alpha'));
  497. } elseif ($action == 'setemail_from') {
  498. $object->email_from = trim(GETPOST('email_from', 'alphawithlgt')); // Must allow 'name <email>'
  499. } elseif ($action == 'setemail_replyto') {
  500. $object->email_replyto = trim(GETPOST('email_replyto', 'alphawithlgt')); // Must allow 'name <email>'
  501. } elseif ($action == 'setemail_errorsto') {
  502. $object->email_errorsto = trim(GETPOST('email_errorsto', 'alphawithlgt')); // Must allow 'name <email>'
  503. } elseif ($action == 'settitle' && empty($object->title)) {
  504. $mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailTitle"));
  505. } elseif ($action == 'setfrom' && empty($object->email_from)) {
  506. $mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailFrom"));
  507. } elseif ($action == 'setevenunsubscribe') {
  508. $object->evenunsubscribe = (GETPOST('evenunsubscribe') ? 1 : 0);
  509. }
  510. if (!$mesg) {
  511. $result = $object->update($user);
  512. if ($result >= 0) {
  513. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  514. exit;
  515. }
  516. $mesg = $object->error;
  517. }
  518. setEventMessages($mesg, $mesgs, 'errors');
  519. $action = "";
  520. }
  521. /*
  522. * Action of adding a file in email form
  523. */
  524. if (GETPOST('addfile')) {
  525. $upload_dir = $conf->mailing->dir_output."/".get_exdir($object->id, 2, 0, 1, $object, 'mailing');
  526. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  527. // Set tmp user directory
  528. dol_add_file_process($upload_dir, 0, 0, 'addedfile', '', null, '', 0);
  529. $action = "edit";
  530. }
  531. // Action of file remove
  532. if (GETPOST("removedfile")) {
  533. $upload_dir = $conf->mailing->dir_output."/".get_exdir($object->id, 2, 0, 1, $object, 'mailing');
  534. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  535. dol_remove_file_process(GETPOST('removedfile'), 0, 0); // We really delete file linked to mailing
  536. $action = "edit";
  537. }
  538. // Action of emailing update
  539. if ($action == 'update' && !GETPOST("removedfile") && !$cancel) {
  540. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  541. $isupload = 0;
  542. if (!$isupload) {
  543. $mesgs = array();
  544. $object->sujet = (string) GETPOST("sujet");
  545. $object->body = (string) GETPOST("bodyemail", 'restricthtml');
  546. $object->bgcolor = preg_replace('/^#/', '', (string) GETPOST("bgcolor"));
  547. $object->bgimage = (string) GETPOST("bgimage");
  548. if (!$object->sujet) {
  549. $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailTopic"));
  550. }
  551. if (!$object->body) {
  552. $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailMessage"));
  553. }
  554. if (!count($mesgs)) {
  555. if ($object->update($user) >= 0) {
  556. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  557. exit;
  558. }
  559. $mesgs[] = $object->error;
  560. $mesgs = array_merge($mesgs, $object->errors);
  561. }
  562. setEventMessages('', $mesgs, 'errors');
  563. $action = "edit";
  564. } else {
  565. $action = "edit";
  566. }
  567. }
  568. // Action of validation confirmation
  569. if ($action == 'confirm_valid' && $confirm == 'yes') {
  570. if ($object->id > 0) {
  571. $object->valid($user);
  572. setEventMessages($langs->trans("MailingSuccessfullyValidated"), null, 'mesgs');
  573. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  574. exit;
  575. } else {
  576. dol_print_error($db);
  577. }
  578. }
  579. // Action of validation confirmation
  580. if ($action == 'confirm_settodraft' && $confirm == 'yes') {
  581. if ($object->id > 0) {
  582. $result = $object->setStatut(0);
  583. if ($result > 0) {
  584. //setEventMessages($langs->trans("MailingSuccessfullyValidated"), null, 'mesgs');
  585. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  586. exit;
  587. } else {
  588. setEventMessages($object->error, $object->errors, 'errors');
  589. }
  590. } else {
  591. dol_print_error($db);
  592. }
  593. }
  594. // Resend
  595. if ($action == 'confirm_reset' && $confirm == 'yes') {
  596. if ($object->id > 0) {
  597. $db->begin();
  598. $result = $object->valid($user);
  599. if ($result > 0) {
  600. $result = $object->reset_targets_status($user);
  601. }
  602. if ($result > 0) {
  603. $db->commit();
  604. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  605. exit;
  606. } else {
  607. setEventMessages($object->error, $object->errors, 'errors');
  608. $db->rollback();
  609. }
  610. } else {
  611. dol_print_error($db);
  612. }
  613. }
  614. // Action of delete confirmation
  615. if ($action == 'confirm_delete' && $confirm == 'yes') {
  616. if ($object->delete($user)) {
  617. $url = (!empty($urlfrom) ? $urlfrom : 'list.php');
  618. header("Location: ".$url);
  619. exit;
  620. }
  621. }
  622. if ($cancel) {
  623. $action = '';
  624. }
  625. }
  626. /*
  627. * View
  628. */
  629. $form = new Form($db);
  630. $htmlother = new FormOther($db);
  631. $help_url = 'EN:Module_EMailing|FR:Module_Mailing|ES:M&oacute;dulo_Mailing';
  632. llxHeader(
  633. '',
  634. $langs->trans("Mailing"),
  635. $help_url,
  636. '',
  637. 0,
  638. 0,
  639. array(
  640. '/includes/ace/src/ace.js',
  641. '/includes/ace/src/ext-statusbar.js',
  642. '/includes/ace/src/ext-language_tools.js',
  643. //'/includes/ace/src/ext-chromevox.js'
  644. ),
  645. array()
  646. );
  647. if ($action == 'create') {
  648. // EMailing in creation mode
  649. print '<form name="new_mailing" action="'.$_SERVER['PHP_SELF'].'" method="POST">'."\n";
  650. print '<input type="hidden" name="token" value="'.newToken().'">';
  651. print '<input type="hidden" name="action" value="add">';
  652. $htmltext = '<i>'.$langs->trans("FollowingConstantsWillBeSubstituted").':<br><br><span class="small">';
  653. foreach ($object->substitutionarray as $key => $val) {
  654. $htmltext .= $key.' = '.$langs->trans($val).'<br>';
  655. }
  656. $htmltext .= '</span></i>';
  657. $availablelink = $form->textwithpicto('<span class="opacitymedium">'.$langs->trans("AvailableVariables").'</span>', $htmltext, 1, 'help', '', 0, 2, 'availvar');
  658. //print '<a href="javascript:document_preview(\''.DOL_URL_ROOT.'/admin/modulehelp.php?id='.$objMod->numero.'\',\'text/html\',\''.dol_escape_js($langs->trans("Module")).'\')">'.img_picto($langs->trans("ClickToShowDescription"), $imginfo).'</a>';
  659. // Print mail form
  660. print load_fiche_titre($langs->trans("NewMailing"), $availablelink, 'object_email');
  661. print dol_get_fiche_head(array(), '', '', -3);
  662. print '<table class="border centpercent">';
  663. print '<tr><td class="fieldrequired titlefieldcreate">'.$langs->trans("MailTitle").'</td><td><input class="flat minwidth300" name="title" value="'.dol_escape_htmltag(GETPOST('title')).'" autofocus="autofocus"></td></tr>';
  664. print '<tr><td class="fieldrequired">'.$langs->trans("MailFrom").'</td><td><input class="flat minwidth200" name="from" value="'.getDolGlobalString('MAILING_EMAIL_FROM').'"></td></tr>';
  665. print '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td><input class="flat minwidth200" name="errorsto" value="'.getDolGlobalString('MAILING_EMAIL_ERRORSTO', getDolGlobalString('MAIN_MAIL_ERRORS_TO')).'"></td></tr>';
  666. // Other attributes
  667. $parameters = array();
  668. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  669. print $hookmanager->resPrint;
  670. if (empty($reshook)) {
  671. print $object->showOptionals($extrafields, 'create');
  672. }
  673. print '</table>';
  674. print '<br><br>';
  675. print '<table class="border centpercent">';
  676. print '<tr><td class="fieldrequired titlefieldcreate">'.$langs->trans("MailTopic").'</td><td><input class="flat minwidth200 quatrevingtpercent" name="sujet" value="'.dol_escape_htmltag(GETPOST('sujet', 'alphanohtml')).'"></td></tr>';
  677. print '<tr><td>'.$langs->trans("BackgroundColorByDefault").'</td><td colspan="3">';
  678. print $htmlother->selectColor(GETPOST('bgcolor'), 'bgcolor', '', 0);
  679. print '</td></tr>';
  680. print '</table>';
  681. print '<div style="padding-top: 10px">';
  682. // wysiwyg editor
  683. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  684. $doleditor = new DolEditor('bodyemail', GETPOST('bodyemail', 'restricthtmlallowunvalid'), '', 600, 'dolibarr_mailings', '', true, true, getDolGlobalInt('FCKEDITOR_ENABLE_MAILING'), 20, '90%');
  685. $doleditor->Create();
  686. print '</div>';
  687. print dol_get_fiche_end();
  688. print $form->buttonsSaveCancel("CreateMailing", 'Cancel');
  689. print '</form>';
  690. } else {
  691. if ($object->id > 0) {
  692. $upload_dir = $conf->mailing->dir_output."/".get_exdir($object->id, 2, 0, 1, $object, 'mailing');
  693. $head = emailing_prepare_head($object);
  694. if ($action == 'settodraft') {
  695. // Confirmation back to draft
  696. print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$object->id, $langs->trans("SetToDraft"), $langs->trans("ConfirmUnvalidateEmailing"), "confirm_settodraft", '', '', 1);
  697. } elseif ($action == 'valid') {
  698. // Confirmation of mailing validation
  699. print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$object->id, $langs->trans("ValidMailing"), $langs->trans("ConfirmValidMailing"), "confirm_valid", '', '', 1);
  700. } elseif ($action == 'reset') {
  701. // Confirm reset
  702. print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$object->id, $langs->trans("ResetMailing"), $langs->trans("ConfirmResetMailing", $object->ref), "confirm_reset", '', '', 2);
  703. } elseif ($action == 'delete') {
  704. // Confirm delete
  705. print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$object->id.(!empty($urlfrom) ? '&urlfrom='.urlencode($urlfrom) : ''), $langs->trans("DeleteAMailing"), $langs->trans("ConfirmDeleteMailing"), "confirm_delete", '', '', 1);
  706. }
  707. if ($action != 'edit' && $action != 'edithtml') {
  708. print dol_get_fiche_head($head, 'card', $langs->trans("Mailing"), -1, 'email');
  709. /*
  710. * View mode mailing
  711. */
  712. if ($action == 'sendall') {
  713. // Define message to recommand from command line
  714. $sendingmode = $conf->global->EMAILING_MAIL_SENDMODE;
  715. if (empty($sendingmode)) {
  716. $sendingmode = $conf->global->MAIN_MAIL_SENDMODE;
  717. }
  718. if (empty($sendingmode)) {
  719. $sendingmode = 'mail'; // If not defined, we use php mail function
  720. }
  721. // MAILING_NO_USING_PHPMAIL may be defined or not.
  722. // MAILING_LIMIT_SENDBYWEB is always defined to something != 0 (-1=forbidden).
  723. // MAILING_LIMIT_SENDBYCLI may be defined ot not (-1=forbidden, 0 or undefined=no limit).
  724. // MAILING_LIMIT_SENDBYDAY may be defined ot not (0 or undefined=no limit).
  725. if (getDolGlobalString('MAILING_NO_USING_PHPMAIL') && $sendingmode == 'mail') {
  726. // EMailing feature may be a spam problem, so when you host several users/instance, having this option may force each user to use their own SMTP agent.
  727. // You ensure that every user is using its own SMTP server when using the mass emailing module.
  728. $linktoadminemailbefore = '<a href="'.DOL_URL_ROOT.'/admin/mails_emailing.php">';
  729. $linktoadminemailend = '</a>';
  730. setEventMessages($langs->trans("MailSendSetupIs", $listofmethods[$sendingmode]), null, 'warnings');
  731. $messagetoshow = $langs->trans("MailSendSetupIs2", '{s1}', '{s2}', '{s3}', '{s4}');
  732. $messagetoshow = str_replace('{s1}', $linktoadminemailbefore, $messagetoshow);
  733. $messagetoshow = str_replace('{s2}', $linktoadminemailend, $messagetoshow);
  734. $messagetoshow = str_replace('{s3}', $langs->transnoentitiesnoconv("MAIN_MAIL_SENDMODE"), $messagetoshow);
  735. $messagetoshow = str_replace('{s4}', $listofmethods['smtps'], $messagetoshow);
  736. setEventMessages($messagetoshow, null, 'warnings');
  737. if (getDolGlobalString('MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS')) {
  738. setEventMessages($langs->trans("MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS), null, 'warnings');
  739. }
  740. $_GET["action"] = '';
  741. } elseif (getDolGlobalInt('MAILING_LIMIT_SENDBYWEB') < 0) {
  742. if (getDolGlobalString('MAILING_LIMIT_WARNING_PHPMAIL') && $sendingmode == 'mail') {
  743. setEventMessages($langs->transnoentitiesnoconv($conf->global->MAILING_LIMIT_WARNING_PHPMAIL), null, 'warnings');
  744. }
  745. if (getDolGlobalString('MAILING_LIMIT_WARNING_NOPHPMAIL') && $sendingmode != 'mail') {
  746. setEventMessages($langs->transnoentitiesnoconv($conf->global->MAILING_LIMIT_WARNING_NOPHPMAIL), null, 'warnings');
  747. }
  748. // The feature is forbidden from GUI, we show just message to use from command line.
  749. setEventMessages($langs->trans("MailingNeedCommand"), null, 'warnings');
  750. setEventMessages('<textarea cols="60" rows="'.ROWS_1.'" wrap="soft">php ./scripts/emailings/mailing-send.php '.$object->id.'</textarea>', null, 'warnings');
  751. if ($conf->file->mailing_limit_sendbyweb != '-1') { // MAILING_LIMIT_SENDBYWEB was set to -1 in database, but it is allowed ot increase it.
  752. setEventMessages($langs->trans("MailingNeedCommand2"), null, 'warnings'); // You can send online with constant...
  753. }
  754. $_GET["action"] = '';
  755. } else {
  756. if (getDolGlobalString('MAILING_LIMIT_WARNING_PHPMAIL') && $sendingmode == 'mail') {
  757. setEventMessages($langs->transnoentitiesnoconv($conf->global->MAILING_LIMIT_WARNING_PHPMAIL), null, 'warnings');
  758. }
  759. if (getDolGlobalString('MAILING_LIMIT_WARNING_NOPHPMAIL') && $sendingmode != 'mail') {
  760. setEventMessages($langs->transnoentitiesnoconv($conf->global->MAILING_LIMIT_WARNING_NOPHPMAIL), null, 'warnings');
  761. }
  762. $text = '';
  763. if (getDolGlobalInt('MAILING_LIMIT_SENDBYDAY') > 0) {
  764. $text .= $langs->trans('WarningLimitSendByDay', getDolGlobalInt('MAILING_LIMIT_SENDBYDAY'));
  765. $text .= '<br><br>';
  766. }
  767. $text .= $langs->trans('ConfirmSendingEmailing').'<br>';
  768. $text .= $langs->trans('LimitSendingEmailing', $conf->global->MAILING_LIMIT_SENDBYWEB);
  769. if (!isset($conf->global->MAILING_LIMIT_SENDBYCLI) || getDolGlobalInt('MAILING_LIMIT_SENDBYCLI') >= 0) {
  770. $text .= '<br><br>';
  771. $text .= $langs->trans("MailingNeedCommand");
  772. $text .= '<br><textarea class="quatrevingtpercent" rows="'.ROWS_2.'" wrap="soft" disabled>php ./scripts/emailings/mailing-send.php '.$object->id.' '.$user->login.'</textarea>';
  773. }
  774. print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id, $langs->trans('SendMailing'), $text, 'sendallconfirmed', '', '', 1, 380, 660, 0, $langs->trans("Confirm"), $langs->trans("Cancel"));
  775. }
  776. }
  777. $linkback = '<a href="'.DOL_URL_ROOT.'/comm/mailing/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  778. $morehtmlref = '<div class="refidno">';
  779. // Ref customer
  780. $morehtmlref .= $form->editfieldkey("", 'title', $object->title, $object, $user->hasRight('mailing', 'creer'), 'string', '', 0, 1);
  781. $morehtmlref .= $form->editfieldval("", 'title', $object->title, $object, $user->hasRight('mailing', 'creer'), 'string', '', null, null, '', 1);
  782. $morehtmlref .= '</div>';
  783. $morehtmlright = '';
  784. $nbtry = $nbok = 0;
  785. if ($object->statut == 2 || $object->statut == 3) {
  786. $nbtry = $object->countNbOfTargets('alreadysent');
  787. $nbko = $object->countNbOfTargets('alreadysentko');
  788. $morehtmlright .= ' ('.$nbtry.'/'.$object->nbemail;
  789. if ($nbko) {
  790. $morehtmlright .= ' - '.$nbko.' '.$langs->trans("Error");
  791. }
  792. $morehtmlright .= ') &nbsp; ';
  793. }
  794. dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
  795. print '<div class="fichecenter">';
  796. print '<div class="fichehalfleft">';
  797. print '<div class="underbanner clearboth"></div>';
  798. print '<table class="border centpercent tableforfield">'."\n";
  799. // From
  800. print '<tr><td class="titlefield">';
  801. print $form->editfieldkey("MailFrom", 'email_from', $object->email_from, $object, $user->hasRight('mailing', 'creer') && $object->statut < 3, 'string');
  802. print '</td><td>';
  803. print $form->editfieldval("MailFrom", 'email_from', $object->email_from, $object, $user->hasRight('mailing', 'creer') && $object->statut < 3, 'string');
  804. $email = CMailFile::getValidAddress($object->email_from, 2);
  805. if ($email && !isValidEmail($email)) {
  806. $langs->load("errors");
  807. print img_warning($langs->trans("ErrorBadEMail", $email));
  808. } elseif ($email && !isValidMailDomain($email)) {
  809. $langs->load("errors");
  810. print img_warning($langs->trans("ErrorBadMXDomain", $email));
  811. }
  812. print '</td></tr>';
  813. // Errors to
  814. print '<tr><td>';
  815. print $form->editfieldkey("MailErrorsTo", 'email_errorsto', $object->email_errorsto, $object, $user->hasRight('mailing', 'creer') && $object->statut < 3, 'string');
  816. print '</td><td>';
  817. print $form->editfieldval("MailErrorsTo", 'email_errorsto', $object->email_errorsto, $object, $user->hasRight('mailing', 'creer') && $object->statut < 3, 'string');
  818. $email = CMailFile::getValidAddress($object->email_errorsto, 2);
  819. if ($email && !isValidEmail($email)) {
  820. $langs->load("errors");
  821. print img_warning($langs->trans("ErrorBadEMail", $email));
  822. } elseif ($email && !isValidMailDomain($email)) {
  823. $langs->load("errors");
  824. print img_warning($langs->trans("ErrorBadMXDomain", $email));
  825. }
  826. print '</td></tr>';
  827. print '</table>';
  828. print '</div>';
  829. print '<div class="fichehalfright">';
  830. print '<div class="underbanner clearboth"></div>';
  831. print '<table class="border centpercent tableforfield">';
  832. // Number of distinct emails
  833. print '<tr><td class="titlefield">';
  834. print $langs->trans("TotalNbOfDistinctRecipients");
  835. print '</td><td>';
  836. $nbemail = ($object->nbemail ? $object->nbemail : 0);
  837. if (is_numeric($nbemail)) {
  838. $text = '';
  839. if ((getDolGlobalString('MAILING_LIMIT_SENDBYWEB') && getDolGlobalInt('MAILING_LIMIT_SENDBYWEB') < $nbemail) && ($object->statut == 1 || ($object->statut == 2 && $nbtry < $nbemail))) {
  840. if (getDolGlobalInt('MAILING_LIMIT_SENDBYWEB') > 0) {
  841. $text .= $langs->trans('LimitSendingEmailing', getDolGlobalInt('MAILING_LIMIT_SENDBYWEB'));
  842. } else {
  843. $text .= $langs->trans('SendingFromWebInterfaceIsNotAllowed');
  844. }
  845. }
  846. if (empty($nbemail)) {
  847. $nbemail .= ' '.img_warning('').' <span class="warning">'.$langs->trans("NoTargetYet").'</span>';
  848. }
  849. if ($text) {
  850. print $form->textwithpicto($nbemail, $text, 1, 'warning');
  851. } else {
  852. print $nbemail;
  853. }
  854. }
  855. print '</td></tr>';
  856. print '<tr><td>';
  857. print $langs->trans("MAIN_MAIL_SENDMODE");
  858. print '</td><td>';
  859. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') && getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'default') {
  860. $text = $listofmethods[getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING')];
  861. } elseif (getDolGlobalString('MAIN_MAIL_SENDMODE')) {
  862. $text = $listofmethods[getDolGlobalString('MAIN_MAIL_SENDMODE')];
  863. } else {
  864. $text = $listofmethods['mail'];
  865. }
  866. print $text;
  867. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'default') {
  868. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'mail') {
  869. print ' <span class="opacitymedium">(';
  870. print getDolGlobalString('MAIN_MAIL_SMTP_SERVER_EMAILING', getDolGlobalString('MAIN_MAIL_SMTP_SERVER'));
  871. print ')</span>';
  872. }
  873. } elseif (getDolGlobalString('MAIN_MAIL_SENDMODE') != 'mail' && getDolGlobalString('MAIN_MAIL_SMTP_SERVER')) {
  874. print ' <span class="opacitymedium">('.getDolGlobalString('MAIN_MAIL_SMTP_SERVER').')</span>';
  875. }
  876. print '</td></tr>';
  877. // Other attributes. Fields from hook formObjectOptions and Extrafields.
  878. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  879. print '</table>';
  880. print '</div>';
  881. print '</div>';
  882. print '<div class="clearboth"></div>';
  883. print dol_get_fiche_end();
  884. // Clone confirmation
  885. if ($action == 'clone') {
  886. // Create an array for form
  887. $formquestion = array(
  888. 'text' => $langs->trans("ConfirmClone"),
  889. array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneContent"), 'value' => 1),
  890. array('type' => 'checkbox', 'name' => 'clone_receivers', 'label' => $langs->trans("CloneReceivers"), 'value' => 0)
  891. );
  892. // Incomplete payment. On demande si motif = escompte ou autre
  893. print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneEMailing', $object->ref), 'confirm_clone', $formquestion, 'yes', 2, 240);
  894. }
  895. // Actions Buttons
  896. if (GETPOST('cancel', 'alpha') || $confirm == 'no' || $action == '' || in_array($action, array('settodraft', 'valid', 'delete', 'sendall', 'clone', 'test', 'editevenunsubscribe'))) {
  897. print "\n\n<div class=\"tabsAction\">\n";
  898. if (($object->statut == 1) && ($user->hasRight('mailing', 'valider') || $object->user_validation_id == $user->id)) {
  899. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=settodraft&token='.newToken().'&id='.$object->id.'">'.$langs->trans("SetToDraft").'</a>';
  900. }
  901. if (($object->statut == 0 || $object->statut == 1 || $object->statut == 2) && $user->hasRight('mailing', 'creer')) {
  902. if (isModEnabled('fckeditor') && getDolGlobalString('FCKEDITOR_ENABLE_MAILING')) {
  903. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&token='.newToken().'&id='.$object->id.'">'.$langs->trans("EditWithEditor").'</a>';
  904. } else {
  905. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&token='.newToken().'&id='.$object->id.'">'.$langs->trans("EditWithTextEditor").'</a>';
  906. }
  907. if (!empty($conf->use_javascript_ajax)) {
  908. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edithtml&token='.newToken().'&id='.$object->id.'">'.$langs->trans("EditHTMLSource").'</a>';
  909. }
  910. }
  911. //print '<a class="butAction" href="card.php?action=test&amp;id='.$object->id.'">'.$langs->trans("PreviewMailing").'</a>';
  912. if (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !$user->hasRight('mailing', 'mailing_advance', 'send')) {
  913. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("TestMailing").'</a>';
  914. } else {
  915. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=test&token='.newToken().'&id='.$object->id.'">'.$langs->trans("TestMailing").'</a>';
  916. }
  917. if ($object->statut == 0) {
  918. if ($object->nbemail <= 0) {
  919. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NoTargetYet")).'">'.$langs->trans("ValidMailing").'</a>';
  920. } elseif (!$user->hasRight('mailing', 'valider')) {
  921. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("ValidMailing").'</a>';
  922. } else {
  923. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=valid&amp;id='.$object->id.'">'.$langs->trans("ValidMailing").'</a>';
  924. }
  925. }
  926. if (($object->statut == 1 || $object->statut == 2) && $object->nbemail > 0 && $user->hasRight('mailing', 'valider')) {
  927. if (getDolGlobalInt('MAILING_LIMIT_SENDBYWEB') < 0) {
  928. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("SendingFromWebInterfaceIsNotAllowed")).'">'.$langs->trans("SendMailing").'</a>';
  929. } elseif (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !$user->hasRight('mailing', 'mailing_advance', 'send')) {
  930. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("SendMailing").'</a>';
  931. } else {
  932. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=sendall&amp;id='.$object->id.'">'.$langs->trans("SendMailing").'</a>';
  933. }
  934. }
  935. if ($user->hasRight('mailing', 'creer')) {
  936. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=clone&amp;object=emailing&amp;id='.$object->id.'">'.$langs->trans("ToClone").'</a>';
  937. }
  938. if (($object->statut == 2 || $object->statut == 3) && $user->hasRight('mailing', 'valider')) {
  939. if (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !$user->hasRight('mailing', 'mailing_advance', 'send')) {
  940. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("ResetMailing").'</a>';
  941. } else {
  942. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=reset&amp;id='.$object->id.'">'.$langs->trans("ResetMailing").'</a>';
  943. }
  944. }
  945. if (($object->statut <= 1 && $user->hasRight('mailing', 'creer')) || $user->hasRight('mailing', 'supprimer')) {
  946. if ($object->statut > 0 && (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !$user->hasRight('mailing', 'mailing_advance', 'delete'))) {
  947. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("DeleteMailing").'</a>';
  948. } else {
  949. print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&id='.$object->id.(!empty($urlfrom) ? '&urlfrom='.$urlfrom : '').'">'.$langs->trans("DeleteMailing").'</a>';
  950. }
  951. }
  952. print '</div>';
  953. }
  954. // Display of the TEST form
  955. if ($action == 'test') {
  956. print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
  957. print load_fiche_titre($langs->trans("TestMailing"));
  958. print dol_get_fiche_head(null, '', '', -1);
  959. // Create mail form object
  960. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  961. $formmail = new FormMail($db);
  962. $formmail->fromname = $object->email_from;
  963. $formmail->frommail = $object->email_from;
  964. $formmail->withsubstit = 1;
  965. $formmail->withfrom = 0;
  966. $formmail->withto = $user->email ? $user->email : 1;
  967. $formmail->withtocc = 0;
  968. $formmail->withtoccc = getDolGlobalString('MAIN_EMAIL_USECCC');
  969. $formmail->withtopic = 0;
  970. $formmail->withtopicreadonly = 1;
  971. $formmail->withfile = 0;
  972. $formmail->withbody = 0;
  973. $formmail->withbodyreadonly = 1;
  974. $formmail->withcancel = 1;
  975. $formmail->withdeliveryreceipt = 0;
  976. // Table of substitutions
  977. $formmail->substit = $object->substitutionarrayfortest;
  978. // Table of post's complementary params
  979. $formmail->param["action"] = "send";
  980. $formmail->param["models"] = 'none';
  981. $formmail->param["mailid"] = $object->id;
  982. $formmail->param["returnurl"] = $_SERVER['PHP_SELF']."?id=".$object->id;
  983. print $formmail->get_form();
  984. print '<br>';
  985. print dol_get_fiche_end();
  986. dol_set_focus('#sendto');
  987. }
  988. $htmltext = '<i>'.$langs->trans("FollowingConstantsWillBeSubstituted").':<br><br><span class="small">';
  989. foreach ($object->substitutionarray as $key => $val) {
  990. $htmltext .= $key.' = '.$langs->trans($val).'<br>';
  991. }
  992. $htmltext .= '</span></i>';
  993. // Print mail content
  994. print load_fiche_titre($langs->trans("EMail"), $form->textwithpicto('<span class="opacitymedium hideonsmartphone">'.$langs->trans("AvailableVariables").'</span>', $htmltext, 1, 'helpclickable', '', 0, 3, 'emailsubstitionhelp'), 'generic');
  995. print dol_get_fiche_head('', '', '', -1);
  996. print '<table class="bordernooddeven tableforfield centpercent">';
  997. // Subject
  998. print '<tr><td class="titlefield">'.$langs->trans("MailTopic").'</td><td colspan="3">'.$object->sujet.'</td></tr>';
  999. // Joined files
  1000. print '<tr><td>'.$langs->trans("MailFile").'</td><td colspan="3">';
  1001. // List of files
  1002. $listofpaths = dol_dir_list($upload_dir, 'all', 0, '', '', 'name', SORT_ASC, 0);
  1003. if (count($listofpaths)) {
  1004. foreach ($listofpaths as $key => $val) {
  1005. print img_mime($listofpaths[$key]['name']).' '.$listofpaths[$key]['name'];
  1006. print '<br>';
  1007. }
  1008. } else {
  1009. print '<span class="opacitymedium">'.$langs->trans("NoAttachedFiles").'</span><br>';
  1010. }
  1011. print '</td></tr>';
  1012. // Background color
  1013. /*print '<tr><td width="15%">'.$langs->trans("BackgroundColorByDefault").'</td><td colspan="3">';
  1014. print $htmlother->selectColor($object->bgcolor,'bgcolor','',0);
  1015. print '</td></tr>';*/
  1016. print '</table>';
  1017. // Message
  1018. print '<div style="padding-top: 10px; background: '.($object->bgcolor ? (preg_match('/^#/', $object->bgcolor) ? '' : '#').$object->bgcolor : 'white').'">';
  1019. if (empty($object->bgcolor) || strtolower($object->bgcolor) == 'ffffff') { // CKEditor does not apply the color of the div into its content area
  1020. $readonly = 1;
  1021. // wysiwyg editor
  1022. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1023. $doleditor = new DolEditor('bodyemail', $object->body, '', 600, 'dolibarr_mailings', '', false, true, !getDolGlobalString('FCKEDITOR_ENABLE_MAILING') ? 0 : 1, 20, '90%', $readonly);
  1024. $doleditor->Create();
  1025. } else {
  1026. print dol_htmlentitiesbr($object->body);
  1027. }
  1028. print '</div>';
  1029. print dol_get_fiche_end();
  1030. } else {
  1031. /*
  1032. * Edition mode mailing (CKeditor or HTML source)
  1033. */
  1034. print dol_get_fiche_head($head, 'card', $langs->trans("Mailing"), -1, 'email');
  1035. $linkback = '<a href="'.DOL_URL_ROOT.'/comm/mailing/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  1036. $morehtmlref = '<div class="refidno">';
  1037. // Ref customer
  1038. $morehtmlref .= $form->editfieldkey("", 'title', $object->title, $object, $user->hasRight('mailing', 'creer'), 'string', '', 0, 1);
  1039. $morehtmlref .= $form->editfieldval("", 'title', $object->title, $object, $user->hasRight('mailing', 'creer'), 'string', '', null, null, '', 1);
  1040. $morehtmlref .= '</div>';
  1041. $morehtmlright = '';
  1042. $nbtry = $nbok = 0;
  1043. if ($object->statut == 2 || $object->statut == 3) {
  1044. $nbtry = $object->countNbOfTargets('alreadysent');
  1045. $nbko = $object->countNbOfTargets('alreadysentko');
  1046. $morehtmlright .= ' ('.$nbtry.'/'.$object->nbemail;
  1047. if ($nbko) {
  1048. $morehtmlright .= ' - '.$nbko.' '.$langs->trans("Error");
  1049. }
  1050. $morehtmlright .= ') &nbsp; ';
  1051. }
  1052. dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
  1053. print '<div class="fichecenter">';
  1054. print '<div class="fichehalfleft">';
  1055. print '<div class="underbanner clearboth"></div>';
  1056. print '<table class="border centpercent tableforfield">';
  1057. /*
  1058. print '<tr><td class="titlefield">'.$langs->trans("Ref").'</td>';
  1059. print '<td colspan="3">';
  1060. print $form->showrefnav($object,'id', $linkback);
  1061. print '</td></tr>';
  1062. */
  1063. // From
  1064. print '<tr><td class="titlefield">'.$langs->trans("MailFrom").'</td><td>'.dol_print_email($object->email_from, 0, 0, 0, 0, 1).'</td></tr>';
  1065. // To
  1066. print '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td>'.dol_print_email($object->email_errorsto, 0, 0, 0, 0, 1).'</td></tr>';
  1067. print '</table>';
  1068. print '</div>';
  1069. print '<div class="fichehalfright">';
  1070. print '<div class="underbanner clearboth"></div>';
  1071. print '<table class="border centpercent tableforfield">';
  1072. // Number of distinct emails
  1073. print '<tr><td>';
  1074. print $langs->trans("TotalNbOfDistinctRecipients");
  1075. print '</td><td>';
  1076. $nbemail = ($object->nbemail ? $object->nbemail : 0);
  1077. if (is_numeric($nbemail)) {
  1078. $text = '';
  1079. if ((getDolGlobalString('MAILING_LIMIT_SENDBYWEB') && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail) && ($object->statut == 1 || $object->statut == 2)) {
  1080. if (getDolGlobalInt('MAILING_LIMIT_SENDBYWEB') > 0) {
  1081. $text .= $langs->trans('LimitSendingEmailing', $conf->global->MAILING_LIMIT_SENDBYWEB);
  1082. } else {
  1083. $text .= $langs->trans('SendingFromWebInterfaceIsNotAllowed');
  1084. }
  1085. }
  1086. if (empty($nbemail)) {
  1087. $nbemail .= ' '.img_warning('').' <span class="warning">'.$langs->trans("NoTargetYet").'</span>';
  1088. }
  1089. if ($text) {
  1090. print $form->textwithpicto($nbemail, $text, 1, 'warning');
  1091. } else {
  1092. print $nbemail;
  1093. }
  1094. }
  1095. print '</td></tr>';
  1096. print '<tr><td>';
  1097. print $langs->trans("MAIN_MAIL_SENDMODE");
  1098. print '</td><td>';
  1099. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') && getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'default') {
  1100. $text = $listofmethods[getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING')];
  1101. } elseif (getDolGlobalString('MAIN_MAIL_SENDMODE')) {
  1102. $text = $listofmethods[getDolGlobalString('MAIN_MAIL_SENDMODE')];
  1103. } else {
  1104. $text = $listofmethods['mail'];
  1105. }
  1106. print $text;
  1107. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'default') {
  1108. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'mail') {
  1109. print ' <span class="opacitymedium">('.getDolGlobalString('MAIN_MAIL_SMTP_SERVER_EMAILING').')</span>';
  1110. }
  1111. } elseif (getDolGlobalString('MAIN_MAIL_SENDMODE') != 'mail' && getDolGlobalString('MAIN_MAIL_SMTP_SERVER')) {
  1112. print ' <span class="opacitymedium">('.getDolGlobalString('MAIN_MAIL_SMTP_SERVER').')</span>';
  1113. }
  1114. print '</td></tr>';
  1115. // Other attributes
  1116. $parameters = array();
  1117. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1118. print $hookmanager->resPrint;
  1119. if (empty($reshook)) {
  1120. print $object->showOptionals($extrafields, 'edit', $parameters);
  1121. }
  1122. print '</table>';
  1123. print '</div>';
  1124. print '</div>';
  1125. print '<div class="clearboth"></div>';
  1126. print dol_get_fiche_end();
  1127. print "<br><br>\n";
  1128. print '<form name="edit_mailing" action="card.php" method="post" enctype="multipart/form-data">'."\n";
  1129. print '<input type="hidden" name="token" value="'.newToken().'">';
  1130. print '<input type="hidden" name="action" value="update">';
  1131. print '<input type="hidden" name="id" value="'.$object->id.'">';
  1132. $htmltext = '<i>'.$langs->trans("FollowingConstantsWillBeSubstituted").':<br><br><span class="small">';
  1133. foreach ($object->substitutionarray as $key => $val) {
  1134. $htmltext .= $key.' = '.$langs->trans($val).'<br>';
  1135. }
  1136. $htmltext .= '</span></i>';
  1137. // Print mail content
  1138. print load_fiche_titre($langs->trans("EMail"), '<span class="opacitymedium">'.$form->textwithpicto($langs->trans("AvailableVariables").'</span>', $htmltext, 1, 'help', '', 0, 2, 'emailsubstitionhelp'), 'generic');
  1139. print dol_get_fiche_head(null, '', '', -1);
  1140. print '<table class="bordernooddeven centpercent">';
  1141. // Subject
  1142. print '<tr><td class="fieldrequired titlefield">'.$langs->trans("MailTopic").'</td><td colspan="3"><input class="flat quatrevingtpercent" type="text" name="sujet" value="'.$object->sujet.'"></td></tr>';
  1143. $trackid = ''; // TODO To avoid conflicts with 2 mass emailing, we should set a trackid here, even if we use another one into email header.
  1144. dol_init_file_process($upload_dir, $trackid);
  1145. // Joined files
  1146. $addfileaction = 'addfile';
  1147. print '<tr><td>'.$langs->trans("MailFile").'</td>';
  1148. print '<td colspan="3">';
  1149. // List of files
  1150. $listofpaths = dol_dir_list($upload_dir, 'all', 0, '', '', 'name', SORT_ASC, 0);
  1151. // TODO Trick to have param removedfile containing nb of image to delete. But this does not works without javascript
  1152. $out .= '<input type="hidden" class="removedfilehidden" name="removedfile" value="">'."\n";
  1153. $out .= '<script type="text/javascript">';
  1154. $out .= 'jQuery(document).ready(function () {';
  1155. $out .= ' jQuery(".removedfile").click(function() {';
  1156. $out .= ' jQuery(".removedfilehidden").val(jQuery(this).val());';
  1157. $out .= ' });';
  1158. $out .= '})';
  1159. $out .= '</script>'."\n";
  1160. if (count($listofpaths)) {
  1161. foreach ($listofpaths as $key => $val) {
  1162. $out .= '<div id="attachfile_'.$key.'">';
  1163. $out .= img_mime($listofpaths[$key]['name']).' '.$listofpaths[$key]['name'];
  1164. $out .= ' <input type="image" style="border: 0px;" src="'.img_picto($langs->trans("Search"), 'delete.png', '', '', 1).'" value="'.($key + 1).'" class="removedfile" id="removedfile_'.$key.'" name="removedfile_'.$key.'" />';
  1165. $out .= '<br></div>';
  1166. }
  1167. } else {
  1168. //$out .= '<span class="opacitymedium">'.$langs->trans("NoAttachedFiles").'</span><br>';
  1169. }
  1170. // Add link to add file
  1171. $maxfilesizearray = getMaxFileSizeArray();
  1172. $maxmin = $maxfilesizearray['maxmin'];
  1173. if ($maxmin > 0) {
  1174. $out .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
  1175. }
  1176. $out .= '<input type="file" class="flat" id="addedfile" name="addedfile" value="'.$langs->trans("Upload").'" />';
  1177. $out .= ' ';
  1178. $out .= '<input type="submit" class="button smallpaddingimp" id="'.$addfileaction.'" name="'.$addfileaction.'" value="'.$langs->trans("MailingAddFile").'" />';
  1179. print $out;
  1180. print '</td></tr>';
  1181. // Background color
  1182. print '<tr><td>'.$langs->trans("BackgroundColorByDefault").'</td><td colspan="3">';
  1183. print $htmlother->selectColor($object->bgcolor, 'bgcolor', '', 0);
  1184. print '</td></tr>';
  1185. print '</table>';
  1186. // Message
  1187. print '<div style="padding-top: 10px">';
  1188. if ($action == 'edit') {
  1189. // wysiwyg editor
  1190. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1191. $doleditor = new DolEditor('bodyemail', $object->body, '', 600, 'dolibarr_mailings', '', true, true, getDolGlobalInt('FCKEDITOR_ENABLE_MAILING'), 20, '90%');
  1192. $doleditor->Create();
  1193. }
  1194. if ($action == 'edithtml') {
  1195. // HTML source editor
  1196. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1197. $doleditor = new DolEditor('bodyemail', $object->body, '', 600, 'dolibarr_mailings', '', true, true, 'ace', 20, '90%');
  1198. $doleditor->Create(0, '', false, 'HTML Source', 'php');
  1199. }
  1200. print '</div>';
  1201. print dol_get_fiche_end();
  1202. print '<div class="center">';
  1203. print '<input type="submit" class="button buttonforacesave button-save" value="'.$langs->trans("Save").'" name="save">';
  1204. print '&nbsp; &nbsp; &nbsp;';
  1205. print '<input type="submit" class="button button-cancel" value="'.$langs->trans("Cancel").'" name="cancel">';
  1206. print '</div>';
  1207. print '</form>';
  1208. print '<br>';
  1209. }
  1210. } else {
  1211. dol_print_error($db, $object->error);
  1212. }
  1213. }
  1214. // End of page
  1215. llxFooter();
  1216. $db->close();