PDA

View Full Version : PHP Form Mailer


TomD
01-29-2004, 06:19 PM
The following is a PHP script I use to test a form as it is being developed as is will parse and email all of the fields without having to "know" what they all are. Something more permanent can be written when the form is finalized - or you can just leave it as it is if it works for you:<?PHP
//This is the page to display after the form is submitted.
require '/home/username/public_html/formreply.htm';
$mailto = "yourname@domain.com"; //Address to mail for info to.
$mailsub = "Auto Form Message"; //Email message subject
$mailfrom = "From:FormMailer@domain.com\n"; //Email message "From" display
$fieldnames = array_keys($HTTP_POST_VARS); //Names of the form fields
$fieldvalues = array_values($HTTP_POST_VARS); //Values of the form fields
for ($i = 0; $i < count($fieldnames); $i++) //Loop through all of the fields
{
$message .= "$fieldnames[$i]: $fieldvalues[$i]\n\n"; //Add Field Name : Field Value for each field
}
$messagetime = strftime("%A, %B %e, %Y, at %X (time zone: %Z).", time()); //Get the time
$message .= "Server date and time form was competed:\n $messagetime\n\n"; //Add time to message
$userip = $_SERVER['REMOTE_ADDR']; //Get the user IP assress
$message .= "IP Address:\n $userip\n\n"; //Add IP address to message
$RemoteHost = gethostbyaddr($userip); //Get user ISP
$message .= "Remote Host: $RemoteHost \n"; //Add ISP to message
mail($mailto, $mailsub, $message, $mailfrom); //Mail the message
?>If you save the PHP as a text file named mailform.php then your HTML would be:<form name="test" action="mailform.php" method="POST">
form stuff here
</form>I picked the basics of this up in a PHP forum some time ago and have elaborated on it as I needed.
Enjoy.

Viper007Bond
01-29-2004, 06:33 PM
Interesting. Looks handy if I was making a lot of forms. I prefer to just echo the values though until it's done as I don't make forms that often.