944,183 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 14503
  • PHP RSS
Mar 14th, 2005
0

E-mail form data quick and easily with PHP

Expand Post »
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.
Last edited by happygeek; Oct 28th, 2006 at 11:16 am. Reason: Formatting
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
RamiroS is offline Offline
57 posts
since Mar 2005
Jul 15th, 2005
0

Re: E-mail form data quick and easily with PHP

Quote originally posted by RamiroS ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dmouse is offline Offline
2 posts
since Jul 2005
Aug 3rd, 2005
0

Re: E-mail form data quick and easily with PHP

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mike moon is offline Offline
1 posts
since Aug 2005
Aug 16th, 2005
0

Re: E-mail form data quick and easily with PHP

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dreamworks is offline Offline
1 posts
since Aug 2005
Nov 27th, 2005
0

Re: E-mail form data quick and easily with PHP

Just wanted to say, "Thanks!" This came in handy...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joekur is offline Offline
1 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Help
Next Thread in PHP Forum Timeline: folder searching





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC