card.php 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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($conf->global->PAYMENT_SECURITY_TOKEN, 2);
  242. if (!getDolGlobalString('PAYMENT_SECURITY_TOKEN_UNIQUE')) {
  243. $substitutionarray['__SECUREKEYPAYMENT_MEMBER__'] = dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
  244. $substitutionarray['__SECUREKEYPAYMENT_DONATION__'] = dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
  245. $substitutionarray['__SECUREKEYPAYMENT_ORDER__'] = dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
  246. $substitutionarray['__SECUREKEYPAYMENT_INVOICE__'] = dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
  247. $substitutionarray['__SECUREKEYPAYMENT_CONTRACTLINE__'] = dol_hash($conf->global->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($conf->global->PAYPAL_SECURITY_TOKEN, 2);
  262. if (!getDolGlobalString('PAYPAL_SECURITY_TOKEN_UNIQUE')) {
  263. $substitutionarray['__SECUREKEYPAYPAL_MEMBER__'] = dol_hash($conf->global->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($conf->global->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($conf->global->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($conf->global->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 (isset($conf->global->MAILING_LIMIT_SENDBYDAY) && getDolGlobalInt('MAILING_LIMIT_SENDBYDAY') >= 0) {
  764. $text .= $langs->trans('WarningLimitSendByDay', $conf->global->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') && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail) && ($object->statut == 1 || ($object->statut == 2 && $nbtry < $nbemail))) {
  840. if (getDolGlobalInt('MAILING_LIMIT_SENDBYWEB') > 0) {
  841. $text .= $langs->trans('LimitSendingEmailing', $conf->global->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">('.getDolGlobalString('MAIN_MAIL_SMTP_SERVER_EMAILING').')</span>';
  870. }
  871. } elseif (getDolGlobalString('MAIN_MAIL_SENDMODE') != 'mail' && getDolGlobalString('MAIN_MAIL_SMTP_SERVER')) {
  872. print ' <span class="opacitymedium">('.getDolGlobalString('MAIN_MAIL_SMTP_SERVER').')</span>';
  873. }
  874. print '</td></tr>';
  875. // Other attributes. Fields from hook formObjectOptions and Extrafields.
  876. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  877. print '</table>';
  878. print '</div>';
  879. print '</div>';
  880. print '<div class="clearboth"></div>';
  881. print dol_get_fiche_end();
  882. // Clone confirmation
  883. if ($action == 'clone') {
  884. // Create an array for form
  885. $formquestion = array(
  886. 'text' => $langs->trans("ConfirmClone"),
  887. array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneContent"), 'value' => 1),
  888. array('type' => 'checkbox', 'name' => 'clone_receivers', 'label' => $langs->trans("CloneReceivers"), 'value' => 0)
  889. );
  890. // Incomplete payment. On demande si motif = escompte ou autre
  891. print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneEMailing', $object->ref), 'confirm_clone', $formquestion, 'yes', 2, 240);
  892. }
  893. // Actions Buttons
  894. if (GETPOST('cancel', 'alpha') || $confirm == 'no' || $action == '' || in_array($action, array('settodraft', 'valid', 'delete', 'sendall', 'clone', 'test', 'editevenunsubscribe'))) {
  895. print "\n\n<div class=\"tabsAction\">\n";
  896. if (($object->statut == 1) && ($user->hasRight('mailing', 'valider') || $object->user_validation_id == $user->id)) {
  897. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=settodraft&token='.newToken().'&id='.$object->id.'">'.$langs->trans("SetToDraft").'</a>';
  898. }
  899. if (($object->statut == 0 || $object->statut == 1 || $object->statut == 2) && $user->hasRight('mailing', 'creer')) {
  900. if (isModEnabled('fckeditor') && getDolGlobalString('FCKEDITOR_ENABLE_MAILING')) {
  901. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&token='.newToken().'&id='.$object->id.'">'.$langs->trans("EditWithEditor").'</a>';
  902. } else {
  903. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&token='.newToken().'&id='.$object->id.'">'.$langs->trans("EditWithTextEditor").'</a>';
  904. }
  905. if (!empty($conf->use_javascript_ajax)) {
  906. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edithtml&token='.newToken().'&id='.$object->id.'">'.$langs->trans("EditHTMLSource").'</a>';
  907. }
  908. }
  909. //print '<a class="butAction" href="card.php?action=test&amp;id='.$object->id.'">'.$langs->trans("PreviewMailing").'</a>';
  910. if (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !$user->hasRight('mailing', 'mailing_advance', 'send')) {
  911. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("TestMailing").'</a>';
  912. } else {
  913. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=test&token='.newToken().'&id='.$object->id.'">'.$langs->trans("TestMailing").'</a>';
  914. }
  915. if ($object->statut == 0) {
  916. if ($object->nbemail <= 0) {
  917. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NoTargetYet")).'">'.$langs->trans("ValidMailing").'</a>';
  918. } elseif (!$user->hasRight('mailing', 'valider')) {
  919. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("ValidMailing").'</a>';
  920. } else {
  921. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=valid&amp;id='.$object->id.'">'.$langs->trans("ValidMailing").'</a>';
  922. }
  923. }
  924. if (($object->statut == 1 || $object->statut == 2) && $object->nbemail > 0 && $user->hasRight('mailing', 'valider')) {
  925. if (getDolGlobalInt('MAILING_LIMIT_SENDBYWEB') < 0) {
  926. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("SendingFromWebInterfaceIsNotAllowed")).'">'.$langs->trans("SendMailing").'</a>';
  927. } elseif (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !$user->hasRight('mailing', 'mailing_advance', 'send')) {
  928. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("SendMailing").'</a>';
  929. } else {
  930. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=sendall&amp;id='.$object->id.'">'.$langs->trans("SendMailing").'</a>';
  931. }
  932. }
  933. if ($user->hasRight('mailing', 'creer')) {
  934. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=clone&amp;object=emailing&amp;id='.$object->id.'">'.$langs->trans("ToClone").'</a>';
  935. }
  936. if (($object->statut == 2 || $object->statut == 3) && $user->hasRight('mailing', 'valider')) {
  937. if (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !$user->hasRight('mailing', 'mailing_advance', 'send')) {
  938. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("ResetMailing").'</a>';
  939. } else {
  940. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=reset&amp;id='.$object->id.'">'.$langs->trans("ResetMailing").'</a>';
  941. }
  942. }
  943. if (($object->statut <= 1 && $user->hasRight('mailing', 'creer')) || $user->hasRight('mailing', 'supprimer')) {
  944. if ($object->statut > 0 && (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !$user->hasRight('mailing', 'mailing_advance', 'delete'))) {
  945. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("DeleteMailing").'</a>';
  946. } else {
  947. print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&id='.$object->id.(!empty($urlfrom) ? '&urlfrom='.$urlfrom : '').'">'.$langs->trans("DeleteMailing").'</a>';
  948. }
  949. }
  950. print '</div>';
  951. }
  952. // Display of the TEST form
  953. if ($action == 'test') {
  954. print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
  955. print load_fiche_titre($langs->trans("TestMailing"));
  956. print dol_get_fiche_head(null, '', '', -1);
  957. // Create mail form object
  958. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  959. $formmail = new FormMail($db);
  960. $formmail->fromname = $object->email_from;
  961. $formmail->frommail = $object->email_from;
  962. $formmail->withsubstit = 1;
  963. $formmail->withfrom = 0;
  964. $formmail->withto = $user->email ? $user->email : 1;
  965. $formmail->withtocc = 0;
  966. $formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC;
  967. $formmail->withtopic = 0;
  968. $formmail->withtopicreadonly = 1;
  969. $formmail->withfile = 0;
  970. $formmail->withbody = 0;
  971. $formmail->withbodyreadonly = 1;
  972. $formmail->withcancel = 1;
  973. $formmail->withdeliveryreceipt = 0;
  974. // Table of substitutions
  975. $formmail->substit = $object->substitutionarrayfortest;
  976. // Table of post's complementary params
  977. $formmail->param["action"] = "send";
  978. $formmail->param["models"] = 'none';
  979. $formmail->param["mailid"] = $object->id;
  980. $formmail->param["returnurl"] = $_SERVER['PHP_SELF']."?id=".$object->id;
  981. print $formmail->get_form();
  982. print '<br>';
  983. print dol_get_fiche_end();
  984. dol_set_focus('#sendto');
  985. }
  986. $htmltext = '<i>'.$langs->trans("FollowingConstantsWillBeSubstituted").':<br><br><span class="small">';
  987. foreach ($object->substitutionarray as $key => $val) {
  988. $htmltext .= $key.' = '.$langs->trans($val).'<br>';
  989. }
  990. $htmltext .= '</span></i>';
  991. // Print mail content
  992. print load_fiche_titre($langs->trans("EMail"), $form->textwithpicto('<span class="opacitymedium hideonsmartphone">'.$langs->trans("AvailableVariables").'</span>', $htmltext, 1, 'helpclickable', '', 0, 3, 'emailsubstitionhelp'), 'generic');
  993. print dol_get_fiche_head('', '', '', -1);
  994. print '<table class="bordernooddeven tableforfield centpercent">';
  995. // Subject
  996. print '<tr><td class="titlefield">'.$langs->trans("MailTopic").'</td><td colspan="3">'.$object->sujet.'</td></tr>';
  997. // Joined files
  998. print '<tr><td>'.$langs->trans("MailFile").'</td><td colspan="3">';
  999. // List of files
  1000. $listofpaths = dol_dir_list($upload_dir, 'all', 0, '', '', 'name', SORT_ASC, 0);
  1001. if (count($listofpaths)) {
  1002. foreach ($listofpaths as $key => $val) {
  1003. print img_mime($listofpaths[$key]['name']).' '.$listofpaths[$key]['name'];
  1004. print '<br>';
  1005. }
  1006. } else {
  1007. print '<span class="opacitymedium">'.$langs->trans("NoAttachedFiles").'</span><br>';
  1008. }
  1009. print '</td></tr>';
  1010. // Background color
  1011. /*print '<tr><td width="15%">'.$langs->trans("BackgroundColorByDefault").'</td><td colspan="3">';
  1012. print $htmlother->selectColor($object->bgcolor,'bgcolor','',0);
  1013. print '</td></tr>';*/
  1014. print '</table>';
  1015. // Message
  1016. print '<div style="padding-top: 10px; background: '.($object->bgcolor ? (preg_match('/^#/', $object->bgcolor) ? '' : '#').$object->bgcolor : 'white').'">';
  1017. if (empty($object->bgcolor) || strtolower($object->bgcolor) == 'ffffff') { // CKEditor does not apply the color of the div into its content area
  1018. $readonly = 1;
  1019. // wysiwyg editor
  1020. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1021. $doleditor = new DolEditor('bodyemail', $object->body, '', 600, 'dolibarr_mailings', '', false, true, !getDolGlobalString('FCKEDITOR_ENABLE_MAILING') ? 0 : 1, 20, '90%', $readonly);
  1022. $doleditor->Create();
  1023. } else {
  1024. print dol_htmlentitiesbr($object->body);
  1025. }
  1026. print '</div>';
  1027. print dol_get_fiche_end();
  1028. } else {
  1029. /*
  1030. * Edition mode mailing (CKeditor or HTML source)
  1031. */
  1032. print dol_get_fiche_head($head, 'card', $langs->trans("Mailing"), -1, 'email');
  1033. $linkback = '<a href="'.DOL_URL_ROOT.'/comm/mailing/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  1034. $morehtmlref = '<div class="refidno">';
  1035. // Ref customer
  1036. $morehtmlref .= $form->editfieldkey("", 'title', $object->title, $object, $user->hasRight('mailing', 'creer'), 'string', '', 0, 1);
  1037. $morehtmlref .= $form->editfieldval("", 'title', $object->title, $object, $user->hasRight('mailing', 'creer'), 'string', '', null, null, '', 1);
  1038. $morehtmlref .= '</div>';
  1039. $morehtmlright = '';
  1040. $nbtry = $nbok = 0;
  1041. if ($object->statut == 2 || $object->statut == 3) {
  1042. $nbtry = $object->countNbOfTargets('alreadysent');
  1043. $nbko = $object->countNbOfTargets('alreadysentko');
  1044. $morehtmlright .= ' ('.$nbtry.'/'.$object->nbemail;
  1045. if ($nbko) {
  1046. $morehtmlright .= ' - '.$nbko.' '.$langs->trans("Error");
  1047. }
  1048. $morehtmlright .= ') &nbsp; ';
  1049. }
  1050. dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
  1051. print '<div class="fichecenter">';
  1052. print '<div class="fichehalfleft">';
  1053. print '<div class="underbanner clearboth"></div>';
  1054. print '<table class="border centpercent tableforfield">';
  1055. /*
  1056. print '<tr><td class="titlefield">'.$langs->trans("Ref").'</td>';
  1057. print '<td colspan="3">';
  1058. print $form->showrefnav($object,'id', $linkback);
  1059. print '</td></tr>';
  1060. */
  1061. // From
  1062. print '<tr><td class="titlefield">'.$langs->trans("MailFrom").'</td><td>'.dol_print_email($object->email_from, 0, 0, 0, 0, 1).'</td></tr>';
  1063. // To
  1064. print '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td>'.dol_print_email($object->email_errorsto, 0, 0, 0, 0, 1).'</td></tr>';
  1065. print '</table>';
  1066. print '</div>';
  1067. print '<div class="fichehalfright">';
  1068. print '<div class="underbanner clearboth"></div>';
  1069. print '<table class="border centpercent tableforfield">';
  1070. // Number of distinct emails
  1071. print '<tr><td>';
  1072. print $langs->trans("TotalNbOfDistinctRecipients");
  1073. print '</td><td>';
  1074. $nbemail = ($object->nbemail ? $object->nbemail : 0);
  1075. if (is_numeric($nbemail)) {
  1076. $text = '';
  1077. if ((getDolGlobalString('MAILING_LIMIT_SENDBYWEB') && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail) && ($object->statut == 1 || $object->statut == 2)) {
  1078. if (getDolGlobalInt('MAILING_LIMIT_SENDBYWEB') > 0) {
  1079. $text .= $langs->trans('LimitSendingEmailing', $conf->global->MAILING_LIMIT_SENDBYWEB);
  1080. } else {
  1081. $text .= $langs->trans('SendingFromWebInterfaceIsNotAllowed');
  1082. }
  1083. }
  1084. if (empty($nbemail)) {
  1085. $nbemail .= ' '.img_warning('').' <span class="warning">'.$langs->trans("NoTargetYet").'</span>';
  1086. }
  1087. if ($text) {
  1088. print $form->textwithpicto($nbemail, $text, 1, 'warning');
  1089. } else {
  1090. print $nbemail;
  1091. }
  1092. }
  1093. print '</td></tr>';
  1094. print '<tr><td>';
  1095. print $langs->trans("MAIN_MAIL_SENDMODE");
  1096. print '</td><td>';
  1097. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') && getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'default') {
  1098. $text = $listofmethods[getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING')];
  1099. } elseif (getDolGlobalString('MAIN_MAIL_SENDMODE')) {
  1100. $text = $listofmethods[getDolGlobalString('MAIN_MAIL_SENDMODE')];
  1101. } else {
  1102. $text = $listofmethods['mail'];
  1103. }
  1104. print $text;
  1105. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'default') {
  1106. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'mail') {
  1107. print ' <span class="opacitymedium">('.getDolGlobalString('MAIN_MAIL_SMTP_SERVER_EMAILING').')</span>';
  1108. }
  1109. } elseif (getDolGlobalString('MAIN_MAIL_SENDMODE') != 'mail' && getDolGlobalString('MAIN_MAIL_SMTP_SERVER')) {
  1110. print ' <span class="opacitymedium">('.getDolGlobalString('MAIN_MAIL_SMTP_SERVER').')</span>';
  1111. }
  1112. print '</td></tr>';
  1113. // Other attributes
  1114. $parameters = array();
  1115. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1116. print $hookmanager->resPrint;
  1117. if (empty($reshook)) {
  1118. print $object->showOptionals($extrafields, 'edit', $parameters);
  1119. }
  1120. print '</table>';
  1121. print '</div>';
  1122. print '</div>';
  1123. print '<div class="clearboth"></div>';
  1124. print dol_get_fiche_end();
  1125. print "<br><br>\n";
  1126. print '<form name="edit_mailing" action="card.php" method="post" enctype="multipart/form-data">'."\n";
  1127. print '<input type="hidden" name="token" value="'.newToken().'">';
  1128. print '<input type="hidden" name="action" value="update">';
  1129. print '<input type="hidden" name="id" value="'.$object->id.'">';
  1130. $htmltext = '<i>'.$langs->trans("FollowingConstantsWillBeSubstituted").':<br><br><span class="small">';
  1131. foreach ($object->substitutionarray as $key => $val) {
  1132. $htmltext .= $key.' = '.$langs->trans($val).'<br>';
  1133. }
  1134. $htmltext .= '</span></i>';
  1135. // Print mail content
  1136. print load_fiche_titre($langs->trans("EMail"), '<span class="opacitymedium">'.$form->textwithpicto($langs->trans("AvailableVariables").'</span>', $htmltext, 1, 'help', '', 0, 2, 'emailsubstitionhelp'), 'generic');
  1137. print dol_get_fiche_head(null, '', '', -1);
  1138. print '<table class="bordernooddeven centpercent">';
  1139. // Subject
  1140. 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>';
  1141. $trackid = ''; // TODO To avoid conflicts with 2 mass emailing, we should set a trackid here, even if we use another one into email header.
  1142. dol_init_file_process($upload_dir, $trackid);
  1143. // Joined files
  1144. $addfileaction = 'addfile';
  1145. print '<tr><td>'.$langs->trans("MailFile").'</td>';
  1146. print '<td colspan="3">';
  1147. // List of files
  1148. $listofpaths = dol_dir_list($upload_dir, 'all', 0, '', '', 'name', SORT_ASC, 0);
  1149. // TODO Trick to have param removedfile containing nb of image to delete. But this does not works without javascript
  1150. $out .= '<input type="hidden" class="removedfilehidden" name="removedfile" value="">'."\n";
  1151. $out .= '<script type="text/javascript">';
  1152. $out .= 'jQuery(document).ready(function () {';
  1153. $out .= ' jQuery(".removedfile").click(function() {';
  1154. $out .= ' jQuery(".removedfilehidden").val(jQuery(this).val());';
  1155. $out .= ' });';
  1156. $out .= '})';
  1157. $out .= '</script>'."\n";
  1158. if (count($listofpaths)) {
  1159. foreach ($listofpaths as $key => $val) {
  1160. $out .= '<div id="attachfile_'.$key.'">';
  1161. $out .= img_mime($listofpaths[$key]['name']).' '.$listofpaths[$key]['name'];
  1162. $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.'" />';
  1163. $out .= '<br></div>';
  1164. }
  1165. } else {
  1166. //$out .= '<span class="opacitymedium">'.$langs->trans("NoAttachedFiles").'</span><br>';
  1167. }
  1168. // Add link to add file
  1169. $maxfilesizearray = getMaxFileSizeArray();
  1170. $maxmin = $maxfilesizearray['maxmin'];
  1171. if ($maxmin > 0) {
  1172. $out .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
  1173. }
  1174. $out .= '<input type="file" class="flat" id="addedfile" name="addedfile" value="'.$langs->trans("Upload").'" />';
  1175. $out .= ' ';
  1176. $out .= '<input type="submit" class="button smallpaddingimp" id="'.$addfileaction.'" name="'.$addfileaction.'" value="'.$langs->trans("MailingAddFile").'" />';
  1177. print $out;
  1178. print '</td></tr>';
  1179. // Background color
  1180. print '<tr><td>'.$langs->trans("BackgroundColorByDefault").'</td><td colspan="3">';
  1181. print $htmlother->selectColor($object->bgcolor, 'bgcolor', '', 0);
  1182. print '</td></tr>';
  1183. print '</table>';
  1184. // Message
  1185. print '<div style="padding-top: 10px">';
  1186. if ($action == 'edit') {
  1187. // wysiwyg editor
  1188. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1189. $doleditor = new DolEditor('bodyemail', $object->body, '', 600, 'dolibarr_mailings', '', true, true, getDolGlobalInt('FCKEDITOR_ENABLE_MAILING'), 20, '90%');
  1190. $doleditor->Create();
  1191. }
  1192. if ($action == 'edithtml') {
  1193. // HTML source editor
  1194. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1195. $doleditor = new DolEditor('bodyemail', $object->body, '', 600, 'dolibarr_mailings', '', true, true, 'ace', 20, '90%');
  1196. $doleditor->Create(0, '', false, 'HTML Source', 'php');
  1197. }
  1198. print '</div>';
  1199. print dol_get_fiche_end();
  1200. print '<div class="center">';
  1201. print '<input type="submit" class="button buttonforacesave button-save" value="'.$langs->trans("Save").'" name="save">';
  1202. print '&nbsp; &nbsp; &nbsp;';
  1203. print '<input type="submit" class="button button-cancel" value="'.$langs->trans("Cancel").'" name="cancel">';
  1204. print '</div>';
  1205. print '</form>';
  1206. print '<br>';
  1207. }
  1208. } else {
  1209. dol_print_error($db, $object->error);
  1210. }
  1211. }
  1212. // End of page
  1213. llxFooter();
  1214. $db->close();