Andrick Borderguard & Ketchup Co.
code.junkie: generic php emailer
 [ 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:
  • The normal way to cope with $_POST[recipient] and $_POST[mailtitle] is to put them as hidden fields inside the form. I'll sometimes go ahead and yank those to bury the vars inside the script.
  • I usually just dump it, wrap the headers around it, change the error messages and call it a day. I like doing error checking via JavaScript.
Home About: T.E.D. Andrick About: Our Philosphy About: The Name Portfolio: Web Design Portfolio: Template Design Portfolio: Identity Design Portfolio: Print Design Clients/Projects: Client List Clients/Projects: Projects List Clients/Projects: Special Projects More: Filter.Junkie Thin Chrome Gold Text Infamous MacOS 'Gel' Buttons More: Code.Junkie YoungPup's dropdown menus The IE6 PNG display hack A really simple JavaScript image button rollover A not-simple CSS image button rollover Sliding DIVs Fading DIVs The IE/ActiveX Control hack Flash and JavaScript variables Flash & XML text marquee The gerenic emailer The beauty of the dynamic header & footer The generic picture viewer More: Links Contact: Employment Contact: Analog Space