Posto il codice completo, cosė speriamo di risolvere questo problema che va avanti da troppo, troppo, tempo e mi sta facendo perdere davvero moltissimo tempo...
<?PHP
// Inclusione file MKPortal per collegamento al database
define ( 'IN_MKP', 1 );
$MK_PATH = "../";
require $MK_PATH."mkportal/conf_mk.php";
require $MK_PATH."mkportal/include/SMF/smf_driverf.php";
$prefix = DBPREFIX;
// Leggo i dati dal database
$query = $DB->query("
SELECT *
FROM mkp_newsletter
WHERE data <= UNIX_TIMESTAMP()
");
// Se c'č qualcosa da spedire proseguo
//if ( $DB->get_num_row() == 0 ) exit();
// Mi creo l'array con gli utenti del sito
//$prefix = DBPREFIX;
$elenco = array();
$users = $DB->query("
SELECT ID_MEMBER, memberName, emailAddress
FROM {$prefix}members
ORDER BY ID_MEMBER
");
while ( $row1 = $DB->fetch_row($users) ) {
$id = $row1['ID_MEMBER'];
$elenco[$id]['name'] = $row1['memberName'];
$elenco[$id]['email'] = $row1['emailAddress'];
}
if ( isset($_GET['totest']) ) {
$elenco = array();
$elenco['1']['name'] = "xxx";
$elenco['1']['email'] = "xxx@libero.it";
$elenco['2']['name'] = "Redazione";
$elenco['2']['email'] = "redazione@mangaforever.net";
}
$numero_di_utenti = count($elenco);
echo $numero_di_utenti;
echo "<br />";
#print_r($elenco);
while ( $row = $DB->fetch_row($query) ) {
$id = $row['id'];
$titolo = $row['titolo'];
$testo = stripslashes($row['testo']);
$data = $row['data'];
$weekly = (int)$row['weekly'];
$totest = 0;
$data_invio = getdate($data);
$data_attuale = getdate(time());
// Per le newsletter settimanali serve a farle inviare SOLO il giorno giusto
if ( $weekly == 2 && ($data_invio['wday'] != $data_attuale['wday']) ) {
$totest = 1;
}
$messaggio = getemail($titolo, $testo);
foreach ( $elenco AS $utente ) {
$to = trim($utente['email']);
$messaggio2 = str_replace("{username}", $utente['name'], $messaggio);
sendmail($to, "redazione@mangaforever.net", $messaggio2, $titolo, 1, $totest);
}
if ( $weekly == 1 && $totest == 0) {
$DB->query("DELETE FROM mkp_newsletter WHERE id = '$id'");
}
}
/*
================================================== ======
GESTIONE NEWSLETTER SUNTO SETTIMANALE AUTOMATICO
================================================== ======
Dettagli giorni:
0 => Domenica
1 => Lunedė
2 => Martedė
3 => Mercoledė
4 => Giovedė
5 => Venerdė
6 => Sabato
*/
// Sceglie di che giorno inviare la newsletter settimanale automatica
$data_attuale = getdate(time());
if ( $data_attuale['wday'] != 2 ) {
exit();
}
$cont = "";
// News
$query_news = "
SELECT id, titolo, testo, data, image
FROM mkp_news
WHERE validate = '1' AND CURDATE() - INTERVAL 7 DAY <= FROM_UNIXTIME(data) AND data <= UNIX_TIMESTAMP()
ORDER BY data DESC
LIMIT 89
";
$cont .= "<tr><td colspan=\"2\" bgcolor=\"#d5d5d5\" align=\"center\"><strong>NEWS DELLA SETTIMANA</strong></td></tr>";
$cont .= build($query_news, "news", "news_show_single", "ide");
// Recensioni
$query_reviews = "
SELECT id, title AS titolo, review AS testo, date AS data, image
FROM mkp_reviews
WHERE validate = '1' AND CURDATE() - INTERVAL 7 DAY <= FROM_UNIXTIME(date) AND date <= UNIX_TIMESTAMP()
ORDER BY date DESC
";
$cont .= "<tr><td colspan=\"2\" bgcolor=\"#d5d5d5\" align=\"center\"><strong>RUBRICHE E RECENSIONI DELLA SETTIMANA</strong></td></tr>";
$cont .= build($query_reviews, "reviews", "entry_view", "iden");
// Download
$query_downloads = "
SELECT id, name AS titolo, description AS testo, data, screen1 AS image
FROM mkp_download
WHERE validate = '1' AND CURDATE() - INTERVAL 7 DAY <= FROM_UNIXTIME(data) AND data <= NOW()
ORDER BY data DESC
";
$cont .= "<tr><td colspan=\"2\" bgcolor=\"#d5d5d5\" align=\"center\"><strong>DOWNLOAD DELLA SETTIMANA</strong></td></tr>";
$cont .= build($query_downloads, "download", "entry_view", "iden");
// Messaggio Email
$content = "
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">
{$cont}
</table>
";
$messaggio = getemail("Newsletter Settimanale", $content);
foreach ( $elenco AS $utente ) {
$to = trim($utente['email']);
sendmail($to, "redazione@mangaforever.net", $messaggio, "Newsletter Settimanale", 1);
}
/*
================================================== ===========
FINE GESTIONE NEWSLETTER SUNTO SETTIMANALE AUTOMATICO
================================================== ===========
*/
// FUNZIONE GENERAZIONE NEWSLETTER SETTIMANALE
function build($query1, $ind, $op, $ide, $col = 2) {
global $DB;
$cont = "";
$count = 0;
$query = $DB->query($query1);
while ( $row = $DB->fetch_row($query) ) {
$id = $row['id'];
$titolo = stripslashes($row['titolo']);
$testo = stripslashes(substr(strip_tags($row['testo']), 0, 150)) . "...";
$data = date("d/m/Y", $row['data']);
$image = $row['image'];
$grid_image = "<img src=\"http://www.mangaforever.net/mkportal/modules/reviews/images/a_no_image.gif\" width=\"64px\" alt=\"\" border=\"0\">";
if ( $image ) {
if ( strtolower(substr($image, 0, 7)) == 'http://' )
$grid_image = "<img src=\"$image\" width=\"64px\" alt=\"\" border=\"0\">";
elseif ( $ind == "reviews" && strtolower(substr($image, 0, 8)) != 'mkportal')
$grid_image = "<img src=\"http://www.mangaforever.net/mkportal/modules/reviews/images/$image\" width=\"64px\" alt=\"\" border=\"0\">";
}
if ( $count == 0 ) {
$cont .= "
<tr>
";
}
$cont .= "
<td class=\"tabnews\" width=\"50%\" valign=\"top\">
<table cellspacing=\"2\" cellpadding=\"2\" width=\"100%\">
<tr>
<td colspan=\"2\" style=\"font: 11px Curier New\">[$data] - <a href=\"http://www.mangaforever.net/index.php?ind=$ind&op=$op&$ide={$row['id']}\"><strong>$titolo</strong></a></td>
</tr>
<tr>
<td bgcolor=\"#FFFFFF\" align=\"center\" width=\"64px\" style=\"height: 64px; margin: auto;\">
<a href=\"http://www.mangaforever.net/index.php?ind=$ind&op=$op&$ide={$row['id']}\">$grid_image</a>
</td>
<td class=\"tdblock\" valign=\"top\">
<div align=\"justify\" style=\"font: 10px Curier New\">$testo</div>
</td>
</tr>
</table>
</td>
";
$count = ( $count + 1 ) % $col;
if ( $count == 0 ) {
$cont .= "
</tr>
";
}
}
if ( $count == 1 ) {
$cont .= "
<td class=\"tabnews\" width=\"50%\">
</td>
</tr>
";
}
return $cont;
}
// FUNZIONE PER INVIO EMAIL
function sendmail($to, $from, $message, $subject, $html = 0, $test = 0) {
if ( !$to ) return false;
$delimiter = "\n";
$encoding = 'UTF-8';
$http_host = "www.mangaforever.net";
$headers = "From: <$from>" . $delimiter;
$headers .= "Return-Path: <$from>" . $delimiter;
if ($_SERVER['HTTP_HOST'] OR $_ENV['HTTP_HOST'])
{
$http_host = $_SERVER['HTTP_HOST'] ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
}
else if ($_SERVER['SERVER_NAME'] OR $_ENV['SERVER_NAME'])
{
$http_host = $_SERVER['SERVER_NAME'] ? $_SERVER['SERVER_NAME'] : $_ENV['SERVER_NAME'];
}
$http_host = trim($http_host);
$msgid = '<' . gmdate('YmdHs') . '.' . substr(md5($message . microtime()), 0, 6) . rand(100000, 999999) . '@' . $http_host . '>';
$headers .= 'Message-ID: ' . $msgid . $delimiter;
$headers .= 'MIME-Version: 1.0' . $delimiter;
if ( $html == 0 ) {
$headers .= 'Content-Type: text/plain' . ("; charset=\"iso-8859-1\"") . $delimiter;
} else {
$headers .= 'Content-Type: text/html' . ("; charset=\"iso-8859-1\"") . $delimiter;
}
$headers .= 'Content-Transfer-Encoding: 8bit' . $delimiter;
$headers .= 'X-Priority: 3' . $delimiter;
$headers .= 'X-Mailer: MangaForever Mail via PHP' . $delimiter;
if ( $test == 1 ) {
return true;
}
// Introduce una pausa di 0.01 secondi
usleep(90000);
if ( !@mail($to, $subject, $message, $headers) ) {
if ( !@mail($to, $subject, $message, "From: <$from>\r\nReturn-Path: <$from>\r\n") ) {
echo "\n";
echo "Indirizzo ".$to." - Non inviata";
return false;
}
#return @mail($to, $subject, $message, "From: <$from>");
}
echo "\n";
echo "Indirizzo ".$to." - Inviata";
return true;
}
|