Friday, June 17, 2005

php Formail script using phpmailer functions

That if you don't have perl's FormMail installed or
there is no sendmail (in a chroot cage for example)


/**
* Formail script using smtp functions (Work done by Marius Popa aka mariuz)
* copyright under GPL
* http://www.gnu.org/copyleft/gpl.html
* Read it before stealing my code ;)
**/
require("class.phpmailer.php");
$body="";
$from="forms@example.com";
$subject=$_POST[subject];
$recipient="foo@example.com"
#$_POST[recipient];
$server="smtp.example.com";
$port="25";


// array for allowed domains (lower case please)
$referers = array('example.com', 'www.example.com');
$HttpsProtocol=$HTTP_SERVER_VARS['HTTPS'];
// add upper case referrers
$size = sizeof($referers);
for($i = 0; $i < $size; $i++){
$referers[] = strtoupper($referers[$i]);
}

// check referers
for($i = 0; $i < sizeof($referers); $i++){
if(substr($HTTP_SERVER_VARS['HTTP_REFERER'], 7, strlen($referers[$i])) == $referers[$i]){
$bad_referer = FALSE;
break;
} //if we are on https connection check if it's a good guy (not a bad referer)
elseif ($HttpsProtocol=="on"){
if(substr($HTTP_SERVER_VARS['HTTP_REFERER'], 8 , strlen($referers[$i])) == $referers[$i])
{
$bad_referer = FALSE;
break;
}
}else {

$bad_referer = TRUE;
}

}
if($bad_referer){
header('Location: bad_referer.php');
//echo "Bad Referer :$HTTP_SERVER_VARS['HTTP']";
exit;
}


if ($HTTP_POST_VARS)
{
while (list($lvar, $lvalue) = each($HTTP_POST_VARS))
{
$body=$body."$lvar=$lvalue |";
}
}

$mail = new PHPMailer();
$mail->From = $from;
$mail->FromName = "Form";
$mail->Host = $server;
$mail->Mailer = "smtp";


$mail->AddAddress($recipient);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHtml(true);
$mail->Send();

if(isset($HTTP_POST_VARS['redirect']) && ($HTTP_POST_VARS['redirect']!="")) {
$redirect_url=$HTTP_POST_VARS['redirect'];
header("Location: $redirect_url");
}
else {
print "Thank you for submiting the folowing request";
echo "$body";
}
?>


::..::

No comments: