OT - E-Mail from form script?

A forum for, well, anything really.
Gripes, moans, ideas or just general chit chat. EXCEPT SPAM!!! - Don't just register to post here - IT WILL GET DELETED!!!
Post Reply
tepidarium
Mega User
Mega User
Posts: 169
Joined: Sun Oct 05, 2003 4:21 am

OT - E-Mail from form script?

Post by tepidarium »

Hi,
Does anyone know of a good script that will send to an e-mail address the results of a web form?

Thanks...
User avatar
John
 Team
 Team
Posts: 5967
Joined: Sun May 19, 2002 8:23 pm
Location: Phoenix, AZ
Contact:

Post by John »

John
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

Yeah... I've used that one myself in previous incarnations of our website. It'll require that your webserver (or host) allows CGI. You can also use mailto as the form's action, using the visiotr's own email client to send the mail, but that doesn't give you control over the output, and various browsers put up different messages when you use mailto.

Kevin
tepidarium
Mega User
Mega User
Posts: 169
Joined: Sun Oct 05, 2003 4:21 am

Post by tepidarium »

thanks guys, I'm going to check this out...have a great weekend!
waitman
Beginner
Beginner
Posts: 5
Joined: Thu May 13, 2004 8:22 am
Location: Buena Park
Contact:

php formmail

Post by waitman »

i had a customer using a formmail perl script, this thing has had security issues in the past (however they have probably been resolved.)

so I replaced it with a php script i threw together, seems to work similar to formmail. i wanted to mimick the functionality of formmail without messing around changing his form html.

the HTML email sending part could be a bit better, but works for the most part.

1. mail.php

Code: Select all

<?php
$redirect = '/thankyou.html';
$recipient = 'addr1@freakinexample.com,addr2@freakinexample.com';

$e = @explode(',',trim($_POST['env_report']));
$env = '';
if (count($e)>0)
{
	foreach ($e as $value)
	{
		if ($value=='REMOTE_HOST')
		{
			$_SERVER[$value]=gethostbyaddr($_SERVER[$value]);
		}
		$env .= '<tr><td valign="top">'.$value.'</td><td valign="top">'.htmlspecialchars($_SERVER[$value]).'&nbsp;</td></tr>';
	}
}

$r = @explode(',',trim($_POST['required']));
$required = array();
$err='';
$emailreq = 0;
if (count($r)>0)
{
	foreach ($r as $value)
	{
		if ($value=='email') $emailreq = 1;
		if (strlen(trim($_POST[$value]))<1)
		{
			$err .= "You did not enter a value for ".$value.", which is required.<br>";
		}
	}
}

if ($emailreq==1)
{
	if(!eregi("^[a-zA-Z0-9_\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $_POST['email']))
	{
		$err .= 'You did not enter a valid email address.<br>';
	}
}

//check required fields

if (strlen($err)>0)
{
	echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Error - Required Fields Missing!</title>
<style type="text/css">
body { Font-Family: Georgia, Helvetica, Tahoma, Verdana, sans-serif; }
blockquote { color: Red; }
</style>
</head>

<body>
<h3>Error - Required Fields Missing!</h3>
<p>Please note the errors below and <a href="javascript:history.back(0);">Click Here</a> to return to the previous page.</p>
<blockquote>'.$err.'</blockquote>
<p>Thank You.</p>
</body>
</html>
';

	exit();
}

$message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Form Submission '.date('Y-m-d H:i:s').'</title>
<style type="text/css">
body { Font-Family: Georgia, Helvetica, Tahoma, Verdana, sans-serif; }
td { background-color: #eeeeee; border: 1px solid #cccccc; padding: 5px; }
</style>
</head>

<body>
<h3>Form Submission '.date('Y-m-d H:i:s').'</h3>
<table border="0" width="100%">';

$_POST['email'] = '<a href="mailto:'.urlencode($_POST['email']).'">'.htmlspecialchars($_POST['email']).'</a>';
$s = @explode(',',trim($_POST['sort']));
if (count($s)>0)
{
	foreach ($s as $value)
	{
		if ($value != 'email')
		{
			$_POST[$value] = htmlspecialchars($_POST[$value]);
		}
		if ($_POST['print_blank_fields']=='0')
		{
			if (strlen(trim($_POST[$value]))>0)
			{
				$message .= '<tr><td valign="top">'.str_replace('_',' ',$value).'</td><td valign="top">'.str_replace("\n",'<br>',$_POST[$value])."</td></tr>";
			}
		} else {
			$message .= '<tr><td valign="top">'.str_replace('_',' ',$value).'</td><td valign="top">'.str_replace("\n",'<br>',$_POST[$value])."&nbsp;</td></tr>";
		}
	}
}
$message .= '<tr><td colspan="2">&nbsp;</td></tr>';
if (strlen($env)>0)
{
	$message .= '<tr><td colspan="2"><strong>Environment Variables</strong></td></tr>'.$env;
}

$message .= '</table><p>Have a Great Day!<br><br><em>The Web Server</em></p></body></html>'."\r\n\r\n\r\n";

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Web Server <web.server@freakinexample.com>\r\n";

mail ($recipient,$_POST['subject'],$message,$headers);

Header('Location: '.$redirect);
exit();
?>

2. form.html (simple example)

Code: Select all

<form method="post" action="mail.php">
<input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR,REMOTE_USER,HTTP_USER_AGENT">
<input type="hidden" name="print_blank_fields" value="0">
<input type="hidden" name="subject" value="Web Site Contact">
<input type="hidden" name="required" value="realname,email">
<input type="hidden" name="sort" value="Company,Home_Office,realname,email,subject">
<input type="text" size="25" maxlength="25" name="realname">
<input type="text" size="25" maxlength="38" name="Company">
<input type="text" size="25" maxlength="38" name="email">
<input type="Radio" name="Home_Office" value="Home">
<input type="Radio" name="Home_Office" value="Home">
<input type="submit" name="submit" value="submit">
</form>
Waitman Gobble
http://emkdesign.com/
Post Reply