Location: PHPKode > projects > NmnNewsletter > NmnNewsletter1.0.8/includes/NewsletterSender.class.php
<?php
/*
    NmnNewsletter is a library that provides newsletter service 
	management for websites running php and mysql.
    Copyright (C) 2006  Ivan Preziosi from netmeans.net - Rome.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    For more informations or to join the development of the library contact
    the author at: hide@address.com
    */

require_once 'phpMailer/mailUtil.php';
require_once 'html2text/class.html2text.inc.php';

class NewsletterSender{

	function send($idNewsletter){
		if(is_null($idNewsletter))$idNewsletter=0;
		if(SETTIMELIMIT)set_time_limit(0);
		if ($idNewsletter > '0'){
			NewsletterLog::createLogs($idNewsletter);
			$recipients = NewsletterSender::getRecipients($idNewsletter);
			//echo count($recipients);
			$nlData = Newsletter::getNewsletter($idNewsletter);
			//NmnLogger::logString("Preparing to send newsletter id=".$idNewsletter);
			foreach ($recipients as $idLog => $recipientData){
				$body = NewsletterSender::prepareBody($nlData["html_body"],$recipientData["email"],$recipientData["idUser"],$idNewsletter);
				$mailBody = $body["htmlBody"];
				$textBody = $body["textBody"];
				$isSent = sendEmail(SMTP_HOST, SMTP_USER, SMTP_PASSWORD, MAIL_FROM, NAME_FROM, $recipientData["email"], $recipientData["email"], $nlData["titolo"], $textBody, $mailBody,'');
				if ($isSent){
					NewsletterLog::markLogAsSent($idLog);
					//NmnLogger::logString("Newsletter(id=".$idNewsletter.") succesfully sent to recipient:". $recipientData["email"]." (userId=".$recipientData["idUser"].")");
				}else{
					//NmnLogger::logString("Newsletter(id=".$idNewsletter.") not sent to recipient:". $recipientData["email"]." (userId=".$recipientData["idUser"].")");
				}	
			}
		}
	}

	function preSend($idNewsletter,$testRecipient){
		if(is_null($idNewsletter))$idNewsletter=0;
		if(is_null($testRecipient))return false;
		$nlData = Newsletter::getNewsletter($idNewsletter);
		$body = NewsletterSender::prepareBody($nlData["html_body"],$testRecipient,0,0);
		$mailBody = $body["htmlBody"];
		$textBody = $body["textBody"];
		$isSent = sendEmail(SMTP_HOST, SMTP_USER, SMTP_PASSWORD, MAIL_FROM, NAME_FROM, $testRecipient, $testRecipient, $nlData["titolo"], $textBody, $mailBody,'');
		if ($isSent){
			//NmnLogger::logString("Newsletter(id=".$idNewsletter.") succesfully pre-sent to recipient:". $testRecipient."");
		}else{
			//NmnLogger::logString("Newsletter(id=".$idNewsletter.") not pre-sent to recipient:". $testRecipient."");	
		}	
		return $isSent;
	}

	function previewNewsletter($idNewsletter,$mode){
		if(is_null($idNewsletter))$idNewsletter=0;
		if(is_null($mode))$mode="html";
		$nlData = Newsletter::getNewsletter($idNewsletter);
		$mailBody = NewsletterSender::prepareBody($nlData["html_body"],"hide@address.com",0,0);
		echo '<html><head><title>Newsletter preview</title>';
		echo '<meta http-equiv="content-type" content="text/html"; charset="'.CHARSET.'"></head><body>';
		if ($mode == "text"){
			echo $mailBody["textBody"];
		}else{
			echo $mailBody["htmlBody"];
		}
		echo '</body></html>';
	}

	function getRecipients($idNewsletter){
		if(is_null($idNewsletter))$idNewsletter=0;
		$dbConn = NewsletterDbConnector::connect();
		$query = "SELECT n.id_".DB_LOGS_TABLE.", u.email, u.id_".DB_USERS_TABLE." FROM ".DB_LOGS_TABLE." n LEFT OUTER JOIN ".DB_USERS_TABLE." u ON u.id_".DB_USERS_TABLE." = n.id_user WHERE n.stato = '0' AND n.id_nl = '".mysql_escape_string($idNewsletter)."'";
		$result = mysql_query($query);
		NewsletterDbConnector::closeConnection($dbConn);
		$users = array();
		while($user = mysql_fetch_assoc($result)){
			$userData = array("idUser" => $user["id_".DB_USERS_TABLE.""], "email" => $user["email"]);
			$users[$user["id_".DB_LOGS_TABLE]] = $userData;
		}
		return $users;
	}

	function prepareBody($body,$mail,$idUser,$idNewsletter){
		if(is_null($idNewsletter))$idNewsletter=0;
		if(is_null($idUser))$idUser=0;
		if (UNSUB_LINK){
		
			$unsubHref = getUnsubHref($idUser);
			$body = str_replace(":|:UNSUBHREF:|:", $unsubHref, $body);
		}
		
		if (TRACK_NL){
			
			$imgLogUrl = SITE_URL.APPLICATION_PATH."imgLog.php?us=".$idUser."&nl=".$idNewsletter;
			$body .="<br /><img src='".$imgLogUrl."' width='1' height='1' border='0' alt=''>";
		}

		
		$h2t = new html2text($body);
		$textBody = $h2t->get_text();
		$return = array("htmlBody" => $body,"textBody" => $textBody);
		return $return;
	}

	



}



?>
Return current item: NmnNewsletter