<?php

include "checklog.php";

function qp_enc($input = "stringa di prova codifica quoted-printable", $line_max = 76) { 
	$hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); 
	$lines = preg_split("/(?:\r\n|\r|\n)/", $input); 
	$eol = isset($_SERVER["windir"]) ? "\r\n" : "\n";
	$escape = "="; 
	$output = ""; 

	while( list(, $line) = each($lines) ) { 
		//$line = rtrim($line);  
		$linlen = strlen($line); 
		$newline = ""; 
		for($i = 0; $i < $linlen; $i++) { 
			$c = substr($line, $i, 1); 
			$dec = ord($c); 
			if ( ($dec == 32) && ($i == ($linlen - 1)) ) { 
				$c = "=20"; 
			}
			elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) {  
				$h2 = floor($dec/16); $h1 = floor($dec%16); 
				$c = $escape.$hex["$h2"].$hex["$h1"]; 
			} 
			if ( (strlen($newline) + strlen($c)) >= $line_max ) { 
				$output .= $newline.$escape.$eol; 
				$newline = ""; 
			} 
			$newline .= $c; 
		} 
		$output .= $newline.$eol; 
	} 
	return trim($output); 
}

$eol = isset($_SERVER["windir"]) ? "\r\n" : "\n";

$mitt = trim($HTTP_POST_VARS["mittente"]);
$ogg = trim($HTTP_POST_VARS["oggetto"]);
$elenco = $HTTP_POST_VARS["elenco"];
$enc = $HTTP_POST_VARS["tipo"];
$msg = $HTTP_POST_VARS["messaggio"];
$msg = trim(rawurldecode($msg));

$destinatari = explode("\n",$elenco);

$nSent = 0;

$AddHeaders = "From: " . $mitt . "\r\n" . "Reply-To: " . $mitt . "";

if ($enc == 1) {
	$AddHeaders  .= "\r\nMIME-Version: 1.0".$eol;
	$AddHeaders .= "Content-type: text/html; charset=iso-8859-1".$eol;
	$AddHeaders .= "Content-Transfer-Encoding: quoted-printable";
	$msg = qp_enc($msg);
	$ogg = str_replace("=\r\n","",qp_enc($ogg));
} else {
	$AddHeaders .= "\r\nMIME-Version: 1.0".$eol;
	$AddHeaders .= "Content-type: text/plain; charset=iso-8859-1";
}

@set_time_limit(28800);
		
foreach ($destinatari as $destinatario) {
	$indirizzo = trim($destinatario);
	if (ereg('^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$', $indirizzo)) {
		$res = mail($indirizzo, $ogg, $msg, $AddHeaders);
		if ($res) {
			$nSent = $nSent + 1;
		}
	}
}
$Out = "Sono stati inviati $nSent messaggi.";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<base target="_self">
<link rel="stylesheet" href="backoffice.css" type="text/css">
<SCRIPT LANGUAGE = \"JavaScript\">
<!--

	document.cookie = "bonews_mitt=";
	document.cookie = "bonews_ogg=";
	document.cookie = "bonews_msg=";
	document.cookie = "bonews_dest=";
	document.cookie = "bonews_enc=";
	document.cookie = "bonews_test=";

//-->
</SCRIPT>
</head>
<body topmargin="0" leftmargin="4" marginwidth="4" marginheight="0" class="pagina">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td width="50%"><span class="titolo">Newsletter - </span><span class="sottotitolo">Risultati invio</span></td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
</table>
<p>&nbsp;</p>
<form action="bonews1.php" method="GET">
  <table border="0" cellpadding="2" class="form">
    <tr> 
      <td nowrap> 
        <?=$Out?>
      </td>
      <td align="left" valign="middle" nowrap>&nbsp;&nbsp;&nbsp;</td>
      <td align="left" valign="middle" width="100%"> 
        <input type="submit" name="Submit" value="OK" class="pulsante">
      </td>
    </tr>
  </table>
</form>
</body>
</html>