31if (!defined(
'NOSESSION')) {
32 define(
'NOSESSION',
'1');
35$sapi_type = php_sapi_name();
36$script_file = basename(__FILE__);
40if (substr($sapi_type, 0, 3) ==
'cgi') {
41 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
45if (!isset($argv[1]) || !$argv[1]) {
46 print
"Usage: ".$script_file.
" (ID_MAILING|all) [userloginforsignature] [maxnbofemails]\n";
52if (isset($argv[2]) || !empty($argv[2])) {
60if (isset($argv[3]) || !empty($argv[3])) {
65require_once $path.
"../../htdocs/master.inc.php";
66require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
67require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
68require_once DOL_DOCUMENT_ROOT.
"/comm/mailing/class/mailing.class.php";
77$version = DOL_VERSION;
81 $conf->global->MAILING_LIMIT_SENDBYCLI = 0;
84$langs->loadLangs(array(
"main",
"mails"));
86if (!isModEnabled(
'mailing')) {
87 print
'Module Emailing not enabled';
91$hookmanager->initHooks(array(
'cli'));
99print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
102 print
'A delay of '.((float)
getDolGlobalInt(
'MAILING_DELAY')).
' seconds has been set between each email'.
"\n";
108if (!empty($dolibarr_main_db_readonly)) {
109 print
"Error: instance in read-only mode\n";
113$user =
new User($db);
116 $user->fetch(
'', $login);
120$sql =
"SELECT m.rowid, m.statut as status";
121$sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
122$sql .=
" WHERE m.statut IN (1,2)";
124 $sql .=
" AND m.rowid= ".((int) $id);
128$resql = $db->query($sql);
130 $num = $db->num_rows($resql);
134 for ($j = 0; $j < $num && $max != 0; $j++) {
135 $obj = $db->fetch_object($resql);
137 dol_syslog(
"Process mailing with id ".$obj->rowid);
138 print
"Process mailing with id ".$obj->rowid.
"\n";
140 if ($obj->status == 1) {
141 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET statut = 2 WHERE rowid = ".((int) $obj->rowid);
142 $result_sql = $db->query($sql);
143 dol_syslog(
"Started mailing campaign ".$obj->rowid, LOG_DEBUG);
147 $emailing->fetch($obj->rowid);
149 $upload_dir =
$conf->mailing->dir_output.
"/".
get_exdir($emailing->id, 2, 0, 1, $emailing,
'mailing');
152 $subject = $emailing->sujet;
153 $message = $emailing->body;
154 $from = $emailing->email_from;
155 $replyto = $emailing->email_replyto;
156 $errorsto = $emailing->email_errorsto;
159 if (preg_match(
'/[\s\t]*<html>/i', $message)) {
168 $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";
169 $sql2 .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles as mc";
170 $sql2 .=
" WHERE mc.statut < 1 AND mc.fk_mailing = ".((int) $id);
173 } elseif (
getDolGlobalInt(
'MAILING_LIMIT_SENDBYCLI') > 0 && $max > 0) {
174 $sql2 .=
" LIMIT ".min(
getDolGlobalInt(
'MAILING_LIMIT_SENDBYCLI'), $max);
175 } elseif ($max > 0) {
176 $sql2 .=
" LIMIT ".((int) $max);
179 $resql2 = $db->query($sql2);
181 $num2 = $db->num_rows($resql2);
182 dol_syslog(
"Nb of targets = ".$num2, LOG_DEBUG);
183 print
"Nb of targets = ".$num2.
"\n";
189 $sqlstartdate =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET date_envoi='".$db->idate($now).
"' WHERE rowid=".((int) $id);
190 $resqlstartdate = $db->query($sqlstartdate);
191 if (!$resqlstartdate) {
196 $thirdpartystatic =
new Societe($db);
205 $obj = $db->fetch_object($resql2);
209 $sendto = str_replace(
',',
' ',
dolGetFirstLastname($obj->firstname, $obj->lastname).
" <".$obj->email.
">");
212 $other = explode(
';', $obj->other);
213 $tmpfield = explode(
'=', $other[0], 2);
214 $other1 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
215 $tmpfield = explode(
'=', $other[1], 2);
216 $other2 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
217 $tmpfield = explode(
'=', $other[2], 2);
218 $other3 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
219 $tmpfield = explode(
'=', $other[3], 2);
220 $other4 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
221 $tmpfield = explode(
'=', $other[4], 2);
222 $other5 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
223 $signature = ((!empty($user->signature) && !
getDolGlobalString(
'MAIN_MAIL_DO_NOT_USE_SIGN')) ? $user->signature :
'');
226 $parameters = array(
'mode' =>
'emailing');
230 $substitutionarray[
'__ID__'] = $obj->source_id;
231 if ($obj->source_type ==
"thirdparty") {
232 $result = $thirdpartystatic->fetch($obj->source_id);
235 $substitutionarray[
'__THIRDPARTY_CUSTOMER_CODE__'] = $thirdpartystatic->code_client;
237 $substitutionarray[
'__THIRDPARTY_CUSTOMER_CODE__'] =
'';
240 $substitutionarray[
'__EMAIL__'] = $obj->email;
241 $substitutionarray[
'__LASTNAME__'] = $obj->lastname;
242 $substitutionarray[
'__FIRSTNAME__'] = $obj->firstname;
243 $substitutionarray[
'__MAILTOEMAIL__'] =
'<a href="mailto:'.$obj->email.
'">'.$obj->email.
'</a>';
244 $substitutionarray[
'__OTHER1__'] = $other1;
245 $substitutionarray[
'__OTHER2__'] = $other2;
246 $substitutionarray[
'__OTHER3__'] = $other3;
247 $substitutionarray[
'__OTHER4__'] = $other4;
248 $substitutionarray[
'__OTHER5__'] = $other5;
249 $substitutionarray[
'__USER_SIGNATURE__'] = $signature;
250 $substitutionarray[
'__SIGNATURE__'] = $signature;
251 $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"/>';
252 $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>';
253 $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);
255 $onlinepaymentenabled = 0;
256 if (isModEnabled(
'paypal')) {
257 $onlinepaymentenabled++;
259 if (isModEnabled(
'paybox')) {
260 $onlinepaymentenabled++;
262 if (isModEnabled(
'stripe')) {
263 $onlinepaymentenabled++;
273 $substitutionarray[
'__SECUREKEYPAYMENT_MEMBER__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'membersubscription'.$obj->source_id,
'2');
274 $substitutionarray[
'__SECUREKEYPAYMENT_ORDER__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'order'.$obj->source_id,
'2');
275 $substitutionarray[
'__SECUREKEYPAYMENT_INVOICE__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'invoice'.$obj->source_id,
'2');
276 $substitutionarray[
'__SECUREKEYPAYMENT_CONTRACTLINE__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'contractline'.$obj->source_id,
'2');
286 $substitutionarray[
'__SECUREKEYPAYPAL_MEMBER__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'membersubscription'.$obj->source_id,
'2');
292 $substitutionarray[
'__SECUREKEYPAYPAL_ORDER__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'order'.$obj->source_id,
'2');
298 $substitutionarray[
'__SECUREKEYPAYPAL_INVOICE__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'invoice'.$obj->source_id,
'2');
304 $substitutionarray[
'__SECUREKEYPAYPAL_CONTRACTLINE__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'contractline'.$obj->source_id,
'2');
312 $substitutionisok =
true;
315 if (preg_match(
'/__UNSUBSCRIBE__/', $message)) {
316 $moreinheader =
"List-Unsubscribe: <__UNSUBSCRIBE_URL__>\n";
325 $listofpaths =
dol_dir_list($upload_dir,
'all', 0,
'',
'',
'name', SORT_ASC, 0);
327 if (count($listofpaths)) {
328 foreach ($listofpaths as $key => $val) {
329 $arr_file[] = $listofpaths[$key][
'fullname'];
331 $arr_name[] = $listofpaths[$key][
'name'];
335 $trackid =
'emailing-'.$obj->fk_mailing.
'-'.$obj->rowid;
336 $upload_dir_tmp = $upload_dir;
337 $mail =
new CMailFile($newsubject, $sendto, $from, $newmessage, $arr_file, $arr_mime, $arr_name,
'',
'', 0, $msgishtml, $errorsto, $arr_css, $trackid, $moreinheader,
'emailing',
'', $upload_dir_tmp);
342 if (!$substitutionisok) {
343 $mail->error =
'Some substitution failed';
349 $res = $mail->sendfile();
356 dol_syslog(
"ok for emailing id ".
$id.
" #".$i.($mail->error ?
' - '.$mail->error :
''), LOG_DEBUG);
376 $sqlok =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
377 $sqlok .=
" SET statut = 1, date_envoi = '".$db->idate($now).
"' WHERE rowid = ".((int) $obj->rowid);
378 $resqlok = $db->query($sqlok);
384 if (strpos($message,
'__CHECK_READ__') !==
false) {
386 $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).
")";
387 dol_syslog(
"card.php: set prospect thirdparty status", LOG_DEBUG);
388 $resqlx = $db->query($sqlx);
395 $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)";
396 dol_syslog(
"card.php: set prospect contact status", LOG_DEBUG);
398 $resqlx = $db->query($sqlx);
413 dol_syslog(
"error for emailing id ".
$id.
" #".$i.($mail->error ?
' - '.$mail->error :
''), LOG_DEBUG);
415 $sqlerror =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
416 $sqlerror .=
" SET statut=-1, date_envoi='".$db->idate($now).
"' WHERE rowid=".$obj->rowid;
417 $resqlerror = $db->query($sqlerror);
432 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET statut=3 WHERE rowid=".$obj->rowid;
433 $result_sql = $db->query($sql);
435 dol_syslog(
"update global status", LOG_DEBUG);
436 print
"Update status of emailing id ".$id.
" to ".$statut.
"\n";
444 $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 a 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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.