<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// actual sending of message
	// ========================================================================================
	
	$query = "select * from `".$Settings['DBPrefix']."nl-Settings`";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result)) {
		$nlSettings[$row['Name']] = $row['Value'];
	}
	
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_newsletter.gif" width="22" height="21"> '.$MsgText[5][2];
	$BrowserTitle = $MsgText[5][2];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[5][2];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[501] == 'y') {
		
			$MessageID = intval($Arguments3);
			
			if ($nlSettings['SendBatchSize'] < 1) {
				$nlSettings['SendBatchSize'] = 10;
			}
			if ($nlSettings['AttemptsLimit'] < 1) {
				$nlSettings['AttemptsLimit'] = 3;
			}
			
			
			$query = "select * from `".$Settings['DBPrefix']."nl-Messages` where RecordNumber='".$MessageID."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
					
			
			$query2 = "select count(RecordNumber) as AddressesCount from `".$Settings['DBPrefix']."nl-Addresses` where DateAdded<=".$row['DateCode']." limit 1";
			$result2 = mysql_query($query2) or die (mysql_error());
			$row2 = mysql_fetch_array($result2);
			
			$TotalAddresses = $row2['AddressesCount'];
			$TotalSent = $row['SentToCount'];
						
			
			if ($TotalAddresses > $TotalSent) {
			
				$StrippedTitle = stripslashes($row['Title']);
				$StrippedMessage = stripslashes($row['Message']);
									
						
				$query3 = "select RecordNumber,AddressID from `".$Settings['DBPrefix']."nl-SendLog` where MessageID='".$MessageID."' and MessageSent=0 and Attempts<".$nlSettings['AttemptsLimit']." limit ".$nlSettings['SendBatchSize'];
				$result3 = mysql_query($query3) or die (mysql_error());
				while ($row3 = mysql_fetch_array($result3)) {
				
					$query4 = "select Email,ConfirmationCode from `".$Settings['DBPrefix']."nl-Addresses` where RecordNumber='".$row3['AddressID']."' limit 1";
					$result4 = mysql_query($query4) or die (mysql_error());
					$row4 = mysql_fetch_array($result4);
					
										
					if ($row['MailFormat'] == 'html') {
						$BoundaryID = uniqid('FlexCMS');
								
						$PlainTextMessage = $MsgText[5][27].": ".$MainURL."/newsletter/messages.html"."\n\n\n\n".$Settings['SiteName']."\n".$BaseURL."\n\n\n".$MsgText[5][28].":\n".$MainURL."/newsletter/unsubscribe_confirm/".$row4['Email']."/".$row4['ConfirmationCode']."\r\n\r\n";

						$HTMLMessage = '<font face="'.$FontFace.'" size="'.$FontSize.'">'.$StrippedMessage.'<font size="2"><br><br><br><br>'.$Settings['SiteName'].'<br><a href="'.$BaseURL.'" target="_blank">'.$BaseURL.'</a><br><br><br>'.$MsgText[5][28].':<br><a href="'.$MainURL.'/newsletter/unsubscribe_confirm/'.$row4['Email'].'/'.$row4['ConfirmationCode'].'" target="_blank">'.$MainURL.'/newsletter/unsubscribe_confirm/'.$row4['Email'].'/'.$row4['ConfirmationCode'].'</a><br><br></font></font>';
						
						$MessageHeaders = 'From: "'.$Settings['SiteName'].'" <'.$Settings['ContactEmail'].'>'."\r\n";
						$MessageHeaders .= "MIME-Version: 1.0\r\n";
						$MessageHeaders .= "Content-Type: multipart/alternative;\r\n boundary=\"".$BoundaryID."\"\r\n\r\n";
						$MessageHeaders .= $PlainTextMessage;
						$MessageHeaders .= "--".$BoundaryID."\r\n".
							"Content-Type: text/plain; charset=ISO-8859-1\r\n".
							"Content-Transfer-Encoding: base64\r\n\r\n";
						$MessageHeaders .= chunk_split(base64_encode($PlainTextMessage));
						$MessageHeaders .= "--".$BoundaryID."\r\n".
							"Content-Type: text/html; charset=ISO-8859-1\r\n".
							"Content-Transfer-Encoding: base64\r\n\r\n";
						$MessageHeaders .= chunk_split(base64_encode($HTMLMessage));
									
					}
					else {
						$Message = "\n".$StrippedMessage."\n\n\n\n".$Settings['SiteName']."\n".$BaseURL."\n\n\n".$MsgText[5][28].":\n".$MainURL."/newsletter/unsubscribe_confirm/".$row4['Email']."/".$row4['ConfirmationCode'];
						
						$MessageHeaders = 'From: "'.$Settings['SiteName'].'" <'.$Settings['ContactEmail'].'>'."\r\n";
						$MessageHeaders .= 'MIME-Version: 1.0'."\r\n".'Content-Type: text/plain; charset="iso-8859-1"'."\r\n".'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
						$MessageHeaders .= $Message;
					}
												
					
					if (mail($row4['Email'], $StrippedTitle, "", $MessageHeaders, '-f'.$Settings['ContactEmail'])) {
						// success
						
						$query5 = "update `".$Settings['DBPrefix']."nl-SendLog` Set MessageSent=1, Attempts=Attempts+1, DateCode=".GetTimestamp()." where RecordNumber=".$row3['RecordNumber']." limit 1";
						$result5 = mysql_query($query5) or die (mysql_error());
						
						$query5 = "update `".$Settings['DBPrefix']."nl-Messages` Set SentToCount=SentToCount+1 where RecordNumber=".$MessageID." limit 1";
						$result5 = mysql_query($query5) or die (mysql_error());
						
						$TotalSent++;
					
					}
					else {
						// failure
						
						$query5 = "update `".$Settings['DBPrefix']."nl-SendLog` Set Attempts=Attempts+1 where RecordNumber=".$row3['RecordNumber']." limit 1";
						$result5 = mysql_query($query5) or die (mysql_error());
					
					}
				
				}
			
			
				if ($TotalAddresses > 0) {
					$PercentComplete = ($TotalSent/$TotalAddresses)*100;
				}
				else {
					$PercentComplete = 0;
				}
			
				print '<br><br><br><br><br><div align="center"><b>Sent: '.$TotalSent.'<br><br>Remaining: '.($TotalAddresses-$TotalSent).'<br><br>'.number_format($PercentComplete,0).'% Complete</b><script>setTimeout("document.location=\''.$MainURL.'/admin/newsletter_sendfinal/'.$MessageID.'/'.rand(100,1000).'.html\';",2000);</script><br><br><br><a href="'.$MainURL.'/admin/newsletter_sendfinal/'.$MessageID.'/'.rand(100,1000).'.html"><font color="#000000">Click here if the numbers above to not change for more than a minute</font></a>.</div>';

				exit();	
			
			}
			else {
			
				if ($TotalAddresses > 0) {
					$PercentComplete = ($TotalSent/$TotalAddresses)*100;
				}
				else {
					$PercentComplete = 0;
				}
			
				$PageContents = '<br><br>Sending Complete.<br><br><br>Total Sent: '.$TotalSent.'<br><br><br><a href="'.$MainURL.'/options.html"><b>'.$MsgText[0][4].'</b></a>';	
			
			}
						
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}
		


?>
