dolibarr/scripts/emailings/mailing-send.php

227 lines
5.4 KiB
PHP
Raw Normal View History

#!/usr/bin/php
2011-01-23 02:00:45 +01:00
<?php
2011-08-31 12:20:39 +02:00
/*
2010-11-20 17:25:23 +01:00
* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
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
* the Free Software Foundation; either version 2 of the License, or
* (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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2011-08-01 00:21:57 +02:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2005-01-05 12:03:06 +01:00
*/
2005-09-02 23:34:22 +02:00
/**
* \file scripts/emailings/mailing-send.php
2009-05-25 00:11:46 +02:00
* \ingroup mailing
* \brief Script d'envoi d'un mailing prepare et valide
*/
2005-09-02 23:34:22 +02:00
$sapi_type = php_sapi_name();
2009-10-22 03:04:23 +02:00
$script_file = basename(__FILE__);
$path=dirname(__FILE__).'/';
2009-10-22 03:04:23 +02:00
// Test if batch mode
if (substr($sapi_type, 0, 3) == 'cgi') {
2009-11-09 15:14:43 +01:00
echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
2009-10-22 03:04:23 +02:00
exit;
}
2005-09-02 23:34:22 +02:00
if (! isset($argv[1]) || ! $argv[1]) {
2009-11-09 15:14:43 +01:00
print "Usage: ".$script_file." ID_MAILING\n";
exit;
}
$id=$argv[1];
2005-01-05 12:03:06 +01:00
2009-12-16 11:23:38 +01:00
require_once ($path."../../htdocs/master.inc.php");
require_once (DOL_DOCUMENT_ROOT."/lib/CMailFile.class.php");
2005-01-05 12:03:06 +01:00
$error = 0;
2009-05-25 00:11:46 +02:00
// We read data of email
$sql = "SELECT m.rowid, m.titre, m.sujet, m.body,";
$sql.= " m.email_from, m.email_replyto, m.email_errorsto";
$sql.= " FROM ".MAIN_DB_PREFIX."mailing as m";
$sql.= " WHERE m.statut >= 1";
$sql.= " AND m.rowid= ".$id;
$sql.= " LIMIT 1";
2009-12-16 11:24:30 +01:00
$resql=$db->query($sql);
2009-02-18 23:05:21 +01:00
if ($resql)
2005-01-05 12:03:06 +01:00
{
$num = $db->num_rows($resql);
$i = 0;
if ($num == 1)
{
$obj = $db->fetch_object($resql);
2009-11-09 15:14:43 +01:00
dol_syslog("mailing ".$id);
$id = $obj->rowid;
$subject = $obj->sujet;
$message = $obj->body;
$from = $obj->email_from;
2009-05-25 00:11:46 +02:00
$replyto = $obj->email_replyto;
$errorsto = $obj->email_errorsto;
2010-12-01 22:16:28 +01:00
// Le message est-il en html
$msgishtml=-1; // Unknown by default
if (preg_match('/[\s\t]*<html>/i',$message)) $msgishtml=1;
2009-02-18 23:05:21 +01:00
$i++;
}
2009-11-09 15:14:43 +01:00
else
{
2009-11-09 15:19:41 +01:00
$mesg="Emailing with id ".$id." not found";
print $mesg."\n";
dol_syslog($mesg,LOG_ERR);
2009-11-09 15:14:43 +01:00
}
2005-01-05 12:03:06 +01:00
}
$nbok=0; $nbko=0;
2008-04-14 01:23:53 +02:00
// On choisit les mails non deja envoyes pour ce mailing (statut=0)
// ou envoyes en erreur (statut=-1)
2010-12-01 22:16:28 +01:00
$sql = "SELECT mc.rowid, mc.nom, mc.prenom, mc.email, mc.other, mc.source_url, mc.source_id, mc.source_type";
2005-01-05 12:03:06 +01:00
$sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
$sql .= " WHERE mc.statut < 1 AND mc.fk_mailing = ".$id;
2005-01-05 12:03:06 +01:00
$resql=$db->query($sql);
if ($resql)
2005-01-05 12:03:06 +01:00
{
$num = $db->num_rows($resql);
2005-01-05 12:03:06 +01:00
if ($num)
{
2009-11-09 15:14:43 +01:00
dol_syslog("nb of targets = ".$num, LOG_DEBUG);
// Positionne date debut envoi
$sql="UPDATE ".MAIN_DB_PREFIX."mailing SET date_envoi=SYSDATE() WHERE rowid=".$id;
$resql2=$db->query($sql);
if (! $resql2)
{
dol_print_error($db);
}
2009-02-18 23:05:21 +01:00
2011-08-20 17:11:31 +02:00
// Look on each email and sent message
$i = 0;
while ($i < $num)
{
$res=1;
2009-02-18 23:05:21 +01:00
$obj = $db->fetch_object($resql);
// sendto en RFC2822
$sendto = str_replace(',',' ',$obj->prenom." ".$obj->nom) ." <".$obj->email.">";
2009-02-18 23:05:21 +01:00
// Make subtsitutions on topic and body
2009-10-19 22:17:19 +02:00
$other=explode(';',$obj->other);
2009-02-18 23:05:21 +01:00
$other1=$other[0];
$other2=$other[1];
$other3=$other[2];
$other4=$other[3];
$other5=$other[4];
$substitutionarray=array(
2010-12-01 22:16:28 +01:00
'__ID__' => $obj->source_id,
'__EMAIL__' => $obj->email,
'__LASTNAME__' => $obj->nom,
2009-02-18 23:05:21 +01:00
'__FIRSTNAME__' => $obj->prenom,
'__OTHER1__' => $other1,
'__OTHER2__' => $other2,
'__OTHER3__' => $other3,
'__OTHER4__' => $other4,
'__OTHER5__' => $other5
);
2011-06-16 23:15:45 +02:00
complete_substitutions_array($substitutionarray,$langs);
2011-06-09 00:08:02 +02:00
$newsubject=make_substitutions($subject,$substitutionarray);
$newmessage=make_substitutions($message,$substitutionarray);
2009-02-18 23:05:21 +01:00
2011-06-09 00:08:02 +02:00
$substitutionisok=true;
// Fabrication du mail
$mail = new CMailFile($newsubject, $sendto, $from, $newmessage,
array(), array(), array(),
2009-05-25 00:11:46 +02:00
'', '', 0, $msgishtml, $errorsto);
2009-02-18 23:05:21 +01:00
if ($mail->error)
{
$res=0;
}
if (! $substitutionisok)
{
$mail->error='Some substitution failed';
$res=0;
}
// Send Email
if ($res)
{
$res=$mail->sendfile();
}
2009-02-18 23:05:21 +01:00
if ($res)
{
// Mail successful
$nbok++;
2009-02-18 23:05:21 +01:00
2009-11-09 15:14:43 +01:00
dol_syslog("ok for #".$i.($mail->error?' - '.$mail->error:''), LOG_DEBUG);
$sql="UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
2010-12-01 22:16:28 +01:00
$sql.=" SET statut=1, date_envoi=".$db->idate(gmmktime())." WHERE rowid=".$obj->rowid;
$resql2=$db->query($sql);
if (! $resql2)
{
dol_print_error($db);
}
}
else
{
// Mail failed
$nbko++;
2009-11-09 15:14:43 +01:00
dol_syslog("error for #".$i.($mail->error?' - '.$mail->error:''), LOG_DEBUG);
$sql="UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
2010-12-01 22:16:28 +01:00
$sql.=" SET statut=-1, date_envoi=".$db->idate(gmmktime())." WHERE rowid=".$obj->rowid;
$resql2=$db->query($sql);
if (! $resql2)
{
dol_print_error($db);
}
}
$i++;
}
}
// Loop finished, set global statut of mail
$statut=2;
if (! $nbko) $statut=3;
$sql="UPDATE ".MAIN_DB_PREFIX."mailing SET statut=".$statut." WHERE rowid=".$id;
2009-11-09 15:14:43 +01:00
dol_syslog("update global status sql=".$sql, LOG_DEBUG);
$resql2=$db->query($sql);
if (! $resql2)
{
dol_print_error($db);
}
2005-01-05 12:03:06 +01:00
}
else
{
dol_print_error($db);
2005-01-05 12:03:06 +01:00
}
?>