2015-12-11 05:08:32 +01:00
#!/usr/bin/env php
2011-01-23 02:00:45 +01:00
< ? php
2011-08-31 12:20:39 +02:00
/*
2019-05-15 13:20:02 +02:00
* Copyright ( C ) 2004 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2005 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2005 - 2016 Regis Houssin < regis . houssin @ inodbox . com >
2019-11-30 19:52:06 +01:00
* Copyright ( C ) 2019 Nicolas ZABOURI < info @ inovea - conseil . com >
2024-07-03 02:30:53 +02:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2025-02-18 16:08:09 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
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
2019-05-15 13:20:02 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2005-01-05 12:03:06 +01:00
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2005-01-05 12:03:06 +01:00
*/
2005-09-02 23:34:22 +02:00
/**
2020-11-12 12:03:43 +01:00
* \file scripts / emailings / mailing - send . php
2019-05-15 13:20:02 +02:00
* \ingroup mailing
2020-11-12 12:03:43 +01:00
* \brief Script to send a prepared and validated emaling from command line
2009-05-25 00:11:46 +02:00
*/
2024-01-26 01:52:06 +01:00
2021-03-01 00:22:36 +01:00
if ( ! defined ( 'NOSESSION' )) {
define ( 'NOSESSION' , '1' );
}
2020-10-27 01:44:00 +01: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__ );
2020-04-10 10:59:32 +02:00
$path = __DIR__ . '/' ;
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' ) {
2020-04-10 10:59:32 +02:00
echo " Error: You are using PHP for CGI. To execute " . $script_file . " from command line, you must use PHP for CLI mode. \n " ;
2024-03-04 19:41:21 +01:00
exit ( 1 );
2005-02-15 22:16:10 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! isset ( $argv [ 1 ]) || ! $argv [ 1 ]) {
2020-11-19 17:49:23 +01:00
print " Usage: " . $script_file . " (ID_MAILING|all) [userloginforsignature] [maxnbofemails] \n " ;
2024-03-04 19:41:21 +01:00
exit ( 1 );
2005-02-15 22:16:10 +01:00
}
2020-11-19 17:49:23 +01:00
2019-05-15 13:20:02 +02:00
$id = $argv [ 1 ];
2020-11-19 17:49:23 +01:00
2021-03-01 00:22:36 +01:00
if ( isset ( $argv [ 2 ]) || ! empty ( $argv [ 2 ])) {
$login = $argv [ 2 ];
} else {
$login = '' ;
}
2005-08-11 21:23:04 +02:00
2024-03-21 10:46:08 +01:00
$max = - 1 ;
2020-11-19 17:49:23 +01:00
2021-03-01 00:22:36 +01:00
if ( isset ( $argv [ 3 ]) || ! empty ( $argv [ 3 ])) {
$max = $argv [ 3 ];
}
2020-11-19 17:49:23 +01:00
2020-04-10 10:59:32 +02:00
require_once $path . " ../../htdocs/master.inc.php " ;
2024-10-04 12:10:25 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/functionscli.lib.php' ;
2020-04-10 10:59:32 +02:00
require_once DOL_DOCUMENT_ROOT . " /core/class/CMailFile.class.php " ;
require_once DOL_DOCUMENT_ROOT . " /comm/mailing/class/mailing.class.php " ;
2024-11-26 03:43:42 +01:00
/**
* @ var Conf $conf
* @ var DoliDB $db
* @ var HookManager $hookmanager
* @ var Translate $langs
* @ var User $user
*/
2013-05-15 11:24:18 +02:00
// Global variables
2019-05-15 13:20:02 +02:00
$version = DOL_VERSION ;
$error = 0 ;
2013-05-15 11:24:18 +02:00
2023-11-27 11:46:58 +01:00
if ( ! getDolGlobalString ( 'MAILING_LIMIT_SENDBYCLI' )) {
2020-12-05 13:26:58 +01:00
$conf -> global -> MAILING_LIMIT_SENDBYCLI = 0 ;
}
2023-01-13 11:09:06 +01:00
$langs -> loadLangs ( array ( " main " , " mails " ));
2023-02-21 13:19:57 +01:00
if ( ! isModEnabled ( 'mailing' )) {
print 'Module Emailing not enabled' ;
2024-03-04 19:41:21 +01:00
exit ( 1 );
2023-02-21 13:19:57 +01:00
}
2024-01-26 01:52:06 +01:00
$hookmanager -> initHooks ( array ( 'cli' ));
2020-12-05 13:26:58 +01:00
2013-05-15 11:24:18 +02:00
/*
* Main
*/
@ set_time_limit ( 0 );
2020-04-10 10:59:32 +02:00
print " ***** " . $script_file . " ( " . $version . " ) pid= " . dol_getmypid () . " ***** \n " ;
2013-05-15 11:24:18 +02:00
2023-12-26 14:49:38 +01:00
if ( getDolGlobalInt ( 'MAILING_DELAY' )) {
print 'A delay of ' . (( float ) getDolGlobalInt ( 'MAILING_DELAY' )) . ' seconds has been set between each email' . " \n " ;
2020-12-24 13:02:45 +01:00
}
2025-02-18 16:08:09 +01:00
//if (getDolGlobalString('MAILING_LIMIT_SENDBYCLI') == '-1') {
//}
2016-12-16 13:46:39 +01:00
2021-07-09 19:34:17 +02:00
if ( ! empty ( $dolibarr_main_db_readonly )) {
print " Error: instance in read-only mode \n " ;
2024-03-04 19:41:21 +01:00
exit ( 1 );
2021-07-09 19:34:17 +02:00
}
2015-04-18 16:39:53 +02:00
$user = new User ( $db );
// for signature, we use user send as parameter
2021-03-01 00:22:36 +01:00
if ( ! empty ( $login )) {
2025-02-18 16:08:09 +01:00
$user -> fetch ( 0 , $login );
2021-03-01 00:22:36 +01:00
}
2024-07-03 02:30:53 +02:00
/** @var DoliDB $db */
2017-05-02 18:21:50 +02:00
// We get list of emailing id to process
2024-03-21 10:46:08 +01:00
$sql = " SELECT m.rowid, m.statut as status " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " mailing as m " ;
2019-05-15 13:20:02 +02:00
$sql .= " WHERE m.statut IN (1,2) " ;
if ( $id != 'all' ) {
2021-04-26 15:44:24 +02:00
$sql .= " AND m.rowid= " . (( int ) $id );
2019-05-15 13:20:02 +02:00
$sql .= " LIMIT 1 " ;
2005-01-05 12:03:06 +01:00
}
2019-05-15 13:20:02 +02:00
$resql = $db -> query ( $sql );
if ( $resql ) {
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
2019-05-15 13:20:02 +02:00
if ( $num ) {
2024-03-21 13:48:50 +01:00
for ( $j = 0 ; $j < $num && $max != 0 ; $j ++ ) {
2009-09-08 16:28:08 +02:00
$obj = $db -> fetch_object ( $resql );
2005-04-21 01:01:30 +02:00
2020-04-10 10:59:32 +02:00
dol_syslog ( " Process mailing with id " . $obj -> rowid );
print " Process mailing with id " . $obj -> rowid . " \n " ;
2013-04-14 20:15:55 +02:00
2024-03-21 13:44:57 +01:00
if ( $obj -> status == 1 ) {
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing SET statut = 2 WHERE rowid = " . (( int ) $obj -> rowid );
2024-03-21 11:56:09 +01:00
$result_sql = $db -> query ( $sql );
dol_syslog ( " Started mailing campaign " . $obj -> rowid , LOG_DEBUG );
2024-03-21 10:46:08 +01:00
}
2017-05-02 18:21:50 +02:00
$emailing = new Mailing ( $db );
$emailing -> fetch ( $obj -> rowid );
2017-09-15 13:43:16 +02:00
2020-04-10 10:59:32 +02:00
$upload_dir = $conf -> mailing -> dir_output . " / " . get_exdir ( $emailing -> id , 2 , 0 , 1 , $emailing , 'mailing' );
2019-11-30 19:52:06 +01:00
2019-05-15 13:20:02 +02:00
$id = $emailing -> id ;
$subject = $emailing -> sujet ;
$message = $emailing -> body ;
$from = $emailing -> email_from ;
$replyto = $emailing -> email_replyto ;
2017-05-02 18:21:50 +02:00
$errorsto = $emailing -> email_errorsto ;
2013-04-14 20:15:55 +02:00
// Le message est-il en html
2023-12-04 10:10:44 +01:00
$msgishtml = - 1 ; // Unknown by default
2021-03-01 00:22:36 +01:00
if ( preg_match ( '/[\s\t]*<html>/i' , $message )) {
2019-05-15 13:20:02 +02:00
$msgishtml = 1 ;
2021-03-01 00:22:36 +01:00
}
2013-04-14 20:15:55 +02:00
2019-05-15 13:20:02 +02:00
$nbok = 0 ;
$nbko = 0 ;
2013-04-14 20:15:55 +02:00
// On choisit les mails non deja envoyes pour ce mailing (statut=0)
// ou envoyes en erreur (statut=-1)
2017-11-21 11:15:33 +01:00
$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 " ;
2020-04-10 10:59:32 +02:00
$sql2 .= " FROM " . MAIN_DB_PREFIX . " mailing_cibles as mc " ;
2020-11-19 17:49:23 +01:00
$sql2 .= " WHERE mc.statut < 1 AND mc.fk_mailing = " . (( int ) $id );
2023-11-27 13:26:44 +01:00
if ( getDolGlobalInt ( 'MAILING_LIMIT_SENDBYCLI' ) > 0 && empty ( $max )) {
2024-03-21 13:55:23 +01:00
$sql2 .= " LIMIT " . getDolGlobalInt ( 'MAILING_LIMIT_SENDBYCLI' );
2023-11-27 13:26:44 +01:00
} elseif ( getDolGlobalInt ( 'MAILING_LIMIT_SENDBYCLI' ) > 0 && $max > 0 ) {
2023-12-26 14:49:38 +01:00
$sql2 .= " LIMIT " . min ( getDolGlobalInt ( 'MAILING_LIMIT_SENDBYCLI' ), $max );
2020-11-19 17:49:23 +01:00
} elseif ( $max > 0 ) {
2021-04-26 15:44:24 +02:00
$sql2 .= " LIMIT " . (( int ) $max );
2017-10-16 08:47:05 +02:00
}
2017-09-15 13:43:16 +02:00
2019-05-15 13:20:02 +02:00
$resql2 = $db -> query ( $sql2 );
if ( $resql2 ) {
2013-04-14 20:15:55 +02:00
$num2 = $db -> num_rows ( $resql2 );
2020-04-10 10:59:32 +02:00
dol_syslog ( " Nb of targets = " . $num2 , LOG_DEBUG );
print " Nb of targets = " . $num2 . " \n " ;
2006-08-06 20:34:37 +02:00
2019-05-15 13:20:02 +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
2020-11-19 17:49:23 +01:00
$sqlstartdate = " UPDATE " . MAIN_DB_PREFIX . " mailing SET date_envoi=' " . $db -> idate ( $now ) . " ' WHERE rowid= " . (( int ) $id );
2019-05-15 13:20:02 +02:00
$resqlstartdate = $db -> query ( $sqlstartdate );
2020-04-10 10:59:32 +02:00
if ( ! $resqlstartdate ) {
2013-04-14 20:15:55 +02:00
dol_print_error ( $db );
2020-04-10 10:59:32 +02:00
$error ++ ;
2013-04-14 20:15:55 +02:00
}
2007-05-26 17:27:34 +02:00
2022-01-11 13:32:32 +01:00
$thirdpartystatic = new Societe ( $db );
2013-04-14 20:15:55 +02:00
// Look on each email and sent message
$i = 0 ;
2019-05-15 13:20:02 +02:00
while ( $i < $num2 ) {
2017-09-15 13:43:16 +02:00
// Here code is common with same loop ino card.php
2019-05-15 13:20:02 +02:00
$res = 1 ;
$now = dol_now ();
2013-04-14 20:15:55 +02:00
2017-11-21 11:15:33 +01:00
$obj = $db -> fetch_object ( $resql2 );
2024-03-21 10:46:08 +01:00
$max -- ;
2013-04-14 20:15:55 +02:00
// sendto en RFC2822
2020-04-10 10:59:32 +02:00
$sendto = str_replace ( ',' , ' ' , dolGetFirstLastname ( $obj -> firstname , $obj -> lastname ) . " < " . $obj -> email . " > " );
2013-04-14 20:15:55 +02:00
// Make subtsitutions on topic and body
2019-05-15 13:20:02 +02:00
$other = explode ( ';' , $obj -> other );
$tmpfield = explode ( '=' , $other [ 0 ], 2 );
$other1 = ( isset ( $tmpfield [ 1 ]) ? $tmpfield [ 1 ] : $tmpfield [ 0 ]);
$tmpfield = explode ( '=' , $other [ 1 ], 2 );
$other2 = ( isset ( $tmpfield [ 1 ]) ? $tmpfield [ 1 ] : $tmpfield [ 0 ]);
$tmpfield = explode ( '=' , $other [ 2 ], 2 );
$other3 = ( isset ( $tmpfield [ 1 ]) ? $tmpfield [ 1 ] : $tmpfield [ 0 ]);
$tmpfield = explode ( '=' , $other [ 3 ], 2 );
$other4 = ( isset ( $tmpfield [ 1 ]) ? $tmpfield [ 1 ] : $tmpfield [ 0 ]);
$tmpfield = explode ( '=' , $other [ 4 ], 2 );
$other5 = ( isset ( $tmpfield [ 1 ]) ? $tmpfield [ 1 ] : $tmpfield [ 0 ]);
2023-11-27 11:46:58 +01:00
$signature = (( ! empty ( $user -> signature ) && ! getDolGlobalString ( 'MAIN_MAIL_DO_NOT_USE_SIGN' )) ? $user -> signature : '' );
2019-05-15 13:20:02 +02:00
$object = null ; // Not defined with mass emailing
$parameters = array ( 'mode' => 'emailing' );
2020-04-10 10:59:32 +02:00
$substitutionarray = getCommonSubstitutionArray ( $langs , 0 , array ( 'object' , 'objectamount' ), $object ); // Note: On mass emailing, this is null because we don't know object
2017-10-16 08:47:05 +02:00
// Array of possible substitutions (See also file mailing-send.php that should manage same substitutions)
$substitutionarray [ '__ID__' ] = $obj -> source_id ;
2022-01-11 13:32:32 +01:00
if ( $obj -> source_type == " thirdparty " ) {
$result = $thirdpartystatic -> fetch ( $obj -> source_id );
if ( $result > 0 ) {
$substitutionarray [ '__THIRDPARTY_CUSTOMER_CODE__' ] = $thirdpartystatic -> code_client ;
} else {
$substitutionarray [ '__THIRDPARTY_CUSTOMER_CODE__' ] = '' ;
}
}
2017-10-16 08:47:05 +02:00
$substitutionarray [ '__EMAIL__' ] = $obj -> email ;
$substitutionarray [ '__LASTNAME__' ] = $obj -> lastname ;
$substitutionarray [ '__FIRSTNAME__' ] = $obj -> firstname ;
2020-04-10 10:59:32 +02:00
$substitutionarray [ '__MAILTOEMAIL__' ] = '<a href="mailto:' . $obj -> email . '">' . $obj -> email . '</a>' ;
2017-10-16 08:47:05 +02:00
$substitutionarray [ '__OTHER1__' ] = $other1 ;
$substitutionarray [ '__OTHER2__' ] = $other2 ;
$substitutionarray [ '__OTHER3__' ] = $other3 ;
$substitutionarray [ '__OTHER4__' ] = $other4 ;
$substitutionarray [ '__OTHER5__' ] = $other5 ;
2019-05-15 13:20:02 +02:00
$substitutionarray [ '__USER_SIGNATURE__' ] = $signature ; // Signature is empty when ran from command line or taken from user in parameter)
$substitutionarray [ '__SIGNATURE__' ] = $signature ; // For backward compatibility
2023-10-11 19:32:04 +02:00
$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"/>' ;
$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">' . $langs -> trans ( " MailUnsubcribe " ) . '</a>' ;
$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 );
2017-09-22 14:34:31 +02:00
2017-09-15 13:43:16 +02:00
$onlinepaymentenabled = 0 ;
2023-04-15 03:16:53 +02:00
if ( isModEnabled ( 'paypal' )) {
2020-04-10 10:59:32 +02:00
$onlinepaymentenabled ++ ;
2021-03-01 00:22:36 +01:00
}
2023-04-15 03:16:53 +02:00
if ( isModEnabled ( 'paybox' )) {
2020-04-10 10:59:32 +02:00
$onlinepaymentenabled ++ ;
2021-03-01 00:22:36 +01:00
}
2023-04-15 03:16:53 +02:00
if ( isModEnabled ( 'stripe' )) {
2020-04-10 10:59:32 +02:00
$onlinepaymentenabled ++ ;
2021-03-01 00:22:36 +01:00
}
2023-11-27 11:46:58 +01:00
if ( $onlinepaymentenabled && getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN' )) {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYMENT__' ] = dol_hash ( getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN' ), '2' );
2023-11-27 11:46:58 +01:00
if ( ! getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN_UNIQUE' )) {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYMENT_MEMBER__' ] = dol_hash ( getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN' ), '2' );
$substitutionarray [ '__SECUREKEYPAYMENT_ORDER__' ] = dol_hash ( getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN' ), '2' );
$substitutionarray [ '__SECUREKEYPAYMENT_INVOICE__' ] = dol_hash ( getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN' ), '2' );
$substitutionarray [ '__SECUREKEYPAYMENT_CONTRACTLINE__' ] = dol_hash ( getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN' ), '2' );
2019-05-15 13:20:02 +02:00
} else {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYMENT_MEMBER__' ] = dol_hash ( getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN' ) . 'membersubscription' . $obj -> source_id , '2' );
$substitutionarray [ '__SECUREKEYPAYMENT_ORDER__' ] = dol_hash ( getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN' ) . 'order' . $obj -> source_id , '2' );
$substitutionarray [ '__SECUREKEYPAYMENT_INVOICE__' ] = dol_hash ( getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN' ) . 'invoice' . $obj -> source_id , '2' );
$substitutionarray [ '__SECUREKEYPAYMENT_CONTRACTLINE__' ] = dol_hash ( getDolGlobalString ( 'PAYMENT_SECURITY_TOKEN' ) . 'contractline' . $obj -> source_id , '2' );
2017-09-22 14:34:31 +02:00
}
2017-09-15 13:43:16 +02:00
}
/* For backward compatibility */
2023-11-27 11:46:58 +01:00
if ( isModEnabled ( 'paypal' ) && getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN' )) {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYPAL__' ] = dol_hash ( getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN' ), '2' );
2017-09-15 13:43:16 +02:00
2023-11-27 11:46:58 +01:00
if ( ! getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN_UNIQUE' )) {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYPAL_MEMBER__' ] = dol_hash ( getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN' ), '2' );
2021-03-01 00:22:36 +01:00
} else {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYPAL_MEMBER__' ] = dol_hash ( getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN' ) . 'membersubscription' . $obj -> source_id , '2' );
2021-03-01 00:22:36 +01:00
}
2013-04-14 20:15:55 +02:00
2023-11-27 11:46:58 +01:00
if ( ! getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN_UNIQUE' )) {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYPAL_ORDER__' ] = dol_hash ( getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN' ), '2' );
2021-03-01 00:22:36 +01:00
} else {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYPAL_ORDER__' ] = dol_hash ( getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN' ) . 'order' . $obj -> source_id , '2' );
2021-03-01 00:22:36 +01:00
}
2017-09-15 13:43:16 +02:00
2023-11-27 11:46:58 +01:00
if ( ! getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN_UNIQUE' )) {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYPAL_INVOICE__' ] = dol_hash ( getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN' ), '2' );
2021-03-01 00:22:36 +01:00
} else {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYPAL_INVOICE__' ] = dol_hash ( getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN' ) . 'invoice' . $obj -> source_id , '2' );
2021-03-01 00:22:36 +01:00
}
2017-09-15 13:43:16 +02:00
2023-11-27 11:46:58 +01:00
if ( ! getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN_UNIQUE' )) {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYPAL_CONTRACTLINE__' ] = dol_hash ( getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN' ), '2' );
2021-03-01 00:22:36 +01:00
} else {
2024-09-05 16:05:37 +02:00
$substitutionarray [ '__SECUREKEYPAYPAL_CONTRACTLINE__' ] = dol_hash ( getDolGlobalString ( 'PAYPAL_SECURITY_TOKEN' ) . 'contractline' . $obj -> source_id , '2' );
2021-03-01 00:22:36 +01:00
}
2017-09-15 13:43:16 +02:00
}
2017-09-22 14:34:31 +02:00
2019-01-27 11:55:16 +01:00
complete_substitutions_array ( $substitutionarray , $langs );
2019-05-15 13:20:02 +02:00
$newsubject = make_substitutions ( $subject , $substitutionarray );
$newmessage = make_substitutions ( $message , $substitutionarray );
2013-04-14 20:15:55 +02:00
2019-05-15 13:20:02 +02:00
$substitutionisok = true ;
2013-04-14 20:15:55 +02:00
2021-05-07 09:19:44 +02:00
$moreinheader = '' ;
if ( preg_match ( '/__UNSUBSCRIBE__/' , $message )) {
2021-05-12 09:21:28 +02:00
$moreinheader = " List-Unsubscribe: <__UNSUBSCRIBE_URL__> \n " ;
$moreinheader = make_substitutions ( $moreinheader , $substitutionarray );
2021-05-07 09:19:44 +02:00
}
2019-11-30 19:52:06 +01:00
$arr_file = array ();
$arr_mime = array ();
$arr_name = array ();
$arr_css = array ();
2020-04-10 10:59:32 +02:00
$listofpaths = dol_dir_list ( $upload_dir , 'all' , 0 , '' , '' , 'name' , SORT_ASC , 0 );
2019-11-30 19:52:06 +01:00
2021-03-01 00:22:36 +01:00
if ( count ( $listofpaths )) {
foreach ( $listofpaths as $key => $val ) {
2020-04-10 10:59:32 +02:00
$arr_file [] = $listofpaths [ $key ][ 'fullname' ];
$arr_mime [] = dol_mimetype ( $listofpaths [ $key ][ 'name' ]);
$arr_name [] = $listofpaths [ $key ][ 'name' ];
2019-11-30 19:52:06 +01:00
}
}
2013-04-14 20:15:55 +02:00
// Fabrication du mail
2020-04-10 10:59:32 +02:00
$trackid = 'emailing-' . $obj -> fk_mailing . '-' . $obj -> rowid ;
2022-10-23 20:45:27 +02:00
$upload_dir_tmp = $upload_dir ;
$mail = new CMailFile ( $newsubject , $sendto , $from , $newmessage , $arr_file , $arr_mime , $arr_name , '' , '' , 0 , $msgishtml , $errorsto , $arr_css , $trackid , $moreinheader , 'emailing' , '' , $upload_dir_tmp );
2019-05-15 13:20:02 +02:00
if ( $mail -> error ) {
$res = 0 ;
2013-04-14 20:15:55 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! $substitutionisok ) {
2019-05-15 13:20:02 +02:00
$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
2019-05-15 13:20:02 +02:00
if ( $res ) {
$res = $mail -> sendfile ();
2013-04-14 20:15:55 +02:00
}
2012-04-05 21:02:00 +02:00
2019-05-15 13:20:02 +02:00
if ( $res ) {
2013-04-14 20:15:55 +02:00
// Mail successful
2020-04-10 10:59:32 +02:00
$nbok ++ ;
2013-04-14 20:15:55 +02:00
2020-04-10 10:59:32 +02:00
dol_syslog ( " ok for emailing id " . $id . " # " . $i . ( $mail -> error ? ' - ' . $mail -> error : '' ), LOG_DEBUG );
2013-04-14 20:15:55 +02:00
2017-05-02 18:21:50 +02:00
// Note: If emailing is 100 000 targets, 100 000 entries are added, so we don't enter events for each target here
// We must union table llx_mailing_taget for event tab OR enter 1 event with a special table link (id of email in event)
// Run trigger
/*
2019-05-15 13:20:02 +02:00
* if ( $obj -> source_type == 'contact' )
* {
* $emailing -> sendtoid = $obj -> source_id ;
* }
* if ( $obj -> source_type == 'thirdparty' )
* {
* $emailing -> socid = $obj -> source_id ;
* }
* // Call trigger
* $result = $emailing -> call_trigger ( 'EMAILING_SENTBYMAIL' , $user );
* if ( $result < 0 ) $error ++ ;
* // End call triggers
*/
2020-04-10 10:59:32 +02:00
$sqlok = " UPDATE " . MAIN_DB_PREFIX . " mailing_cibles " ;
2021-05-11 21:13:52 +02:00
$sqlok .= " SET statut = 1, date_envoi = ' " . $db -> idate ( $now ) . " ' WHERE rowid = " . (( int ) $obj -> rowid );
2019-05-15 13:20:02 +02:00
$resqlok = $db -> query ( $sqlok );
2020-04-10 10:59:32 +02:00
if ( ! $resqlok ) {
2013-04-14 20:15:55 +02:00
dol_print_error ( $db );
2020-04-10 10:59:32 +02:00
$error ++ ;
2019-05-15 13:20:02 +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
2021-05-11 21:13:52 +02:00
$sqlx = " 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 ) . " ) " ;
2014-09-18 21:18:25 +02:00
dol_syslog ( " card.php: set prospect thirdparty status " , LOG_DEBUG );
2019-05-15 13:20:02 +02:00
$resqlx = $db -> query ( $sqlx );
2020-04-10 10:59:32 +02:00
if ( ! $resqlx ) {
2013-04-14 20:15:55 +02:00
dol_print_error ( $db );
2020-04-10 10:59:32 +02:00
$error ++ ;
2013-04-14 20:15:55 +02:00
}
2019-05-15 13:20:02 +02:00
// Update status communication of contact prospect
2021-05-11 21:13:52 +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= " . (( int ) $obj -> rowid ) . " AND mc.source_type = 'contact' AND mc.source_id = sc.rowid) " ;
2014-09-18 21:18:25 +02:00
dol_syslog ( " card.php: set prospect contact status " , LOG_DEBUG );
2013-04-14 20:15:55 +02:00
2019-05-15 13:20:02 +02:00
$resqlx = $db -> query ( $sqlx );
2020-04-10 10:59:32 +02:00
if ( ! $resqlx ) {
2013-04-14 20:15:55 +02:00
dol_print_error ( $db );
2020-04-10 10:59:32 +02:00
$error ++ ;
2013-04-14 20:15:55 +02:00
}
}
2016-02-08 12:20:40 +01:00
2023-12-26 14:49:38 +01:00
if ( getDolGlobalInt ( 'MAILING_DELAY' )) {
2024-10-24 14:01:30 +02:00
usleep (( int ) (( float ) getDolGlobalInt ( 'MAILING_DELAY' ) * 1000000 ));
2017-10-16 08:47:05 +02:00
}
2013-04-14 20:15:55 +02:00
}
2019-05-15 13:20:02 +02:00
} else {
2013-04-14 20:15:55 +02:00
// Mail failed
2020-04-10 10:59:32 +02:00
$nbko ++ ;
2013-04-14 20:15:55 +02:00
2020-04-10 10:59:32 +02:00
dol_syslog ( " error for emailing id " . $id . " # " . $i . ( $mail -> error ? ' - ' . $mail -> error : '' ), LOG_DEBUG );
2013-04-14 20:15:55 +02:00
2020-04-10 10:59:32 +02:00
$sqlerror = " UPDATE " . MAIN_DB_PREFIX . " mailing_cibles " ;
$sqlerror .= " SET statut=-1, date_envoi=' " . $db -> idate ( $now ) . " ' WHERE rowid= " . $obj -> rowid ;
2019-05-15 13:20:02 +02:00
$resqlerror = $db -> query ( $sqlerror );
2020-04-10 10:59:32 +02:00
if ( ! $resqlerror ) {
2013-04-14 20:15:55 +02:00
dol_print_error ( $db );
2020-04-10 10:59:32 +02:00
$error ++ ;
2013-04-14 20:15:55 +02:00
}
2012-04-04 14:45:48 +02:00
}
2013-04-14 20:15:55 +02:00
2020-04-10 10:59:32 +02:00
$i ++ ;
2012-04-04 14:45:48 +02:00
}
2019-05-15 13:20:02 +02:00
} else {
2024-03-21 10:46:08 +01:00
//$mesg = "Emailing id ".$id." has no recipient to target";
2020-04-10 10:59:32 +02:00
print $mesg . " \n " ;
2019-01-27 11:55:16 +01:00
dol_syslog ( $mesg , LOG_ERR );
2009-09-08 16:28:08 +02:00
2024-03-21 11:56:09 +01:00
// Loop finished, set global statut of mail
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing SET statut=3 WHERE rowid= " . $obj -> rowid ;
$result_sql = $db -> query ( $sql );
2013-04-14 20:15:55 +02:00
2024-03-21 11:56:09 +01:00
dol_syslog ( " update global status " , LOG_DEBUG );
print " Update status of emailing id " . $id . " to " . $statut . " \n " ;
2009-09-08 16:28:08 +02:00
}
2019-05-15 13:20:02 +02:00
} else {
2013-04-14 20:15:55 +02:00
dol_print_error ( $db );
2020-04-10 10:59:32 +02:00
$error ++ ;
2013-04-14 20:15:55 +02:00
}
2009-09-08 16:28:08 +02:00
}
2019-05-15 13:20:02 +02:00
} else {
$mesg = " No validated emailing id to send found. " ;
2020-04-10 10:59:32 +02:00
print $mesg . " \n " ;
2019-01-27 11:55:16 +01:00
dol_syslog ( $mesg , LOG_ERR );
2020-04-10 10:59:32 +02:00
$error ++ ;
2009-09-08 16:28:08 +02:00
}
2019-05-15 13:20:02 +02:00
} else {
2009-09-08 16:28:08 +02:00
dol_print_error ( $db );
2020-04-10 10:59:32 +02:00
$error ++ ;
2005-01-05 12:03:06 +01:00
}
2013-04-14 20:15:55 +02:00
exit ( $error );