Template – Service (XML)

<?php
require_once("../../admin/includes/init_page_public.php");
require_once("../../admin/classes/class.phpmailer.php");


$test_mode = true;

if($test_mode){
	$user_name = 'Rex';
	$email = '[email protected]';
	$subject = 'test';
	$content = 'content';
}else{
	$param_array = $_POST;
	$user_name = $util->filterArrayValue($param_array, 'user_name', 'User Name', 'text', true);
	$email = $util->filterArrayValue($param_array, 'email', 'email', 'text', true);
	$subject = $util->filterArrayValue($param_array, 'subject', 'subject', 'text', true);
	$content = $util->filterArrayValue($param_array, 'content', 'content', 'text', true);
}


$err = $util->err;
if(strlen($err) > 0){
	//$err = 'Format of parameter is invalid';
	$errCode = 3;
}


if(strlen($err) == 0 && $errCode == 0){
	try{
		$contacts = $db->get_option('contacts');
		if($util->isEmpty($contacts)) throw new Exception("目前無法寄送Email");

		$contacts = json_decode($contacts, true);

	}catch(Exception $e){
		$err = $e->getMessage();
		$errCode = 9;
	}
}


if(strlen($err) == 0 && $errCode == 0){
	try{
		$mail= new PHPMailer();
		$mail->From = $email;
		$mail->FromName = $user_name;
		$mail->Subject = $subject;
		$mail->Body = $content;

		foreach($contacts as $contact){
			$mail->AddAddress($contact['url'], $contact['title']);
		}

		$result = $mail->Send();
		if(!$result) throw new Exception("目前無法寄送電子郵件,請稍侯再試");
	}catch(Exception $e){
		$err = $e->getMessage();
		$errCode = 9;
	}
}


//Write XML response
header("content-type:application/xml;charset=utf-8");

$output = '<?xml version="1.0" encoding="utf-8"?>';
$output .= '<root><status>';

if(strlen($err) == 0 && $errCode == 0){
    $output .= '<code>true</code>';
    $output .= '<detail>Action is completed</detail>';
}else{
    $output .= '<code>false</code>';
    $output .= '<detail file="' . $errFile . '" code="' . $errCode . '" lang="' . $lang . '">' . 
		'<![CDATA[' . $err . ']]></detail>';
}

$output .= '</status>';

$output .= '</root>';

echo $output;
?>