bismay4u
7/11/2012 - 1:33 PM

[PHP Code for Form Mailing] PHP Code For Mailing Contents Of A Form

[PHP Code for Form Mailing] PHP Code For Mailing Contents Of A Form

<?php
$mailTo="a@a.com;b@b.com";
$mailCC="x@a.com";
$redirect="thanks.html";
$subject="Enquiry For Crew";
$from="t@t.com";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/'. phpversion() . "\r\n";

if(isset($_POST["recipient"])) $mailTo=$_POST["recipient"];
if(isset($_POST["redirect"])) $redirect=$_POST["redirect"];
if(isset($_POST["subject"])) $subject=$_POST["subject"];
if(isset($_POST["email"])) $from=$_POST["email"];

unset($_POST["env_report"]);unset($_POST["print_config"]);
unset($_POST["recipient"]);unset($_POST["redirect"]);unset($_POST["subject"]);

ob_start();
echo "<table width=800px border=0 cellpadding=1 cellspacing=0 style='margin:auto;border:2px solid #aaa;'>";
echo "<tr><th align=center>Message Header 1</th></tr>";
echo "<tr><td align=center height=3px><hr/></td></tr>";
echo "<tr><td align=center>";
echo getDataTable($_POST);
echo "</td></tr>";
echo "<tr><td align=center height=3px><hr/></td></tr>";
echo "<tr><th align=center>Message Header 2</th></tr>";
echo "<tr><td align=center height=3px><hr/></td></tr>";
echo "<tr><td align=center>";
echo getExtraArrTable(array("Position","Salary","Special_Requirements"),$_POST);
echo "</td></tr>";
echo "</table>";
$msgData=ob_get_contents();
ob_clean();

$headers .= "Reply-To: {$from}\r\n";
$headers .= "From: {$from}\r\n";

if(strlen($mailCC)>0) $mailTo="$mailTo;$mailCC";

if(strpos($mailTo,";")>1) {
	$mailTo=explode(";",$mailTo);
	foreach($mailTo as $mailID) {
		if(strlen(trim($mailID))>0) {
			mail(trim($mailID),$subject,$msgData,$headers);
		}
	}
} else {
	mail($mailTo,$subject,$msgData,$headers);
}
include($redirect);
exit();

function getDataTable($data=array()) {
	$s="<table width=100% border=0 cellpadding=1 cellspacing=0>";
	foreach($data as $a=>$b) {
		if(is_array($b)) continue;
		$a=toTitle($a);
		$ss="<tr>";
		$ss.="<th width=20% align=left>$a</th><th> :: </th>";
		$ss.="<td>$b</td>";
		$ss.="</tr>\n";
		$s.=$ss;
	}
	$s.="</table>";
	return $s;
}
function getExtraArrTable($arr,$data) {
	$s="<table width=100% border=0 cellpadding=1 cellspacing=0>";
	$s.="<tr style='background:#DDD'>";
	foreach($arr as $a) {
		$a=toTitle($a);
		$s.="<th width=20% height=20px align=center>$a</th>";
	}
	$s.="</tr>\n";
	$max=count($data[$arr[0]]);
	for($i=0;$i<$max;$i++) {
		$ss="<tr>";
		foreach($arr as $a) {
			if(isset($data[$a][$i])) {
				$a=toTitle($data[$a][$i]);
				$ss.="<td width=20% align=left>$a</td>";
			} else {
				$ss.="<td width=20% align=left>&nbsp;</td>";
			}
		}
		$ss.="</tr>\n";
		$s.=$ss;
	}
	$s.="</table>";
	return $s;
}
function toTitle($txt) {
	$txt=str_replace("_"," ",$txt);
	$txt=ucwords($txt);
	return $txt;
}
?>