|
[ The Generic PHP Emailer ]
Nothing really special here, and again nothing unique -- just what the title says, a baseline generic PHP email script. Where I got this is lost in the sands of antiquity; it may have been a cookbook or a scripting site somewhere [though due to the internal commenting, I'd suspect the latter; if you're reading this and you actually wrote it, my thanks]. It's not hyper-intelligent or even particularly secure -- but it generally doesn't have to be.
But it is simple and certainly effective enough, and I've never had an issue with it. Elementary -- but hey, next time I need it I know where to find it. the code:
if ($_SERVER['REQUEST_METHOD']=="POST") {
// In testing, if you get an Bad referer error
// comment out or remove the next three lines
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg="Values submitted by the user:\n";
foreach($_POST as $key => $val){
if (is_array($val)){
$msg.="Item: $key\n";
foreach($val as $v){
$v = stripslashes($v);
$msg.=" $v\n";
}
} else {
$val = stripslashes($val);
$msg.="$key: $val\n";
}
}
$msg = str_replace("submit: submit", "", $msg);
$additional_headers = "From: $HTTP_POST_VARS[from]
<$HTTP_POST_VARS[email]>\n"."Return-Path: $HTTP_POST_VARS[email]\n";
if (mail($_POST[recipient], $_POST[mailtitle], $msg, $additional_headers)) {
echo "Thank you for your email. We will process it as soon as possible.";
} else {
echo "We're sorry; your email did not go through.";
}
}
considerations:
|
