Location: PHPKode > projects > @1 File Store > bulkemail.php
<?

$MustLogin=1|2|4;
require_once("libs/header.php");

// check user level (make sure its admin login)

if($CurUser->getlevel()!=-1)
{	
	$smarty->assign("errmessage", "Access Denied!");
	$smarty->display("bulkemail.tpl");
	exit();
}

// Put java script codes here
$javascriptcodes="

function checkInput()
{
	if(document.forms[0].adminemail.value==''){
		if(confirm('Leave admin email empty?')==false){
			document.forms[0].adminemail.focus();
			return false;
		}
	}
	if(document.forms[0].subjecttext.value==''){
		if(confirm('Leave subject empty?')==false){
			document.forms[0].subjecttext.focus();
			return false;
		}
	}	
	if(document.forms[0].bodytext.value==''){
		if(confirm('Leave email body text empty?')==false){
			document.forms[0].bodytext.focus();
			return false;
		}
	}
	return true;
}
";

$smarty->assign("javascriptcodes", $javascriptcodes);

if(!isset($_POST["submit"])){

	//****************** DISPLAY FORM ******************

	

	// grab user groups (id,name)

	$table_suffix="user_groups";

	$SQL="SELECT `".DB_PREFIX.$table_suffix."`.id,`".DB_PREFIX.$table_suffix."`.group_name FROM `".DB_PREFIX.$table_suffix."` order by `".DB_PREFIX.$table_suffix."`.id";

	if(!$mysql->query($SQL)) {

		exit($mysql->error);

	}

	$group_list=array();

	while(!$mysql->EOF) {

		$group_list[]=$mysql->arr;

		$mysql->movenext();

	}

		

	$smarty->assign("submited", 0);

	$smarty->assign("self", $_SERVER['PHP_SELF']);

	$smarty->assign("groups", $group_list);

	$smarty->assign("adminemail", ADMIN_EMAIL);

	$smarty->assign("headertext", $BULK_EMAIL_HEADER);

	$smarty->assign("footertext", $BULK_EMAIL_FOOTER);

	$smarty->display("bulkemail.tpl");

}else{

	//****************** PROCESS SUBMITTED FORM ******************

	

	// grab posted data

	$recipient=$_POST["recipient"];		

	$adminemail=$_POST["adminemail"];

	$subjecttext=$_POST["subjecttext"];

	$headertext=$_POST["headertext"];

	$bodytext=$_POST["bodytext"];

	$footertext=$_POST["footertext"];

		

	// init needed vars

	$recipient_category=NULL;

	$group_id=NULL;

	$group_name=NULL;

	$email_list=array();

	$SQL=NULL;

	

	// make SQL query depend on the recipient (for grab users email)

	switch($recipient){

		case 1:{

					$recipient_category=$lang['all_admins_subadmins_groups'];

					

					$table_suffix="users";

					$SQL="SELECT `".DB_PREFIX.$table_suffix."`.email FROM `".DB_PREFIX.$table_suffix."` order by `".DB_PREFIX.$table_suffix."`.id";

					

			   } break;

		case 2:{

					$recipient_category=$lang['all_admins'];

					

					$table_suffix="users";

					$SQL="SELECT `".DB_PREFIX.$table_suffix."`.email FROM `".DB_PREFIX.$table_suffix."` WHERE `".DB_PREFIX.$table_suffix."`.level=-1 order by `".DB_PREFIX.$table_suffix."`.id";

					

			   } break;

		case 3:{

					$recipient_category=$lang['all_subadmins'];

					

					$table_suffix="users";

					$SQL="SELECT `".DB_PREFIX.$table_suffix."`.email FROM `".DB_PREFIX.$table_suffix."` WHERE `".DB_PREFIX.$table_suffix."`.level=0 order by `".DB_PREFIX.$table_suffix."`.id";

					

			   } break;

		case 4:{

					$recipient_category=$lang['all_groups'];

					

					$table_suffix="users";

					$SQL="SELECT `".DB_PREFIX.$table_suffix."`.email FROM `".DB_PREFIX.$table_suffix."` WHERE `".DB_PREFIX.$table_suffix."`.level>0 order by `".DB_PREFIX.$table_suffix."`.id";

					

			   } break;			   

		case 5:{

					$group_id=$_POST["group"];

						

					// grab group name

					$table_suffix="user_groups";

					$SQL="SELECT `".DB_PREFIX.$table_suffix."`.group_name FROM `".DB_PREFIX.$table_suffix."` WHERE `".DB_PREFIX.$table_suffix."`.id=".$group_id;

					if(!$mysql->query($SQL)) {

						exit($mysql->error);

					}

					

					$group_name=$mysql->arr[0];

					

					$recipient_category=$lang['specific_groups']." ('".$group_name."')";

					

					$table_suffix="users";

					$SQL="SELECT `".DB_PREFIX.$table_suffix."`.email FROM `".DB_PREFIX.$table_suffix."` WHERE `".DB_PREFIX.$table_suffix."`.level=".$group_id." order by `".DB_PREFIX.$table_suffix."`.id";

					

			   } break;

		default:{

					exit("Invalid Form Value!");

			    } 

		

	}

	

	// grab users email

	if(!$mysql->query($SQL)) {

		exit($mysql->error);

	}					

	while(!$mysql->EOF) {

		$email_list[]=$mysql->arr[0];

		$mysql->movenext();

	}

	

	// convert html chars

	$subjecttext=str_replace("\\\"","''",$subjecttext); $subjecttext=str_replace("\\'","'",$subjecttext);
	$headertext=str_replace("\\\"","&quot;",$headertext); $headertext=str_replace("\\'","&#039;",$headertext);
	$bodytext=str_replace("\\\"","&quot;",$bodytext); $bodytext=str_replace("\\'","&#039;",$bodytext);
	$footertext=str_replace("\\\"","&quot;",$footertext); $footertext=str_replace("\\'","&#039;",$footertext);

	// execute email sending
	if(count($email_list)){
		$bodytext=str_replace("\n","<br/>\n",$bodytext);
		$content=$headertext."<p>".$bodytext."<p>".$footertext; // you can change here of how the email content would look like

		$bemail_headers  = 'MIME-Version: 1.0' . "\r\n";
		$bemail_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$bemail_headers .= "From:".$adminemail;
		foreach($email_list as $email)
		{
			mail(trim($email), $subjecttext, $content, $bemail_headers); // send email
		}
	}

	$smarty->assign("submited", 1);

	$messages=$lang['bulk_email_sent']." <b>".$recipient_category."</b> (".count($email_list)." ".$lang['emails'].")";
	$smarty->assign("messages", $messages);	
	$smarty->assign("content", $lang['from'].": ".$adminemail."<br/>".$lang['subject'].": ".$subjecttext."<br/><br/>".$headertext."<br/><br/>".$bodytext."<br/><br/>".$footertext);
	$smarty->assign("self", $_SERVER['PHP_SELF']);
	$smarty->display("bulkemail.tpl");
}
?>
Return current item: @1 File Store