954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

E-mail form data quick and easily with PHP

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:

[php]
# 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);
[/php]

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:

[php]
# 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);
[/php]

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

[php]
# 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.";
}

[/php]

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


[php]
# 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.";
}
[/php]

Thats it... easy, nice and painless.

RamiroS
Junior Poster in Training
57 posts since Mar 2005
Reputation Points: 10
Solved Threads: 2
 

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:

[php] # 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); [/php]

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:

[php] # 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); [/php]

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

[php] # 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."; }

[/php]

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

[php] # 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."; } [/php]

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

dmouse
Newbie Poster
2 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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

Untitled Document1 2 3 4 5
"
I would like to know how to access the vars from php to post via a button , any help would be greatly appreshiated

mike moon
Newbie Poster
1 post since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Dreamworks
Newbie Poster
1 post since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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

joekur
Newbie Poster
1 post since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You