30if (!defined(
'NOSESSION')) {
31 define(
'NOSESSION',
'1');
34$sapi_type = php_sapi_name();
35$script_file = basename(__FILE__);
39if (substr($sapi_type, 0, 3) ==
'cgi') {
40 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
44if (!isset($argv[1]) || !$argv[1]) {
45 print
"Usage: ".$script_file.
" (ID_MAILING|all) [userloginforsignature] [maxnbofemails]\n";
51if (isset($argv[2]) || !empty($argv[2])) {
59if (isset($argv[3]) || !empty($argv[3])) {
64require_once $path.
"../../htdocs/master.inc.php";
65require_once DOL_DOCUMENT_ROOT.
"/core/class/CMailFile.class.php";
66require_once DOL_DOCUMENT_ROOT.
"/comm/mailing/class/mailing.class.php";
69$version = DOL_VERSION;
73 $conf->global->MAILING_LIMIT_SENDBYCLI = 0;
76$langs->loadLangs(array(
"main",
"mails"));
78if (!isModEnabled(
'mailing')) {
79 print
'Module Emailing not enabled';
83$hookmanager->initHooks(array(
'cli'));
91print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
94 print
'A delay of '.((float)
getDolGlobalInt(
'MAILING_DELAY')).
' seconds has been set between each email'.
"\n";
100if (!empty($dolibarr_main_db_readonly)) {
101 print
"Error: instance in read-only mode\n";
105$user =
new User($db);
108 $user->fetch(
'', $login);
112$sql =
"SELECT m.rowid, m.statut as status";
113$sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
114$sql .=
" WHERE m.statut IN (1,2)";
116 $sql .=
" AND m.rowid= ".((int) $id);
120$resql = $db->query($sql);
122 $num = $db->num_rows($resql);
126 for ($j = 0; $j < $num && $max != 0; $j++) {
127 $obj = $db->fetch_object($resql);
129 dol_syslog(
"Process mailing with id ".$obj->rowid);
130 print
"Process mailing with id ".$obj->rowid.
"\n";
132 if ($obj->status == 1) {
133 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET statut = 2 WHERE rowid = ".((int) $obj->rowid);
134 $result_sql = $db->query($sql);
135 dol_syslog(
"Started mailing campaign ".$obj->rowid, LOG_DEBUG);
139 $emailing->fetch($obj->rowid);
141 $upload_dir = $conf->mailing->dir_output.
"/".
get_exdir($emailing->id, 2, 0, 1, $emailing,
'mailing');
144 $subject = $emailing->sujet;
145 $message = $emailing->body;
146 $from = $emailing->email_from;
147 $replyto = $emailing->email_replyto;
148 $errorsto = $emailing->email_errorsto;
151 if (preg_match(
'/[\s\t]*<html>/i', $message)) {
160 $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";
161 $sql2 .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles as mc";
162 $sql2 .=
" WHERE mc.statut < 1 AND mc.fk_mailing = ".((int) $id);
165 } elseif (
getDolGlobalInt(
'MAILING_LIMIT_SENDBYCLI') > 0 && $max > 0) {
166 $sql2 .=
" LIMIT ".min(
getDolGlobalInt(
'MAILING_LIMIT_SENDBYCLI'), $max);
167 } elseif ($max > 0) {
168 $sql2 .=
" LIMIT ".((int) $max);
171 $resql2 = $db->query($sql2);
173 $num2 = $db->num_rows($resql2);
174 dol_syslog(
"Nb of targets = ".$num2, LOG_DEBUG);
175 print
"Nb of targets = ".$num2.
"\n";
181 $sqlstartdate =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET date_envoi='".$db->idate($now).
"' WHERE rowid=".((int) $id);
182 $resqlstartdate = $db->query($sqlstartdate);
183 if (!$resqlstartdate) {
188 $thirdpartystatic =
new Societe($db);
197 $obj = $db->fetch_object($resql2);
201 $sendto = str_replace(
',',
' ',
dolGetFirstLastname($obj->firstname, $obj->lastname).
" <".$obj->email.
">");
204 $other = explode(
';', $obj->other);
205 $tmpfield = explode(
'=', $other[0], 2);
206 $other1 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
207 $tmpfield = explode(
'=', $other[1], 2);
208 $other2 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
209 $tmpfield = explode(
'=', $other[2], 2);
210 $other3 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
211 $tmpfield = explode(
'=', $other[3], 2);
212 $other4 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
213 $tmpfield = explode(
'=', $other[4], 2);
214 $other5 = (isset($tmpfield[1]) ? $tmpfield[1] : $tmpfield[0]);
215 $signature = ((!empty($user->signature) && !
getDolGlobalString(
'MAIN_MAIL_DO_NOT_USE_SIGN')) ? $user->signature :
'');
218 $parameters = array(
'mode' =>
'emailing');
222 $substitutionarray[
'__ID__'] = $obj->source_id;
223 if ($obj->source_type ==
"thirdparty") {
224 $result = $thirdpartystatic->fetch($obj->source_id);
227 $substitutionarray[
'__THIRDPARTY_CUSTOMER_CODE__'] = $thirdpartystatic->code_client;
229 $substitutionarray[
'__THIRDPARTY_CUSTOMER_CODE__'] =
'';
232 $substitutionarray[
'__EMAIL__'] = $obj->email;
233 $substitutionarray[
'__LASTNAME__'] = $obj->lastname;
234 $substitutionarray[
'__FIRSTNAME__'] = $obj->firstname;
235 $substitutionarray[
'__MAILTOEMAIL__'] =
'<a href="mailto:'.$obj->email.
'">'.$obj->email.
'</a>';
236 $substitutionarray[
'__OTHER1__'] = $other1;
237 $substitutionarray[
'__OTHER2__'] = $other2;
238 $substitutionarray[
'__OTHER3__'] = $other3;
239 $substitutionarray[
'__OTHER4__'] = $other4;
240 $substitutionarray[
'__OTHER5__'] = $other5;
241 $substitutionarray[
'__USER_SIGNATURE__'] = $signature;
242 $substitutionarray[
'__SIGNATURE__'] = $signature;
243 $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"/>';
244 $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>';
245 $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);
247 $onlinepaymentenabled = 0;
248 if (isModEnabled(
'paypal')) {
249 $onlinepaymentenabled++;
251 if (isModEnabled(
'paybox')) {
252 $onlinepaymentenabled++;
254 if (isModEnabled(
'stripe')) {
255 $onlinepaymentenabled++;
265 $substitutionarray[
'__SECUREKEYPAYMENT_MEMBER__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'membersubscription'.$obj->source_id,
'2');
266 $substitutionarray[
'__SECUREKEYPAYMENT_ORDER__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'order'.$obj->source_id,
'2');
267 $substitutionarray[
'__SECUREKEYPAYMENT_INVOICE__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'invoice'.$obj->source_id,
'2');
268 $substitutionarray[
'__SECUREKEYPAYMENT_CONTRACTLINE__'] =
dol_hash(
getDolGlobalString(
'PAYMENT_SECURITY_TOKEN') .
'contractline'.$obj->source_id,
'2');
278 $substitutionarray[
'__SECUREKEYPAYPAL_MEMBER__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'membersubscription'.$obj->source_id,
'2');
284 $substitutionarray[
'__SECUREKEYPAYPAL_ORDER__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'order'.$obj->source_id,
'2');
290 $substitutionarray[
'__SECUREKEYPAYPAL_INVOICE__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'invoice'.$obj->source_id,
'2');
296 $substitutionarray[
'__SECUREKEYPAYPAL_CONTRACTLINE__'] =
dol_hash(
getDolGlobalString(
'PAYPAL_SECURITY_TOKEN') .
'contractline'.$obj->source_id,
'2');
304 $substitutionisok =
true;
307 if (preg_match(
'/__UNSUBSCRIBE__/', $message)) {
308 $moreinheader =
"List-Unsubscribe: <__UNSUBSCRIBE_URL__>\n";
317 $listofpaths =
dol_dir_list($upload_dir,
'all', 0,
'',
'',
'name', SORT_ASC, 0);
319 if (count($listofpaths)) {
320 foreach ($listofpaths as $key => $val) {
321 $arr_file[] = $listofpaths[$key][
'fullname'];
323 $arr_name[] = $listofpaths[$key][
'name'];
327 $trackid =
'emailing-'.$obj->fk_mailing.
'-'.$obj->rowid;
328 $upload_dir_tmp = $upload_dir;
329 $mail =
new CMailFile($newsubject, $sendto, $from, $newmessage, $arr_file, $arr_mime, $arr_name,
'',
'', 0, $msgishtml, $errorsto, $arr_css, $trackid, $moreinheader,
'emailing',
'', $upload_dir_tmp);
334 if (!$substitutionisok) {
335 $mail->error =
'Some substitution failed';
341 $res = $mail->sendfile();
348 dol_syslog(
"ok for emailing id ".
$id.
" #".$i.($mail->error ?
' - '.$mail->error :
''), LOG_DEBUG);
368 $sqlok =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
369 $sqlok .=
" SET statut = 1, date_envoi = '".$db->idate($now).
"' WHERE rowid = ".((int) $obj->rowid);
370 $resqlok = $db->query($sqlok);
376 if (strpos($message,
'__CHECK_READ__') !==
false) {
378 $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).
")";
379 dol_syslog(
"card.php: set prospect thirdparty status", LOG_DEBUG);
380 $resqlx = $db->query($sqlx);
387 $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)";
388 dol_syslog(
"card.php: set prospect contact status", LOG_DEBUG);
390 $resqlx = $db->query($sqlx);
405 dol_syslog(
"error for emailing id ".
$id.
" #".$i.($mail->error ?
' - '.$mail->error :
''), LOG_DEBUG);
407 $sqlerror =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
408 $sqlerror .=
" SET statut=-1, date_envoi='".$db->idate($now).
"' WHERE rowid=".$obj->rowid;
409 $resqlerror = $db->query($sqlerror);
424 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing SET statut=3 WHERE rowid=".$obj->rowid;
425 $result_sql = $db->query($sql);
427 dol_syslog(
"update global status", LOG_DEBUG);
428 print
"Update status of emailing id ".$id.
" to ".$statut.
"\n";
436 $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.
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.