2005-02-15 22:16:10 +01:00
#!/usr/bin/php
2005-01-05 12:03:06 +01:00
< ? PHP
/* Copyright ( C ) 2004 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2005-02-15 22:16:10 +01:00
* Copyright ( C ) 2005 Laurent Destailleur < eldy @ users . sourceforge . net >
2005-01-05 12:03:06 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*
* $Id $
* $Source $
*
*
2005-02-15 22:16:10 +01:00
* !!! Envoi mailing !!!
2005-01-05 12:03:06 +01:00
*
* L 'utilisation d' adresses de courriers <EFBFBD> lectroniques dans les op<EFBFBD> rations
* de prospection commerciale est subordonn<EFBFBD> e au recueil du consentement
* pr<EFBFBD> alable des personnes concern<EFBFBD> es .
*
* Le dispositif juridique applicable a <EFBFBD> t<EFBFBD> introduit par l ' article 22 de
* la loi du 21 juin 2004 pour la confiance dans l ' <EFBFBD> conomie num<EFBFBD> rique .
*
* Les dispositions applicables sont d<EFBFBD> finies par les articles L . 34 - 5 du
* code des postes et des t<EFBFBD> l<EFBFBD> communications et L . 121 - 20 - 5 du code de la
* consommation . L ' application du principe du consentement pr<EFBFBD> alable en
* droit fran<EFBFBD> ais r<EFBFBD> sulte de la transposition de l ' article 13 de la Directive
* europ<EFBFBD> enne du 12 juillet 2002 <EFBFBD> Vie priv<EFBFBD> e et communications <EFBFBD> lectroniques <EFBFBD> .
*/
2005-04-21 01:01:30 +02:00
2005-02-15 22:16:10 +01:00
// Test si mode batch
2005-04-21 01:01:30 +02:00
$sapi_type = php_sapi_name ();
if ( substr ( $sapi_type , 0 , 3 ) == 'cgi' ) {
echo " Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer mailing-send.php en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI. \n " ;
2005-02-15 22:16:10 +01:00
exit ;
}
if ( ! $argv [ 1 ]) {
2005-08-11 21:23:04 +02:00
print " Usage: mailing-send.php ID_MAILING \n " ;
2005-02-15 22:16:10 +01:00
exit ;
}
$id = $argv [ 1 ];
2005-01-05 12:03:06 +01:00
2005-08-11 21:23:04 +02:00
// Recupere root dolibarr
$path = eregi_replace ( 'mailing-send.php' , '' , $_SERVER [ " PHP_SELF " ]);
require_once ( $path . " ../htdocs/master.inc.php " );
require_once ( DOL_DOCUMENT_ROOT . " /lib/CMailFile.class.php " );
2005-01-05 12:03:06 +01:00
$error = 0 ;
2005-04-21 01:01:30 +02:00
// On r<> cup<75> re donn<6E> es du mail
2005-01-05 12:03:06 +01:00
$sql = " SELECT m.rowid, m.titre, m.sujet, m.body " ;
$sql .= " , m.email_from, m.email_replyto, m.email_errorsto " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " mailing as m " ;
2005-02-15 22:16:10 +01:00
$sql .= " WHERE m.statut >= 1 " ;
$sql .= " AND m.rowid= " . $id ;
2005-01-05 12:03:06 +01:00
$sql .= " LIMIT 1 " ;
2005-04-21 01:01:30 +02:00
$resql = $db -> query ( $sql );
if ( $resql )
2005-01-05 12:03:06 +01:00
{
2005-04-21 01:01:30 +02:00
$num = $db -> num_rows ( $resql );
2005-01-05 12:03:06 +01:00
$i = 0 ;
if ( $num == 1 )
{
2005-04-21 01:01:30 +02:00
$obj = $db -> fetch_object ( $resql );
2005-01-05 12:03:06 +01:00
2005-02-15 22:16:10 +01:00
dolibarr_syslog ( " mailing-send: mailing " . $id );
2005-01-05 12:03:06 +01:00
$id = $obj -> rowid ;
$subject = $obj -> sujet ;
$message = $obj -> body ;
$from = $obj -> email_from ;
$errorsto = $obj -> email_errorsto ;
$i ++ ;
}
}
2005-04-21 01:01:30 +02:00
$nbok = 0 ; $nbko = 0 ;
// On choisit les mails non d<> j<EFBFBD> envoy<6F> s pour ce mailing (statut=0)
// ou envoy<6F> s en erreur (statut=-1)
$sql = " SELECT mc.rowid, mc.nom, mc.prenom, mc.email " ;
2005-01-05 12:03:06 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " mailing_cibles as mc " ;
2005-04-21 01:01:30 +02:00
$sql .= " WHERE mc.statut < 1 AND mc.fk_mailing = " . $id ;
2005-01-05 12:03:06 +01:00
2005-04-21 01:01:30 +02:00
$resql = $db -> query ( $sql );
if ( $resql )
2005-01-05 12:03:06 +01:00
{
2005-04-21 01:01:30 +02:00
$num = $db -> num_rows ( $resql );
2005-01-05 12:03:06 +01:00
2005-04-21 01:01:30 +02:00
if ( $num )
2005-01-05 12:03:06 +01:00
{
2005-08-11 21:23:04 +02:00
dolibarr_syslog ( " mailing-send: target number = $num " );
2005-04-21 01:01:30 +02:00
// Positionne date debut envoi
2005-07-25 11:33:05 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing SET date_envoi=SYSDATE() WHERE rowid= " . $id ;
2005-04-21 01:01:30 +02:00
$resql2 = $db -> query ( $sql );
if ( ! $resql2 )
{
dolibarr_print_error ( $db );
}
// Boucle sur chaque adresse et envoie le mail
$i = 0 ;
while ( $i < $num )
{
$obj = $db -> fetch_object ( $resql );
$sendto = stripslashes ( $obj -> prenom ) . " " . stripslashes ( $obj -> nom ) . " < " . $obj -> email . " > " ;
2005-08-11 21:23:04 +02:00
$mail = new CMailFile ( $subject , $sendto , $from , $message , array (), array (), array ());
2005-04-21 01:01:30 +02:00
$mail -> errors_to = $errorsto ;
if ( $mail -> sendfile () )
{
// Mail envoye avec succes
$nbok ++ ;
2005-07-25 11:33:05 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing_cibles SET statut=1, date_envoi=SYSDATE() WHERE rowid= " . $obj -> rowid ;
2005-04-21 01:01:30 +02:00
$resql2 = $db -> query ( $sql );
if ( ! $resql2 )
{
dolibarr_print_error ( $db );
}
}
else
{
// Mail en echec
$nbko ++ ;
2005-08-11 21:23:04 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing_cibles SET statut=-1, date_envoi=SYSDATE() WHERE rowid= " . $obj -> rowid ;
2005-04-21 01:01:30 +02:00
$resql2 = $db -> query ( $sql );
if ( ! $resql2 )
{
dolibarr_print_error ( $db );
}
}
$i ++ ;
}
2005-08-11 21:23:04 +02:00
}
2005-04-21 01:01:30 +02:00
2005-08-11 21:23:04 +02:00
// Met a jour statut global du mail
$statut = 2 ;
if ( ! $nbko ) $statut = 3 ;
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing SET statut= " . $statut . " WHERE rowid= " . $id ;
$resql2 = $db -> query ( $sql );
if ( ! $resql2 )
{
dolibarr_print_error ( $db );
2005-01-05 12:03:06 +01:00
}
}
else
{
2005-04-21 01:01:30 +02:00
dolibarr_syslog ( $db -> error ());
dolibarr_print_error ( $db );
2005-01-05 12:03:06 +01:00
}
?>