mailing-send.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/usr/bin/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. *
  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 <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file scripts/emailings/mailing-send.php
  22. * \ingroup mailing
  23. * \brief Script d'envoi d'un mailing prepare et valide
  24. */
  25. $sapi_type = php_sapi_name();
  26. $script_file = basename(__FILE__);
  27. $path=dirname(__FILE__).'/';
  28. // Test if batch mode
  29. if (substr($sapi_type, 0, 3) == 'cgi') {
  30. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  31. exit(-1);
  32. }
  33. if (! isset($argv[1]) || ! $argv[1]) {
  34. print "Usage: ".$script_file." (ID_MAILING|all)\n";
  35. exit(-1);
  36. }
  37. $id=$argv[1];
  38. require_once ($path."../../htdocs/master.inc.php");
  39. require_once (DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php");
  40. // Global variables
  41. $version=DOL_VERSION;
  42. $error=0;
  43. /*
  44. * Main
  45. */
  46. @set_time_limit(0);
  47. print "***** ".$script_file." (".$version.") pid=".getmypid()." *****\n";
  48. // We get list of emailing to process
  49. $sql = "SELECT m.rowid, m.titre, m.sujet, m.body,";
  50. $sql.= " m.email_from, m.email_replyto, m.email_errorsto";
  51. $sql.= " FROM ".MAIN_DB_PREFIX."mailing as m";
  52. $sql.= " WHERE m.statut = 1";
  53. if ($id != 'all')
  54. {
  55. $sql.= " AND m.rowid= ".$id;
  56. $sql.= " LIMIT 1";
  57. }
  58. dol_syslog("sql=".$sql);
  59. $resql=$db->query($sql);
  60. if ($resql)
  61. {
  62. $num = $db->num_rows($resql);
  63. $j = 0;
  64. if ($num)
  65. {
  66. for ($j=0; $j<$num; $j++)
  67. {
  68. $obj = $db->fetch_object($resql);
  69. dol_syslog("Process mailing with id ".$obj->rowid);
  70. print "Process mailing with id ".$obj->rowid."\n";
  71. $id = $obj->rowid;
  72. $subject = $obj->sujet;
  73. $message = $obj->body;
  74. $from = $obj->email_from;
  75. $replyto = $obj->email_replyto;
  76. $errorsto = $obj->email_errorsto;
  77. // Le message est-il en html
  78. $msgishtml=-1; // Unknown by default
  79. if (preg_match('/[\s\t]*<html>/i',$message)) $msgishtml=1;
  80. $nbok=0; $nbko=0;
  81. // On choisit les mails non deja envoyes pour ce mailing (statut=0)
  82. // ou envoyes en erreur (statut=-1)
  83. $sql2 = "SELECT mc.rowid, mc.lastname as lastname, mc.firstname as firstname, mc.email, mc.other, mc.source_url, mc.source_id, mc.source_type, mc.tag";
  84. $sql2.= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
  85. $sql2.= " WHERE mc.statut < 1 AND mc.fk_mailing = ".$id;
  86. $resql2=$db->query($sql2);
  87. if ($resql2)
  88. {
  89. $num2 = $db->num_rows($resql2);
  90. dol_syslog("Nb of targets = ".$num2, LOG_DEBUG);
  91. print "Nb of targets = ".$num2."\n";
  92. if ($num2)
  93. {
  94. $now=dol_now();
  95. // Positionne date debut envoi
  96. $sqlstartdate="UPDATE ".MAIN_DB_PREFIX."mailing SET date_envoi='".$db->idate($now)."' WHERE rowid=".$id;
  97. $resqlstartdate=$db->query($sqlstartdate);
  98. if (! $resqlstartdate)
  99. {
  100. dol_print_error($db);
  101. $error++;
  102. }
  103. // Look on each email and sent message
  104. $i = 0;
  105. while ($i < $num2)
  106. {
  107. $res=1;
  108. $now=dol_now();
  109. $obj2 = $db->fetch_object($resql2);
  110. // sendto en RFC2822
  111. $sendto = str_replace(',',' ',dolGetFirstLastname($obj2->firstname, $obj2->lastname) ." <".$obj2->email.">");
  112. // Make subtsitutions on topic and body
  113. $other=explode(';',$obj2->other);
  114. $other1=$other[0];
  115. $other2=$other[1];
  116. $other3=$other[2];
  117. $other4=$other[3];
  118. $other5=$other[4];
  119. $substitutionarray=array(
  120. '__ID__' => $obj->source_id,
  121. '__EMAIL__' => $obj->email,
  122. '__CHECK_READ__' => '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag='.$obj2->tag.'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" width="1" height="1" style="width:1px;height:1px" border="0"/>',
  123. '__UNSUBSCRIBE__' => '<a href="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-unsubscribe.php?tag='.$obj2->tag.'&unsuscrib=1&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" target="_blank">'.$langs->trans("MailUnsubcribe").'</a>',
  124. '__MAILTOEMAIL__' => '<a href="mailto:'.$obj2->email.'">'.$obj2->email.'</a>',
  125. '__LASTNAME__' => $obj2->lastname,
  126. '__FIRSTNAME__' => $obj2->firstname,
  127. '__OTHER1__' => $other1,
  128. '__OTHER2__' => $other2,
  129. '__OTHER3__' => $other3,
  130. '__OTHER4__' => $other4,
  131. '__OTHER5__' => $other5
  132. );
  133. complete_substitutions_array($substitutionarray,$langs);
  134. $newsubject=make_substitutions($subject,$substitutionarray);
  135. $newmessage=make_substitutions($message,$substitutionarray);
  136. $substitutionisok=true;
  137. // Fabrication du mail
  138. $mail = new CMailFile(
  139. $newsubject,
  140. $sendto,
  141. $from,
  142. $newmessage,
  143. array(),
  144. array(),
  145. array(),
  146. '',
  147. '',
  148. 0,
  149. $msgishtml,
  150. $errorsto
  151. );
  152. if ($mail->error)
  153. {
  154. $res=0;
  155. }
  156. if (! $substitutionisok)
  157. {
  158. $mail->error='Some substitution failed';
  159. $res=0;
  160. }
  161. // Send Email
  162. if ($res)
  163. {
  164. $res=$mail->sendfile();
  165. }
  166. if ($res)
  167. {
  168. // Mail successful
  169. $nbok++;
  170. dol_syslog("ok for emailing id ".$id." #".$i.($mail->error?' - '.$mail->error:''), LOG_DEBUG);
  171. $sqlok ="UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  172. $sqlok.=" SET statut=1, date_envoi='".$db->idate($now)."' WHERE rowid=".$obj2->rowid;
  173. $resqlok=$db->query($sqlok);
  174. if (! $resqlok)
  175. {
  176. dol_print_error($db);
  177. $error++;
  178. }
  179. else
  180. {
  181. //if cheack read is use then update prospect contact status
  182. if (strpos($message, '__CHECK_READ__') !== false)
  183. {
  184. //Update status communication of thirdparty prospect
  185. $sqlx = "UPDATE ".MAIN_DB_PREFIX."societe SET fk_stcomm=2 WHERE rowid IN (SELECT source_id FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE rowid=".$obj2->rowid.")";
  186. dol_syslog("fiche.php: set prospect thirdparty status sql=".$sql, LOG_DEBUG);
  187. $resqlx=$db->query($sqlx);
  188. if (! $resqlx)
  189. {
  190. dol_print_error($db);
  191. $error++;
  192. }
  193. //Update status communication of contact prospect
  194. $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=".$obj2->rowid." AND mc.source_type = 'contact' AND mc.source_id = sc.rowid)";
  195. dol_syslog("fiche.php: set prospect contact status sql=".$sql, LOG_DEBUG);
  196. $resqlx=$db->query($sqlx);
  197. if (! $resqlx)
  198. {
  199. dol_print_error($db);
  200. $error++;
  201. }
  202. }
  203. }
  204. }
  205. else
  206. {
  207. // Mail failed
  208. $nbko++;
  209. dol_syslog("error for emailing id ".$id." #".$i.($mail->error?' - '.$mail->error:''), LOG_DEBUG);
  210. $sqlerror="UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  211. $sqlerror.=" SET statut=-1, date_envoi=".$db->idate($now)." WHERE rowid=".$obj2->rowid;
  212. $resqlerror=$db->query($sqlerror);
  213. if (! $resqlerror)
  214. {
  215. dol_print_error($db);
  216. $error++;
  217. }
  218. }
  219. $i++;
  220. }
  221. }
  222. else
  223. {
  224. $mesg="Emailing id ".$id." has no recipient to target";
  225. print $mesg."\n";
  226. dol_syslog($mesg,LOG_ERR);
  227. }
  228. // Loop finished, set global statut of mail
  229. $statut=2;
  230. if (! $nbko) $statut=3;
  231. $sqlenddate="UPDATE ".MAIN_DB_PREFIX."mailing SET statut=".$statut." WHERE rowid=".$id;
  232. dol_syslog("update global status sql=".$sqlenddate, LOG_DEBUG);
  233. print "Update status of emailing id ".$id." to ".$statut."\n";
  234. $resqlenddate=$db->query($sqlenddate);
  235. if (! $resqlenddate)
  236. {
  237. dol_print_error($db);
  238. $error++;
  239. }
  240. }
  241. else
  242. {
  243. dol_print_error($db);
  244. $error++;
  245. }
  246. }
  247. }
  248. else
  249. {
  250. $mesg="No validated emailing id to send found.";
  251. print $mesg."\n";
  252. dol_syslog($mesg,LOG_ERR);
  253. $error++;
  254. }
  255. }
  256. else
  257. {
  258. dol_print_error($db);
  259. $error++;
  260. }
  261. exit($error);
  262. ?>