mailing-send.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  5. * Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  6. * Copyright (C) 2005-2016 Regis Houssin <regis.houssin@inodbox.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file scripts/emailings/mailing-send.php
  23. * \ingroup mailing
  24. * \brief Script d'envoi d'un mailing prepare et valide
  25. */
  26. $sapi_type = php_sapi_name();
  27. $script_file = basename(__FILE__);
  28. $path=dirname(__FILE__).'/';
  29. // Test if batch mode
  30. if (substr($sapi_type, 0, 3) == 'cgi') {
  31. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  32. exit(-1);
  33. }
  34. if (! isset($argv[1]) || ! $argv[1]) {
  35. print "Usage: ".$script_file." (ID_MAILING|all)\n";
  36. exit(-1);
  37. }
  38. $id=$argv[1];
  39. if (isset($argv[2]) || !empty($argv[2])) $login = $argv[2];
  40. else $login = '';
  41. require_once $path."../../htdocs/master.inc.php";
  42. require_once DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php";
  43. require_once DOL_DOCUMENT_ROOT."/comm/mailing/class/mailing.class.php";
  44. // Global variables
  45. $version=DOL_VERSION;
  46. $error=0;
  47. /*
  48. * Main
  49. */
  50. @set_time_limit(0);
  51. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  52. if ($conf->global->MAILING_LIMIT_SENDBYCLI == '-1')
  53. {
  54. }
  55. $user = new User($db);
  56. // for signature, we use user send as parameter
  57. if (! empty($login)) $user->fetch('',$login);
  58. // We get list of emailing id to process
  59. $sql = "SELECT m.rowid";
  60. $sql.= " FROM ".MAIN_DB_PREFIX."mailing as m";
  61. $sql.= " WHERE m.statut IN (1,2)";
  62. if ($id != 'all')
  63. {
  64. $sql.= " AND m.rowid= ".$id;
  65. $sql.= " LIMIT 1";
  66. }
  67. $resql=$db->query($sql);
  68. if ($resql)
  69. {
  70. $num = $db->num_rows($resql);
  71. $j = 0;
  72. if ($num)
  73. {
  74. for ($j=0; $j<$num; $j++)
  75. {
  76. $obj = $db->fetch_object($resql);
  77. dol_syslog("Process mailing with id ".$obj->rowid);
  78. print "Process mailing with id ".$obj->rowid."\n";
  79. $emailing = new Mailing($db);
  80. $emailing->fetch($obj->rowid);
  81. $id = $emailing->id;
  82. $subject = $emailing->sujet;
  83. $message = $emailing->body;
  84. $from = $emailing->email_from;
  85. $replyto = $emailing->email_replyto;
  86. $errorsto = $emailing->email_errorsto;
  87. // Le message est-il en html
  88. $msgishtml=-1; // Unknown by default
  89. if (preg_match('/[\s\t]*<html>/i',$message)) $msgishtml=1;
  90. $nbok=0; $nbko=0;
  91. // On choisit les mails non deja envoyes pour ce mailing (statut=0)
  92. // ou envoyes en erreur (statut=-1)
  93. $sql2 = "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";
  94. $sql2.= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
  95. $sql2.= " WHERE mc.statut < 1 AND mc.fk_mailing = ".$id;
  96. if ($conf->global->MAILING_LIMIT_SENDBYCLI > 0)
  97. {
  98. $sql2.= " LIMIT ".$conf->global->MAILING_LIMIT_SENDBYCLI;
  99. }
  100. $resql2=$db->query($sql2);
  101. if ($resql2)
  102. {
  103. $num2 = $db->num_rows($resql2);
  104. dol_syslog("Nb of targets = ".$num2, LOG_DEBUG);
  105. print "Nb of targets = ".$num2."\n";
  106. if ($num2)
  107. {
  108. $now=dol_now();
  109. // Positionne date debut envoi
  110. $sqlstartdate="UPDATE ".MAIN_DB_PREFIX."mailing SET date_envoi='".$db->idate($now)."' WHERE rowid=".$id;
  111. $resqlstartdate=$db->query($sqlstartdate);
  112. if (! $resqlstartdate)
  113. {
  114. dol_print_error($db);
  115. $error++;
  116. }
  117. // Look on each email and sent message
  118. $i = 0;
  119. while ($i < $num2)
  120. {
  121. // Here code is common with same loop ino card.php
  122. $res=1;
  123. $now=dol_now();
  124. $obj = $db->fetch_object($resql2);
  125. // sendto en RFC2822
  126. $sendto = str_replace(',',' ',dolGetFirstLastname($obj->firstname, $obj->lastname) ." <".$obj->email.">");
  127. // Make subtsitutions on topic and body
  128. $other=explode(';',$obj->other);
  129. $tmpfield=explode('=',$other[0],2); $other1=(isset($tmpfield[1])?$tmpfield[1]:$tmpfield[0]);
  130. $tmpfield=explode('=',$other[1],2); $other2=(isset($tmpfield[1])?$tmpfield[1]:$tmpfield[0]);
  131. $tmpfield=explode('=',$other[2],2); $other3=(isset($tmpfield[1])?$tmpfield[1]:$tmpfield[0]);
  132. $tmpfield=explode('=',$other[3],2); $other4=(isset($tmpfield[1])?$tmpfield[1]:$tmpfield[0]);
  133. $tmpfield=explode('=',$other[4],2); $other5=(isset($tmpfield[1])?$tmpfield[1]:$tmpfield[0]);
  134. $signature = ((!empty($user->signature) && empty($conf->global->MAIN_MAIL_DO_NOT_USE_SIGN))?$user->signature:'');
  135. $object = null; // Not defined with mass emailing
  136. $parameters=array('mode'=>'emailing');
  137. $substitutionarray=getCommonSubstitutionArray($langs, 0, array('object','objectamount'), $object); // Note: On mass emailing, this is null because we don't know object
  138. // Array of possible substitutions (See also file mailing-send.php that should manage same substitutions)
  139. $substitutionarray['__ID__'] = $obj->source_id;
  140. $substitutionarray['__EMAIL__'] = $obj->email;
  141. $substitutionarray['__LASTNAME__'] = $obj->lastname;
  142. $substitutionarray['__FIRSTNAME__'] = $obj->firstname;
  143. $substitutionarray['__MAILTOEMAIL__'] = '<a href="mailto:'.$obj->email.'">'.$obj->email.'</a>';
  144. $substitutionarray['__OTHER1__'] = $other1;
  145. $substitutionarray['__OTHER2__'] = $other2;
  146. $substitutionarray['__OTHER3__'] = $other3;
  147. $substitutionarray['__OTHER4__'] = $other4;
  148. $substitutionarray['__OTHER5__'] = $other5;
  149. $substitutionarray['__USER_SIGNATURE__'] = $signature; // Signature is empty when ran from command line or taken from user in parameter)
  150. $substitutionarray['__SIGNATURE__'] = $signature; // For backward compatibility
  151. $substitutionarray['__CHECK_READ__'] = '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag='.$obj->tag.'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" width="1" height="1" style="width:1px;height:1px" border="0"/>';
  152. $substitutionarray['__UNSUBSCRIBE__'] = '<a href="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-unsubscribe.php?tag='.$obj->tag.'&unsuscrib=1&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" target="_blank">'.$langs->trans("MailUnsubcribe").'</a>';
  153. $onlinepaymentenabled = 0;
  154. if (! empty($conf->paypal->enabled)) $onlinepaymentenabled++;
  155. if (! empty($conf->paybox->enabled)) $onlinepaymentenabled++;
  156. if (! empty($conf->stripe->enabled)) $onlinepaymentenabled++;
  157. if ($onlinepaymentenabled && ! empty($conf->global->PAYMENT_SECURITY_TOKEN))
  158. {
  159. $substitutionarray['__SECUREKEYPAYMENT__']=dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
  160. if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE))
  161. {
  162. $substitutionarray['__SECUREKEYPAYMENT_MEMBER__']=dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
  163. $substitutionarray['__SECUREKEYPAYMENT_ORDER__']=dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
  164. $substitutionarray['__SECUREKEYPAYMENT_INVOICE__']=dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
  165. $substitutionarray['__SECUREKEYPAYMENT_CONTRACTLINE__']=dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
  166. }
  167. else
  168. {
  169. $substitutionarray['__SECUREKEYPAYMENT_MEMBER__']=dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . 'membersubscription' . $obj->source_id, 2);
  170. $substitutionarray['__SECUREKEYPAYMENT_ORDER__']=dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . 'order' . $obj->source_id, 2);
  171. $substitutionarray['__SECUREKEYPAYMENT_INVOICE__']=dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . 'invoice' . $obj->source_id, 2);
  172. $substitutionarray['__SECUREKEYPAYMENT_CONTRACTLINE__']=dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . 'contractline' . $obj->source_id, 2);
  173. }
  174. }
  175. /* For backward compatibility */
  176. if (! empty($conf->paypal->enabled) && ! empty($conf->global->PAYPAL_SECURITY_TOKEN))
  177. {
  178. $substitutionarray['__SECUREKEYPAYPAL__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2);
  179. if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $substitutionarray['__SECUREKEYPAYPAL_MEMBER__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2);
  180. else $substitutionarray['__SECUREKEYPAYPAL_MEMBER__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'membersubscription' . $obj->source_id, 2);
  181. if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $substitutionarray['__SECUREKEYPAYPAL_ORDER__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2);
  182. else $substitutionarray['__SECUREKEYPAYPAL_ORDER__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'order' . $obj->source_id, 2);
  183. if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $substitutionarray['__SECUREKEYPAYPAL_INVOICE__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2);
  184. else $substitutionarray['__SECUREKEYPAYPAL_INVOICE__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'invoice' . $obj->source_id, 2);
  185. if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $substitutionarray['__SECUREKEYPAYPAL_CONTRACTLINE__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2);
  186. else $substitutionarray['__SECUREKEYPAYPAL_CONTRACTLINE__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'contractline' . $obj->source_id, 2);
  187. }
  188. complete_substitutions_array($substitutionarray,$langs);
  189. $newsubject=make_substitutions($subject,$substitutionarray);
  190. $newmessage=make_substitutions($message,$substitutionarray);
  191. $substitutionisok=true;
  192. // Fabrication du mail
  193. $trackid='emailing-'.$obj->fk_mailing.'-'.$obj->rowid;
  194. $mail = new CMailFile(
  195. $newsubject,
  196. $sendto,
  197. $from,
  198. $newmessage,
  199. array(),
  200. array(),
  201. array(),
  202. '',
  203. '',
  204. 0,
  205. $msgishtml,
  206. $errorsto,
  207. '',
  208. $trackid,
  209. '',
  210. 'emailing'
  211. );
  212. if ($mail->error)
  213. {
  214. $res=0;
  215. }
  216. if (! $substitutionisok)
  217. {
  218. $mail->error='Some substitution failed';
  219. $res=0;
  220. }
  221. // Send Email
  222. if ($res)
  223. {
  224. $res=$mail->sendfile();
  225. }
  226. if ($res)
  227. {
  228. // Mail successful
  229. $nbok++;
  230. dol_syslog("ok for emailing id ".$id." #".$i.($mail->error?' - '.$mail->error:''), LOG_DEBUG);
  231. // Note: If emailing is 100 000 targets, 100 000 entries are added, so we don't enter events for each target here
  232. // We must union table llx_mailing_taget for event tab OR enter 1 event with a special table link (id of email in event)
  233. // Run trigger
  234. /*
  235. if ($obj->source_type == 'contact')
  236. {
  237. $emailing->sendtoid = $obj->source_id;
  238. }
  239. if ($obj->source_type == 'thirdparty')
  240. {
  241. $emailing->socid = $obj->source_id;
  242. }
  243. // Call trigger
  244. $result=$emailing->call_trigger('EMAILING_SENTBYMAIL',$user);
  245. if ($result < 0) $error++;
  246. // End call triggers
  247. */
  248. $sqlok ="UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  249. $sqlok.=" SET statut=1, date_envoi='".$db->idate($now)."' WHERE rowid=".$obj->rowid;
  250. $resqlok=$db->query($sqlok);
  251. if (! $resqlok)
  252. {
  253. dol_print_error($db);
  254. $error++;
  255. }
  256. else
  257. {
  258. //if cheack read is use then update prospect contact status
  259. if (strpos($message, '__CHECK_READ__') !== false)
  260. {
  261. //Update status communication of thirdparty prospect
  262. $sqlx = "UPDATE ".MAIN_DB_PREFIX."societe SET fk_stcomm=2 WHERE rowid IN (SELECT source_id FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE rowid=".$obj->rowid.")";
  263. dol_syslog("card.php: set prospect thirdparty status", LOG_DEBUG);
  264. $resqlx=$db->query($sqlx);
  265. if (! $resqlx)
  266. {
  267. dol_print_error($db);
  268. $error++;
  269. }
  270. //Update status communication of contact prospect
  271. $sqlx = "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=".$obj->rowid." AND mc.source_type = 'contact' AND mc.source_id = sc.rowid)";
  272. dol_syslog("card.php: set prospect contact status", LOG_DEBUG);
  273. $resqlx=$db->query($sqlx);
  274. if (! $resqlx)
  275. {
  276. dol_print_error($db);
  277. $error++;
  278. }
  279. }
  280. if (!empty($conf->global->MAILING_DELAY)) {
  281. sleep($conf->global->MAILING_DELAY);
  282. }
  283. }
  284. }
  285. else
  286. {
  287. // Mail failed
  288. $nbko++;
  289. dol_syslog("error for emailing id ".$id." #".$i.($mail->error?' - '.$mail->error:''), LOG_DEBUG);
  290. $sqlerror="UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  291. $sqlerror.=" SET statut=-1, date_envoi=".$db->idate($now)." WHERE rowid=".$obj->rowid;
  292. $resqlerror=$db->query($sqlerror);
  293. if (! $resqlerror)
  294. {
  295. dol_print_error($db);
  296. $error++;
  297. }
  298. }
  299. $i++;
  300. }
  301. }
  302. else
  303. {
  304. $mesg="Emailing id ".$id." has no recipient to target";
  305. print $mesg."\n";
  306. dol_syslog($mesg,LOG_ERR);
  307. }
  308. // Loop finished, set global statut of mail
  309. $statut=2;
  310. if (! $nbko) $statut=3;
  311. $sqlenddate="UPDATE ".MAIN_DB_PREFIX."mailing SET statut=".$statut." WHERE rowid=".$id;
  312. dol_syslog("update global status", LOG_DEBUG);
  313. print "Update status of emailing id ".$id." to ".$statut."\n";
  314. $resqlenddate=$db->query($sqlenddate);
  315. if (! $resqlenddate)
  316. {
  317. dol_print_error($db);
  318. $error++;
  319. }
  320. }
  321. else
  322. {
  323. dol_print_error($db);
  324. $error++;
  325. }
  326. }
  327. }
  328. else
  329. {
  330. $mesg="No validated emailing id to send found.";
  331. print $mesg."\n";
  332. dol_syslog($mesg,LOG_ERR);
  333. $error++;
  334. }
  335. }
  336. else
  337. {
  338. dol_print_error($db);
  339. $error++;
  340. }
  341. exit($error);