Intro

This is one of the fastest (and raw) ways to process form information from a web page. We assume that you have a form setup with Dreamweaver or a html editor.

Getting started

In the $_POST global variable the results of a submitted from are stored (normally when someone clicks a submit button). Using the foreach() function we can generate something like this:

# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";

# iterate the $_POST array
foreach ($_POST as $key=>$value) {
    $message.=$key.": " . $value . "\n";
}

# Send message by e-mail.
$sm=mail($to, $subject, $message);

To get the most from this example name the fields in the form with common names like Name or Address dont use field1 or text2 since the format you will get looks like this: fieldname: fieldvalue.

Since the last example creates a formatted version of every part of the $_POST array oyu will see that even the submit button is included in the message. To avoid this and to avoid unwanted fields such as hidden fields that our code may need we can create some exeptions:

# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.

# iterate the $_POST array
foreach ($_POST as $key=>$value) {
    if (!in_array($key, $ex)) {
        $message.=$key.": " . $value . "\n";
    }
}

# Send message by e-mail.
$sm=mail($to, $subject, $message);

We can also add some error handling to be sure the e-mail was sent.

# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.

# iterate the $_POST array
foreach ($_POST as $key=>$value) {
    if (!in_array($key, $ex)) {
        $message.=$key.": " . $value . "\n";
    }
}

# Send message by e-mail.
$sm=mail($to, $subject, $message);
if (!$sm) {
    print "Cannot send e-mail.";
}else{
    print "E-Mail was sent.";
}

Now, ading more toys we can add some script if the mail() funcitons fails so we can store the message as a file.

# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.

# iterate the $_POST array
foreach ($_POST as $key=>$value) {
    if (!in_array($key, $ex)) {
        $message.=$key.": " . $value . "\n";
    }
}

# Send message by e-mail.
$sm=mail($to, $subject, $message);
if (!$sm) {
    print "Cannot send e-mail.";
    # Send info to a text file
    $fp=fopen("file.txt", "a+"); # Change file to whatever filename you want. If the file is not there php will try to create it.
    $fw=fwrite($fp, $message, strlen($message));
    fclose($fp);
}else{
    print "E-Mail was sent.";
}

Thats it... easy, nice and painless.

This is one of the fastest (and raw) ways to process form information from a web page. We assume that you have a form setup with Dreamweaver or a html editor.

In the $_POST global variable the results of a submitted from are stored (normally when someone clicks a submit button). Using the foreach() function we can generate something like this:

# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";

# iterate the $_POST array
foreach ($_POST as $key=>$value) {
    $message.=$key.": " . $value . "\n";
}

# Send message by e-mail.
$sm=mail($to, $subject, $message);

To get the most from this example name the fields in the form with common names like Name or Address dont use field1 or text2 since the format you will get looks like this: fieldname: fieldvalue.

Since the last example creates a formatted version of every part of the $_POST array oyu will see that even the submit button is included in the message. To avoid this and to avoid unwanted fields such as hidden fields that our code may need we can create some exeptions:

# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.

# iterate the $_POST array
foreach ($_POST as $key=>$value) {
    if (!in_array($key, $ex)) {
        $message.=$key.": " . $value . "\n";
    }
}

# Send message by e-mail.
$sm=mail($to, $subject, $message);

We can also add some error handling to be sure the e-mail was sent.

# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.

# iterate the $_POST array
foreach ($_POST as $key=>$value) {
    if (!in_array($key, $ex)) {
        $message.=$key.": " . $value . "\n";
    }
}

# Send message by e-mail.
$sm=mail($to, $subject, $message);
if (!$sm) {
    print "Cannot send e-mail.";
}else{
    print "E-Mail was sent.";
}

Now, ading more toys we can add some script if the mail() funcitons fails so we can store the message as a file.

# Create an empty message variable.
$message="";
# The mail that we want to use to recevie the submitted information.
$to="info@mydomain.com";
# The E-Mail subject
$subject="This is a message from Page X";
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.

# iterate the $_POST array
foreach ($_POST as $key=>$value) {
    if (!in_array($key, $ex)) {
        $message.=$key.": " . $value . "\n";
    }
}

# Send message by e-mail.
$sm=mail($to, $subject, $message);
if (!$sm) {
    print "Cannot send e-mail.";
    # Send info to a text file
    $fp=fopen("file.txt", "a+"); # Change file to whatever filename you want. If the file is not there php will try to create it.
    $fw=fwrite($fp, $message, strlen($message));
    fclose($fp);
}else{
    print "E-Mail was sent.";
}

Thats it... easy, nice and painless.

it's my assumption that the mail function mail() used above only works with the local system mail service what if it is active ....mite want to contact stmp() functions

i will sound like an idiot but i am trying to post vars back to an email address from a html page ie.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#88a8e8" text="#f0fdfd">
<select name="select">
<option>1 </option>
<option>2 </option>
<option>3 </option>
<option>4 </option>
<option>5 </option>
</select>

<textarea name="test" cols="40" rows="5"></textarea>
</p>
<p>
<input type="submit" name="Post" value="Submit Job" action="<?php mail ('mail addrress@somewhere.com','Subject : help',(vars select & test),"addrress@somewhere.com");
?>"
I would like to know how to access the vars from php to post via a button , any help would be greatly appreshiated

Hi,
I was wondering if you could email the code for this email form to me. I also wasn't sure if you knew how I could also make it possible for this form to be sent to the person filling out the form as well?

I have been looking for someone to help me out. So I would greatly appreciate what you have to offer.

Ciao.

Just wanted to say, "Thanks!" This came in handy...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.