28if (!defined(
'NOSESSION')) {
29 define(
'NOSESSION',
'1');
32$sapi_type = php_sapi_name();
33$script_file = basename(__FILE__);
37if (substr($sapi_type, 0, 3) ==
'cgi') {
38 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
42if (!isset($argv[1]) || !$argv[1]) {
43 print
"Usage: ".$script_file.
" (ID_MAILING|all) [userloginforsignature] [maxnbofemails]\n";
49if (isset($argv[2]) || !empty($argv[2])) {
57if (isset($argv[3]) || !empty($argv[3])) {
62require_once $path.
"../../htdocs/master.inc.php";
63require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
64require_once DOL_DOCUMENT_ROOT.
"/comm/mailing/class/mailing.class.php";
67$version = DOL_VERSION;
71 $conf->global->MAILING_LIMIT_SENDBYCLI = 0;
74$langs->loadLangs(array(
"main",
"mails"));
76if (!isModEnabled(
'mailing')) {
77 print
'Module Emailing not enabled';
87print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
90 print
'A delay of '.((float)
getDolGlobalInt(
'MAILING_DELAY')).
' seconds has been set between each email'.
"\n";
96if (!empty($dolibarr_main_db_readonly)) {
97 print
"Error: instance in read-only mode\n";
101$user =
new User($db);
104 $user->fetch(
'', $login);
108$sql =
"SELECT m.rowid";
109$sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
110$sql .=
" WHERE m.statut IN (1,2)";
112 $sql .=
" AND m.rowid= ".((int) $id);
116$resql = $db->query($sql);
118 $num = $db->num_rows($resql);
122 for ($j = 0; $j < $num; $j++) {
123 $obj = $db->fetch_object($resql);
125 dol_syslog(
"Process mailing with id ".$obj->rowid);
126 print
"Process mailing with id ".$obj->rowid.
"\n";
129 $emailing->fetch($obj->rowid);
131 $upload_dir = $conf->mailing->dir_output.
"/".
get_exdir($emailing->id, 2, 0, 1, $emailing,
'mailing');
134 $subject = $emailing->sujet;
135 $message = $emailing->body;
136 $from = $emailing->email_from;
137 $replyto = $emailing->email_replyto;
138 $errorsto = $emailing->email_errorsto;
141 if (preg_match(
'/[\s\t]*<html>/i', $message)) {
150 $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";
151 $sql2 .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles as mc";
152 $sql2 .=
" WHERE mc.statut < 1 AND mc.fk_mailing = ".((int) $id);
155 } elseif (
getDolGlobalInt(
'MAILING_LIMIT_SENDBYCLI') > 0 && $max > 0) {
156 $sql2 .=
" LIMIT ".min(
getDolGlobalInt(
'MAILING_LIMIT_SENDBYCLI'), $max);
157 } elseif ($max > 0) {
158 $sql2 .=
" LIMIT ".((int) $max);
161 $resql2 = $db->query($sql2);
163 $num2 = $db->num_rows($resql2);
164 dol_syslog(
"Nb of targets = ".$num2, LOG_DEBUG);
165 print
"Nb of targets = ".$num2.
"\n";
171 $sqlstartdate =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET date_envoi='".$db->idate($now).
"' WHERE rowid=".((int) $id);
172 $resqlstartdate = $db->query($sqlstartdate);
173 if (!$resqlstartdate) {
178 $thirdpartystatic =
new Societe($db);
187 $obj = $db->fetch_object($resql2);
190 $sendto = str_replace(
',',
' ',
dolGetFirstLastname($obj->firstname, $obj->lastname).
" <".$obj->email.
">");
193 $other = explode(
';', $obj->other);
194 $tmpfield = explode(
'=', $other[0], 2);
195 $other1 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
196 $tmpfield = explode(
'=', $other[1], 2);
197 $other2 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
198 $tmpfield = explode(
'=', $other[2], 2);
199 $other3 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
200 $tmpfield = explode(
'=', $other[3], 2);
201 $other4 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
202 $tmpfield = explode(
'=', $other[4], 2);
203 $other5 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
204 $signature = ((!empty($user->signature) && !
getDolGlobalString(
'MAIN_MAIL_DO_NOT_USE_SIGN')) ? $user->signature :
'');
207 $parameters = array(
'mode' =>
'emailing');
211 $substitutionarray[
'__ID__'] = $obj->source_id;
212 if ($obj->source_type ==
"thirdparty") {
213 $result = $thirdpartystatic->fetch($obj->source_id);
216 $substitutionarray[
'__THIRDPARTY_CUSTOMER_CODE__'] = $thirdpartystatic->code_client;
218 $substitutionarray[
'__THIRDPARTY_CUSTOMER_CODE__'] =
'';
221 $substitutionarray[
'__EMAIL__'] = $obj->email;
222 $substitutionarray[
'__LASTNAME__'] = $obj->lastname;
223 $substitutionarray[
'__FIRSTNAME__'] = $obj->firstname;
224 $substitutionarray[
'__MAILTOEMAIL__'] =
'<a href="mailto:'.$obj->email.
'">'.$obj->email.
'</a>';
225 $substitutionarray[
'__OTHER1__'] = $other1;
226 $substitutionarray[
'__OTHER2__'] = $other2;
227 $substitutionarray[
'__OTHER3__'] = $other3;
228 $substitutionarray[
'__OTHER4__'] = $other4;
229 $substitutionarray[
'__OTHER5__'] = $other5;
230 $substitutionarray[
'__USER_SIGNATURE__'] = $signature;
231 $substitutionarray[
'__SIGNATURE__'] = $signature;
232 $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"/>';
233 $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>';
234 $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);
236 $onlinepaymentenabled = 0;
237 if (isModEnabled(
'paypal')) {
238 $onlinepaymentenabled++;
240 if (isModEnabled(
'paybox')) {
241 $onlinepaymentenabled++;
243 if (isModEnabled(
'stripe')) {
244 $onlinepaymentenabled++;
254 $substitutionarray[
'__SECUREKEYPAYMENT_MEMBER__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'membersubscription'.$obj->source_id, 2);
255 $substitutionarray[
'__SECUREKEYPAYMENT_ORDER__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'order'.$obj->source_id, 2);
256 $substitutionarray[
'__SECUREKEYPAYMENT_INVOICE__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'invoice'.$obj->source_id, 2);
257 $substitutionarray[
'__SECUREKEYPAYMENT_CONTRACTLINE__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'contractline'.$obj->source_id, 2);
267 $substitutionarray[
'__SECUREKEYPAYPAL_MEMBER__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'membersubscription'.$obj->source_id, 2);
273 $substitutionarray[
'__SECUREKEYPAYPAL_ORDER__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'order'.$obj->source_id, 2);
279 $substitutionarray[
'__SECUREKEYPAYPAL_INVOICE__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'invoice'.$obj->source_id, 2);
285 $substitutionarray[
'__SECUREKEYPAYPAL_CONTRACTLINE__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'contractline'.$obj->source_id, 2);
293 $substitutionisok =
true;
296 if (preg_match(
'/__UNSUBSCRIBE__/', $message)) {
297 $moreinheader =
"List-Unsubscribe: <__UNSUBSCRIBE_URL__>\n";
306 $listofpaths =
dol_dir_list($upload_dir,
'all', 0,
'',
'',
'name', SORT_ASC, 0);
308 if (count($listofpaths)) {
309 foreach ($listofpaths as $key => $val) {
310 $arr_file[] = $listofpaths[$key][
'fullname'];
312 $arr_name[] = $listofpaths[$key][
'name'];
316 $trackid =
'emailing-'.$obj->fk_mailing.
'-'.$obj->rowid;
317 $upload_dir_tmp = $upload_dir;
318 $mail =
new CMailFile($newsubject, $sendto, $from, $newmessage, $arr_file, $arr_mime, $arr_name,
'',
'', 0, $msgishtml, $errorsto, $arr_css, $trackid, $moreinheader,
'emailing',
'', $upload_dir_tmp);
323 if (!$substitutionisok) {
324 $mail->error =
'Some substitution failed';
330 $res = $mail->sendfile();
337 dol_syslog(
"ok for emailing id ".$id.
" #".$i.($mail->error ?
' - '.$mail->error :
''), LOG_DEBUG);
357 $sqlok =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
358 $sqlok .=
" SET statut = 1, date_envoi = '".$db->idate($now).
"' WHERE rowid = ".((int) $obj->rowid);
359 $resqlok = $db->query($sqlok);
365 if (strpos($message,
'__CHECK_READ__') !==
false) {
367 $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).
")";
368 dol_syslog(
"card.php: set prospect thirdparty status", LOG_DEBUG);
369 $resqlx = $db->query($sqlx);
376 $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)";
377 dol_syslog(
"card.php: set prospect contact status", LOG_DEBUG);
379 $resqlx = $db->query($sqlx);
394 dol_syslog(
"error for emailing id ".$id.
" #".$i.($mail->error ?
' - '.$mail->error :
''), LOG_DEBUG);
396 $sqlerror =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
397 $sqlerror .=
" SET statut=-1, date_envoi='".$db->idate($now).
"' WHERE rowid=".$obj->rowid;
398 $resqlerror = $db->query($sqlerror);
408 $mesg =
"Emailing id ".$id.
" has no recipient to target";
419 $sqlenddate =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET statut=".((int) $statut).
" WHERE rowid=".((int) $id);
421 dol_syslog(
"update global status", LOG_DEBUG);
422 print
"Update status of emailing id ".$id.
" to ".$statut.
"\n";
423 $resqlenddate = $db->query($sqlenddate);
424 if (!$resqlenddate) {
434 $mesg =
"No validated emailing id to send found.";
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($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_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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...
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.