Location: PHPKode > projects > Content*Builder > contentbuilder/plugins/newsletter2/newsletterSend.inc.php
<?php
/* Mail einbinden */
if(!class_exists("Mail")) {
	require_once 'Mail.php';
}

/* Mail_Mime einbinden */
if(!class_exists("Mail_Mime")) {
	require_once 'Mail/mime.php';
}

/* Date für Versandberechnungen einbinden */
if(!class_exists("Date")) {
	require_once 'Date.php';
}

/*
* wir sind mal optimistisch
*/
$nlError = false;

/*
* erster einfacher Check. Zum Versand wird nur die ID des Newsletters benötigt
*/
if(empty($newsletterID)) {
	$nlError = true;
}

/*
* Abrufen der Medien Infos
*/
$nlTplTypeID = $nlOption->getSingleOption("nlTplTypeID");
$nlMediaID   = $nlOption->getSingleOption("nlMediaID");

$myMedia     = new MediaManagement();
$myMedia->userID = $HTTP_SESSION_VARS[user];
$myMedia->setOptionObject($nlOption);

$nlGeneralQuery = mysql_fetch_assoc(mysql_query("SELECT * FROM ".TABLE."_nl_general WHERE newsletterID = '$newsletterID'"));
if(($nlGeneralQuery[userID] != $cbUserID) && !(ADMIN & $cbPerms)) {
	$nlError = true;
	$nlErrorArr[] = $newsletterText["infoSendErrorPermission"];
}

/*
* weiterer Check, ob es diesen Newsletter überhaupt gibt
*/
if($nlGeneralQuery[newsletterID] == "") {
	$nlError = true;
	$nlErrorArr[] = $newsletterText["infoSendErrorPermission"];
}

/*
* Info Array für die Erfolgs- / Fehlermeldung
*/
$nlSend = array();

/*
* Versand Check. Berechnung der Zeit, die zwischen dem letzten Versand und dem jetzigen liegt.
*/
if(1 & $nlGeneralQuery[options]) {
	if(!($nlMultipleSendProtectionOverride)) {
		$nlError = true;
		
		$nlCurrentDate       =& new Date();
		$nlSendDate          =& new Date($nlGeneralQuery[sendTime]);
		$nlSendDayDifference = Date_Calc::dateDiff($nlCurrentDate->getDay(), $nlCurrentDate->getMonth(), $nlCurrentDate->getYear(), $nlSendDate->getDay(), $nlSendDate->getMonth(), $nlSendDate->getYear());
		
		$nlSend['multipleSendProtection']['set'] = 1;
		$nlSend['multipleSendProtection']['dayDiff'] = $nlSendDayDifference;
	}
}


if(!$nlError) {
	/* KategorieID rausfiltern */
	$nlCategoryID = $nlGeneralQuery[categoryID];
	
	/* Kategorie auslesen */
	$nlCategoryQuery = mysql_fetch_assoc(mysql_query("SELECT * FROM ".TABLE."_nl_category WHERE categoryID = '$nlCategoryID'"));
	
	/* Title herausfiltern, Subject zusammensetzen */
	$nlTitle = $nlGeneralQuery[title];
	$nlSubject = $nlCategoryQuery[name] ." ". $nlTitle; 
	
	/* Content Versionen auslesen */
	$nlContentQuery = mysql_query("SELECT * FROM ".TABLE."_nl_content WHERE newsletterID = '$newsletterID'");
	while($nlContentRow = mysql_fetch_assoc($nlContentQuery)) {
		if($nlContentRow[typeID] == 1) {
			$nlPlainVersion = $nlContentRow[text];
		}
		if($nlContentRow[typeID] == 2) {
			$nlHTMLVersion  = $nlContentRow[text];
		}
	}

	/*
	* Mail Parameter setzen
	*/
	if(substr(PHP_OS, 0, 3) == 'WIN') {
		$crlf="\r\n";
	} else {
		$crlf="\n";
	}
	
	$hdrs = array (
		'From'    => $nlOption->getSingleOption("from"),
		'Subject' => $nlSubject
	);
	
	$nlMimeObject = new Mail_mime($crlf);
	if($nlOption->getSingleOption("sendMethod") != "mail") {
		$nlSendParams = $nlOption->getMultipleOption($nlOption->getSingleOption("sendMethod"));
		$nlMailObject =& Mail::factory($nlOption->getSingleOption("sendMethod"), $nlSendParams);
	} else {
		$nlMailObject =& Mail::factory($nlOption->getSingleOption("sendMethod"));
	}
	
	/* Template Check */
	$nlTemplateInfos = $myMedia->getFileValues($nlGeneralQuery[templateID]);
	
	$_templateAvailable = 0;
	if($nlTemplateInfos[typeID] == $nlTplTypeID && $nlTemplateInfos[branchID] == $nlMediaID) {
		$_templateAvailable = 1;
		/*
		* Suchen und einbinden von Smarty. Klappt das nicht, wird auch net geparst
		*/
		$smarty = new Smarty;
		$smarty->template_dir = $cbDirUploads."files/".$nlTemplateInfos[branchName];
		$smarty->compile_dir  = $t_basedir."compiled/";
	}
	
	$nlSubscribersQuery = mysql_query("SELECT * FROM ".TABLE."_nl_subscription AS sub, ".TABLE."_community_user AS user WHERE sub.userID = user.userID AND sub.categoryID = '$nlCategoryID'");
	while($nlSubscribersRow = mysql_fetch_assoc($nlSubscribersQuery)) {
		/*
		* Kopieren der Originalversionen
		*/
		$nlPlainParsedVersion = $nlPlainVersion;
		$nlHTMLParsedVersion  = $nlHTMLVersion;
		
		if($_templateAvailable) {
			$smarty->assign(
				array ( "receiver"            => $nlSubscribersRow, 
						"newsletterTitle"     => $nlTitle,
						"categoryName"        => $nlCategoryQuery[name],
						"categoryID"          => $nlCategoryQuery[categoryID],
						"categoryDescription" => $nlCategoryQuery[description],
						"plainContent"        => $nlPlainVersion,
						"htmlContent"         => $nlHTMLVersion
				)
			);
			
			/*
			* getrenntes Parsen der Plain und HTML Mails mit einer 
			* Template Datei. Es muss 2 mal gemacht werden, da 
			* MIME Mails beide Versionen enthalten
			*/
			$smarty->assign( "templateType", "plain" );
			$nlPlainParsedVersion = $smarty->fetch($nlTemplateInfos[filename]);
			
			$smarty->assign( "templateType", "html" );
			$nlHTMLParsedVersion = $smarty->fetch($nlTemplateInfos[filename]);
		}
		
		/*
		* Ob nun Smarty benutzt wurde oder nicht, $nlXXXXParsedVersion
		* enthält den zu sendenden Content
		*/
		$nlMimeObject->setTXTBody($nlPlainParsedVersion);
		$nlMimeObject->setHTMLBody($nlHTMLParsedVersion);
		
		$nlMimeBody    = $nlMimeObject->get();
		$nlMimeHeaders = $nlMimeObject->headers($hdrs);
	
		/* Absenden der Plain Mail an den User */
		if($nlSubscribersRow[typeID] == 1) {
			$result = $nlMailObject->send($nlSubscribersRow[email], $hdrs, $nlPlainParsedVersion);
		}
		/* Absenden der HTML Mail an den User */
		if($nlSubscribersRow[typeID] == 2) {
			$result = $nlMailObject->send($nlSubscribersRow[email], $nlMimeHeaders, $nlMimeBody);
		}
		
	}
	
	/*
	* Fehlermeldung bei Versand Problemen erzeugen
	*/
	if($result == true) {
		$nlSend['success'] = 1;
	} else {
		if(is_object($result)) {
			$nlSend['error']['code']    = $result->getCode();
			$nlSend['error']['message'] = $result->getMessage();
			$nlSend['error']['set']     = 1;
		}
	}
	
	$nlNewOptions = (1 | $nlGeneralQuery[options]);
	$sendUpdateQuery = mysql_query("UPDATE ".TABLE."_nl_general SET options = '$nlNewOptions', sendTime = NOW() WHERE newsletterID = '$newsletterID'");
}

$nlSend['newsletterID'] = $newsletterID;

$tpl->assign("nlSend", $nlSend);
$tpl->assign("nlSend_set", "1");

if(count($nlErrorArr) > 1) {
	$tpl->assign("errorSet", "1");
	$tpl->assign("errorList", $nlErrorArr);
}

$mainPluginContent[]  = $tpl->fetch($config['templates']."newsletter.template", null, "pluginNL".$cbUserID);
$mainMessageContent[] = $tpl->fetch($config['templates']."messages.template", null, "pluginNL".$cbUserID);
?>
Return current item: Content*Builder