29if (!defined(
'NOSESSION')) {
30 define(
'NOSESSION',
'1');
33$sapi_type = php_sapi_name();
34$script_file = basename(__FILE__);
38if (substr($sapi_type, 0, 3) ==
'cgi') {
39 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
43if (!isset($argv[1]) || !$argv[1]) {
44 print
"Usage: ".$script_file.
" (ID_MAILING|all) [userloginforsignature] [maxnbofemails]\n";
50if (isset($argv[2]) || !empty($argv[2])) {
58if (isset($argv[3]) || !empty($argv[3])) {
63require_once $path.
"../../htdocs/master.inc.php";
64require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
65require_once DOL_DOCUMENT_ROOT.
"/comm/mailing/class/mailing.class.php";
68$version = DOL_VERSION;
72 $conf->global->MAILING_LIMIT_SENDBYCLI = 0;
75$langs->loadLangs(array(
"main",
"mails"));
77if (!isModEnabled(
'mailing')) {
78 print
'Module Emailing not enabled';
82$hookmanager->initHooks(array(
'cli'));
90print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
93 print
'A delay of '.((float)
getDolGlobalInt(
'MAILING_DELAY')).
' seconds has been set between each email'.
"\n";
99if (!empty($dolibarr_main_db_readonly)) {
100 print
"Error: instance in read-only mode\n";
104$user =
new User($db);
107 $user->fetch(
'', $login);
111$sql =
"SELECT m.rowid, m.statut as status";
112$sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
113$sql .=
" WHERE m.statut IN (1,2)";
115 $sql .=
" AND m.rowid= ".((int) $id);
119$resql = $db->query($sql);
121 $num = $db->num_rows($resql);
125 for ($j = 0; $j < $num && $max != 0; $j++) {
126 $obj = $db->fetch_object($resql);
128 dol_syslog(
"Process mailing with id ".$obj->rowid);
129 print
"Process mailing with id ".$obj->rowid.
"\n";
131 if ($obj->status == 1) {
132 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET statut = 2 WHERE rowid = ".((int) $obj->rowid);
133 $result_sql = $db->query($sql);
134 dol_syslog(
"Started mailing campaign ".$obj->rowid, LOG_DEBUG);
138 $emailing->fetch($obj->rowid);
140 $upload_dir = $conf->mailing->dir_output.
"/".
get_exdir($emailing->id, 2, 0, 1, $emailing,
'mailing');
143 $subject = $emailing->sujet;
144 $message = $emailing->body;
145 $from = $emailing->email_from;
146 $replyto = $emailing->email_replyto;
147 $errorsto = $emailing->email_errorsto;
150 if (preg_match(
'/[\s\t]*<html>/i', $message)) {
159 $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";
160 $sql2 .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles as mc";
161 $sql2 .=
" WHERE mc.statut < 1 AND mc.fk_mailing = ".((int) $id);
164 } elseif (
getDolGlobalInt(
'MAILING_LIMIT_SENDBYCLI') > 0 && $max > 0) {
165 $sql2 .=
" LIMIT ".min(
getDolGlobalInt(
'MAILING_LIMIT_SENDBYCLI'), $max);
166 } elseif ($max > 0) {
167 $sql2 .=
" LIMIT ".((int) $max);
170 $resql2 = $db->query($sql2);
172 $num2 = $db->num_rows($resql2);
173 dol_syslog(
"Nb of targets = ".$num2, LOG_DEBUG);
174 print
"Nb of targets = ".$num2.
"\n";
180 $sqlstartdate =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET date_envoi='".$db->idate($now).
"' WHERE rowid=".((int) $id);
181 $resqlstartdate = $db->query($sqlstartdate);
182 if (!$resqlstartdate) {
187 $thirdpartystatic =
new Societe($db);
196 $obj = $db->fetch_object($resql2);
200 $sendto = str_replace(
',',
' ',
dolGetFirstLastname($obj->firstname, $obj->lastname).
" <".$obj->email.
">");
203 $other = explode(
';', $obj->other);
204 $tmpfield = explode(
'=', $other[0], 2);
205 $other1 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
206 $tmpfield = explode(
'=', $other[1], 2);
207 $other2 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
208 $tmpfield = explode(
'=', $other[2], 2);
209 $other3 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
210 $tmpfield = explode(
'=', $other[3], 2);
211 $other4 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
212 $tmpfield = explode(
'=', $other[4], 2);
213 $other5 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
214 $signature = ((!empty($user->signature) && !
getDolGlobalString(
'MAIN_MAIL_DO_NOT_USE_SIGN')) ? $user->signature :
'');
217 $parameters = array(
'mode' =>
'emailing');
221 $substitutionarray[
'__ID__'] = $obj->source_id;
222 if ($obj->source_type ==
"thirdparty") {
223 $result = $thirdpartystatic->fetch($obj->source_id);
226 $substitutionarray[
'__THIRDPARTY_CUSTOMER_CODE__'] = $thirdpartystatic->code_client;
228 $substitutionarray[
'__THIRDPARTY_CUSTOMER_CODE__'] =
'';
231 $substitutionarray[
'__EMAIL__'] = $obj->email;
232 $substitutionarray[
'__LASTNAME__'] = $obj->lastname;
233 $substitutionarray[
'__FIRSTNAME__'] = $obj->firstname;
234 $substitutionarray[
'__MAILTOEMAIL__'] =
'<a href="mailto:'.$obj->email.
'">'.$obj->email.
'</a>';
235 $substitutionarray[
'__OTHER1__'] = $other1;
236 $substitutionarray[
'__OTHER2__'] = $other2;
237 $substitutionarray[
'__OTHER3__'] = $other3;
238 $substitutionarray[
'__OTHER4__'] = $other4;
239 $substitutionarray[
'__OTHER5__'] = $other5;
240 $substitutionarray[
'__USER_SIGNATURE__'] = $signature;
241 $substitutionarray[
'__SIGNATURE__'] = $signature;
242 $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"/>';
243 $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>';
244 $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);
246 $onlinepaymentenabled = 0;
247 if (isModEnabled(
'paypal')) {
248 $onlinepaymentenabled++;
250 if (isModEnabled(
'paybox')) {
251 $onlinepaymentenabled++;
253 if (isModEnabled(
'stripe')) {
254 $onlinepaymentenabled++;
264 $substitutionarray[
'__SECUREKEYPAYMENT_MEMBER__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'membersubscription'.$obj->source_id, 2);
265 $substitutionarray[
'__SECUREKEYPAYMENT_ORDER__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'order'.$obj->source_id, 2);
266 $substitutionarray[
'__SECUREKEYPAYMENT_INVOICE__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'invoice'.$obj->source_id, 2);
267 $substitutionarray[
'__SECUREKEYPAYMENT_CONTRACTLINE__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'contractline'.$obj->source_id, 2);
277 $substitutionarray[
'__SECUREKEYPAYPAL_MEMBER__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'membersubscription'.$obj->source_id, 2);
283 $substitutionarray[
'__SECUREKEYPAYPAL_ORDER__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'order'.$obj->source_id, 2);
289 $substitutionarray[
'__SECUREKEYPAYPAL_INVOICE__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'invoice'.$obj->source_id, 2);
295 $substitutionarray[
'__SECUREKEYPAYPAL_CONTRACTLINE__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'contractline'.$obj->source_id, 2);
303 $substitutionisok =
true;
306 if (preg_match(
'/__UNSUBSCRIBE__/', $message)) {
307 $moreinheader =
"List-Unsubscribe: <__UNSUBSCRIBE_URL__>\n";
316 $listofpaths =
dol_dir_list($upload_dir,
'all', 0,
'',
'',
'name', SORT_ASC, 0);
318 if (count($listofpaths)) {
319 foreach ($listofpaths as $key => $val) {
320 $arr_file[] = $listofpaths[$key][
'fullname'];
322 $arr_name[] = $listofpaths[$key][
'name'];
326 $trackid =
'emailing-'.$obj->fk_mailing.
'-'.$obj->rowid;
327 $upload_dir_tmp = $upload_dir;
328 $mail =
new CMailFile($newsubject, $sendto, $from, $newmessage, $arr_file, $arr_mime, $arr_name,
'',
'', 0, $msgishtml, $errorsto, $arr_css, $trackid, $moreinheader,
'emailing',
'', $upload_dir_tmp);
333 if (!$substitutionisok) {
334 $mail->error =
'Some substitution failed';
340 $res = $mail->sendfile();
347 dol_syslog(
"ok for emailing id ".$id.
" #".$i.($mail->error ?
' - '.$mail->error :
''), LOG_DEBUG);
367 $sqlok =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
368 $sqlok .=
" SET statut = 1, date_envoi = '".$db->idate($now).
"' WHERE rowid = ".((int) $obj->rowid);
369 $resqlok = $db->query($sqlok);
375 if (strpos($message,
'__CHECK_READ__') !==
false) {
377 $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).
")";
378 dol_syslog(
"card.php: set prospect thirdparty status", LOG_DEBUG);
379 $resqlx = $db->query($sqlx);
386 $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)";
387 dol_syslog(
"card.php: set prospect contact status", LOG_DEBUG);
389 $resqlx = $db->query($sqlx);
404 dol_syslog(
"error for emailing id ".$id.
" #".$i.($mail->error ?
' - '.$mail->error :
''), LOG_DEBUG);
406 $sqlerror =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
407 $sqlerror .=
" SET statut=-1, date_envoi='".$db->idate($now).
"' WHERE rowid=".$obj->rowid;
408 $resqlerror = $db->query($sqlerror);
423 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET statut=3 WHERE rowid=".$obj->rowid;
424 $result_sql = $db->query($sql);
426 dol_syslog(
"update global status", LOG_DEBUG);
427 print
"Update status of emailing id ".$id.
" to ".$statut.
"\n";
435 $mesg =
"No validated emailing id to send found.";
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class to manage emailings module.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null, $include=null)
Return array of possible common substitutions.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.