2005-02-15 22:16:10 +01:00
#!/usr/bin/php
2011-01-23 02:00:45 +01:00
< ? php
2011-08-31 12:20:39 +02:00
/*
2010-11-20 17:25:23 +01:00
* Copyright ( C ) 2004 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2013-04-14 20:34:31 +02:00
* Copyright ( C ) 2005 - 2013 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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2005-01-05 12:03:06 +01:00
* ( 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
2011-08-01 00:21:57 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2005-01-05 12:03:06 +01:00
*/
2005-04-21 01:01:30 +02:00
2005-09-02 23:34:22 +02:00
/**
2009-09-08 16:28:08 +02:00
* \file scripts / emailings / mailing - send . php
2009-05-25 00:11:46 +02:00
* \ingroup mailing
* \brief Script d 'envoi d' un mailing prepare et valide
*/
2005-09-02 23:34:22 +02:00
2005-04-21 01:01:30 +02:00
$sapi_type = php_sapi_name ();
2009-10-22 03:04:23 +02:00
$script_file = basename ( __FILE__ );
2010-03-16 19:46:28 +01:00
$path = dirname ( __FILE__ ) . '/' ;
2009-10-22 03:04:23 +02:00
// Test if batch mode
2005-04-21 01:01:30 +02:00
if ( substr ( $sapi_type , 0 , 3 ) == 'cgi' ) {
2009-11-09 15:14:43 +01:00
echo " Error: You are using PHP for CGI. To execute " . $script_file . " from command line, you must use PHP for CLI mode. \n " ;
2013-06-09 14:40:33 +02:00
exit ( - 1 );
2005-02-15 22:16:10 +01:00
}
2005-09-02 23:34:22 +02:00
if ( ! isset ( $argv [ 1 ]) || ! $argv [ 1 ]) {
2013-04-14 20:15:55 +02:00
print " Usage: " . $script_file . " (ID_MAILING|all) \n " ;
2013-06-09 14:40:33 +02:00
exit ( - 1 );
2005-02-15 22:16:10 +01:00
}
$id = $argv [ 1 ];
2005-01-05 12:03:06 +01:00
2009-12-16 11:23:38 +01:00
require_once ( $path . " ../../htdocs/master.inc.php " );
2011-10-24 09:29:17 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /core/class/CMailFile.class.php " );
2005-08-11 21:23:04 +02:00
2005-01-05 12:03:06 +01:00
2013-05-15 11:24:18 +02:00
// Global variables
$version = DOL_VERSION ;
$error = 0 ;
/*
* Main
*/
@ set_time_limit ( 0 );
2013-05-22 12:23:42 +02:00
print " ***** " . $script_file . " ( " . $version . " ) pid= " . getmypid () . " ***** \n " ;
2013-05-15 11:24:18 +02:00
2005-01-05 12:03:06 +01:00
2005-04-21 01:01:30 +02:00
2013-04-14 20:15:55 +02:00
// We get list of emailing to process
2009-05-25 00:11:46 +02: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 " ;
2013-04-14 20:15:55 +02:00
$sql .= " WHERE m.statut = 1 " ;
if ( $id != 'all' )
2005-01-05 12:03:06 +01:00
{
2013-04-14 20:15:55 +02:00
$sql .= " AND m.rowid= " . $id ;
$sql .= " LIMIT 1 " ;
2005-01-05 12:03:06 +01:00
}
2013-04-14 20:15:55 +02:00
dol_syslog ( " sql= " . $sql );
2005-04-21 01:01:30 +02:00
$resql = $db -> query ( $sql );
if ( $resql )
2005-01-05 12:03:06 +01:00
{
2009-09-08 16:28:08 +02:00
$num = $db -> num_rows ( $resql );
2013-04-14 20:15:55 +02:00
$j = 0 ;
2005-01-05 12:03:06 +01:00
2009-09-08 16:28:08 +02:00
if ( $num )
{
2013-04-14 20:15:55 +02:00
for ( $j = 0 ; $j < $num ; $j ++ )
2009-09-08 16:28:08 +02:00
{
$obj = $db -> fetch_object ( $resql );
2005-04-21 01:01:30 +02:00
2013-04-14 20:15:55 +02:00
dol_syslog ( " Process mailing with id " . $obj -> rowid );
print " Process mailing with id " . $obj -> rowid . " \n " ;
$id = $obj -> rowid ;
$subject = $obj -> sujet ;
$message = $obj -> body ;
$from = $obj -> email_from ;
$replyto = $obj -> email_replyto ;
$errorsto = $obj -> email_errorsto ;
// Le message est-il en html
$msgishtml =- 1 ; // Unknown by default
if ( preg_match ( '/[\s\t]*<html>/i' , $message )) $msgishtml = 1 ;
$nbok = 0 ; $nbko = 0 ;
// On choisit les mails non deja envoyes pour ce mailing (statut=0)
// ou envoyes en erreur (statut=-1)
$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 " ;
$sql2 .= " FROM " . MAIN_DB_PREFIX . " mailing_cibles as mc " ;
$sql2 .= " WHERE mc.statut < 1 AND mc.fk_mailing = " . $id ;
$resql2 = $db -> query ( $sql2 );
if ( $resql2 )
2006-08-06 20:34:37 +02:00
{
2013-04-14 20:15:55 +02:00
$num2 = $db -> num_rows ( $resql2 );
2013-06-05 16:12:07 +02:00
dol_syslog ( " Nb of targets = " . $num2 , LOG_DEBUG );
print " Nb of targets = " . $num2 . " \n " ;
2006-08-06 20:34:37 +02:00
2013-04-14 20:15:55 +02:00
if ( $num2 )
{
$now = dol_now ();
2009-02-18 23:05:21 +01:00
2013-04-14 20:15:55 +02:00
// Positionne date debut envoi
$sqlstartdate = " UPDATE " . MAIN_DB_PREFIX . " mailing SET date_envoi=' " . $db -> idate ( $now ) . " ' WHERE rowid= " . $id ;
$resqlstartdate = $db -> query ( $sqlstartdate );
if ( ! $resqlstartdate )
{
dol_print_error ( $db );
2013-06-05 16:12:07 +02:00
$error ++ ;
2013-04-14 20:15:55 +02:00
}
2007-05-26 17:27:34 +02:00
2013-04-14 20:15:55 +02:00
// Look on each email and sent message
$i = 0 ;
while ( $i < $num2 )
2012-04-04 14:45:48 +02:00
{
2013-04-14 20:15:55 +02:00
$res = 1 ;
$now = dol_now ();
$obj2 = $db -> fetch_object ( $resql2 );
// sendto en RFC2822
$sendto = str_replace ( ',' , ' ' , dolGetFirstLastname ( $obj2 -> firstname , $obj2 -> lastname ) . " < " . $obj2 -> email . " > " );
// Make subtsitutions on topic and body
$other = explode ( ';' , $obj2 -> other );
$other1 = $other [ 0 ];
$other2 = $other [ 1 ];
$other3 = $other [ 2 ];
$other4 = $other [ 3 ];
$other5 = $other [ 4 ];
$substitutionarray = array (
'__ID__' => $obj -> source_id ,
'__EMAIL__' => $obj -> email ,
2013-04-14 20:34:31 +02:00
'__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"/>' ,
'__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>' ,
2013-04-14 20:15:55 +02:00
'__MAILTOEMAIL__' => '<a href="mailto:' . $obj2 -> email . '">' . $obj2 -> email . '</a>' ,
'__LASTNAME__' => $obj2 -> lastname ,
'__FIRSTNAME__' => $obj2 -> firstname ,
'__OTHER1__' => $other1 ,
'__OTHER2__' => $other2 ,
'__OTHER3__' => $other3 ,
'__OTHER4__' => $other4 ,
'__OTHER5__' => $other5
);
complete_substitutions_array ( $substitutionarray , $langs );
$newsubject = make_substitutions ( $subject , $substitutionarray );
$newmessage = make_substitutions ( $message , $substitutionarray );
$substitutionisok = true ;
// Fabrication du mail
$mail = new CMailFile (
$newsubject ,
$sendto ,
$from ,
$newmessage ,
array (),
array (),
array (),
'' ,
'' ,
0 ,
$msgishtml ,
$errorsto
);
if ( $mail -> error )
2012-04-04 14:45:48 +02:00
{
2013-04-14 20:15:55 +02:00
$res = 0 ;
}
if ( ! $substitutionisok )
{
$mail -> error = 'Some substitution failed' ;
$res = 0 ;
2012-04-04 14:45:48 +02:00
}
2012-04-05 21:02:00 +02:00
2013-04-14 20:15:55 +02:00
// Send Email
if ( $res )
{
$res = $mail -> sendfile ();
}
2012-04-05 21:02:00 +02:00
2013-04-14 20:15:55 +02:00
if ( $res )
{
// Mail successful
$nbok ++ ;
dol_syslog ( " ok for emailing id " . $id . " # " . $i . ( $mail -> error ? ' - ' . $mail -> error : '' ), LOG_DEBUG );
$sqlok = " UPDATE " . MAIN_DB_PREFIX . " mailing_cibles " ;
$sqlok .= " SET statut=1, date_envoi=' " . $db -> idate ( $now ) . " ' WHERE rowid= " . $obj2 -> rowid ;
$resqlok = $db -> query ( $sqlok );
if ( ! $resqlok )
{
dol_print_error ( $db );
2013-06-05 16:12:07 +02:00
$error ++ ;
2013-04-14 20:15:55 +02:00
}
else
{
//if cheack read is use then update prospect contact status
if ( strpos ( $message , '__CHECK_READ__' ) !== false )
{
//Update status communication of thirdparty prospect
$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 . " ) " ;
dol_syslog ( " fiche.php: set prospect thirdparty status sql= " . $sql , LOG_DEBUG );
$resqlx = $db -> query ( $sqlx );
if ( ! $resqlx )
{
dol_print_error ( $db );
2013-06-05 16:12:07 +02:00
$error ++ ;
2013-04-14 20:15:55 +02:00
}
2013-11-16 13:00:21 +01:00
//Update status communication of contact prospect
2013-04-14 20:15:55 +02:00
$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) " ;
dol_syslog ( " fiche.php: set prospect contact status sql= " . $sql , LOG_DEBUG );
$resqlx = $db -> query ( $sqlx );
if ( ! $resqlx )
{
dol_print_error ( $db );
$error ++ ;
}
}
}
}
else
2012-04-04 14:45:48 +02:00
{
2013-04-14 20:15:55 +02:00
// Mail failed
$nbko ++ ;
dol_syslog ( " error for emailing id " . $id . " # " . $i . ( $mail -> error ? ' - ' . $mail -> error : '' ), LOG_DEBUG );
$sqlerror = " UPDATE " . MAIN_DB_PREFIX . " mailing_cibles " ;
$sqlerror .= " SET statut=-1, date_envoi= " . $db -> idate ( $now ) . " WHERE rowid= " . $obj2 -> rowid ;
$resqlerror = $db -> query ( $sqlerror );
if ( ! $resqlerror )
{
dol_print_error ( $db );
$error ++ ;
}
2012-04-04 14:45:48 +02:00
}
2013-04-14 20:15:55 +02:00
$i ++ ;
2012-04-04 14:45:48 +02:00
}
}
2013-06-05 16:12:07 +02:00
else
{
$mesg = " Emailing id " . $id . " has no recipient to target " ;
print $mesg . " \n " ;
dol_syslog ( $mesg , LOG_ERR );
2013-04-14 20:15:55 +02:00
}
2009-09-08 16:28:08 +02:00
2013-04-14 20:15:55 +02:00
// Loop finished, set global statut of mail
$statut = 2 ;
if ( ! $nbko ) $statut = 3 ;
2009-09-08 16:28:08 +02:00
2013-04-14 20:15:55 +02:00
$sqlenddate = " UPDATE " . MAIN_DB_PREFIX . " mailing SET statut= " . $statut . " WHERE rowid= " . $id ;
dol_syslog ( " update global status sql= " . $sqlenddate , LOG_DEBUG );
print " Update status of emailing id " . $id . " to " . $statut . " \n " ;
$resqlenddate = $db -> query ( $sqlenddate );
if ( ! $resqlenddate )
2009-09-08 16:28:08 +02:00
{
dol_print_error ( $db );
2013-04-14 20:15:55 +02:00
$error ++ ;
2009-09-08 16:28:08 +02:00
}
}
2013-04-14 20:15:55 +02:00
else
{
dol_print_error ( $db );
$error ++ ;
}
2009-09-08 16:28:08 +02:00
}
}
2013-04-14 20:15:55 +02:00
else
2009-09-08 16:28:08 +02:00
{
2013-06-05 16:12:07 +02:00
$mesg = " No validated emailing id to send found. " ;
print $mesg . " \n " ;
dol_syslog ( $mesg , LOG_ERR );
2013-04-14 20:15:55 +02:00
$error ++ ;
2009-09-08 16:28:08 +02:00
}
2005-01-05 12:03:06 +01:00
}
else
{
2009-09-08 16:28:08 +02:00
dol_print_error ( $db );
2013-04-14 20:15:55 +02:00
$error ++ ;
2005-01-05 12:03:06 +01:00
}
2013-04-14 20:15:55 +02:00
exit ( $error );
2005-01-05 12:03:06 +01:00
?>