E-mail form data quick and easily with PHP

RamiroS 0 Tallied Votes 244 Views Share

This is one of the fastest ways to get data submitted by a HTML form, make it readable and send it by e-mail. Fast and easy. From the raw example to some basic functionality.

# To get more information about this code and some extras please visit:
# http://www.daniweb.com/techtalkforums/showthread.php?p=101880#post101880

# 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);
Member Avatar for LastMitch
LastMitch

@RamiroS

This is one of the fastest ways to get data submitted by a HTML form, make it readable and send it by e-mail. Fast and easy. From the raw example to some basic functionality.

You know it would be nice if you post the whole contact form so people can test it out.

It's sad that noone acknowledge your thread.

I know it's 7 years to late.

Thanks for sharing!

mlesniak 22 Light Poster

I hear you, and I agree.
Here's an HTML form:

<html>
<head>
    <title>PHP_Email_Form</title>
</head>

<body>
<h1>PHP Email Form</h1>
<form action="EmailFormData.php" method="post" name="mailform">
<table align=center cellpadding=5 cellspacing=0>
   <tr>
       <td>First:</td>
       <td><input type="text" name="data1" size="12"></td>
   </tr>
   <tr>
       <td>Second:</td>
       <td><input type="text" name="data2" size="12"></td>
   </tr>
   <tr>
       <td>Third:</td>
       <td><input type="text" name="data3" size="12">
       <input type="submit" name="Send" value="Send">
       </td>
   </tr>
</table>
</form>
</body>
</html>

and here's the PHP script, EmailFormData.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>EmailTest01</title>
</head>

<body BGCOLOR=#0099FF>

<!-- This script is designed to be a simple email test -->

<?PHP 
# Create an empty message variable.
$message="";
# The mail that we want to use to receive the submitted information.
$to="YourEmailAddress@YourDomain";
$MailFrom = "FromAddressForDisplay@YourDomain";
# The E-Mail subject
$subject="This is a message from PHPEmailForm.html";
# iterate the $_POST array
foreach ($_POST as $key=>$value)
{
    $message.=$key.": " . $value . "\n";
}
# Send message by e-mail.
if($sm=mail($to, $subject, $message, "From: $MailFrom"))
{
   print("<B><CENTER><FONT COLOR=BLUE>Your email has been successfully sent</FONT></CENTER></B>\n");
}
else
{
   print("<B><CENTER><FONT COLOR=RED>OOPS. Something's gone wrong. Try debugging your script.</FONT></CENTER></B>\n");
}

?>

</body>
</html>

I've just added a bit of colour to the feedback.
I hope you like it.

LastMitch commented: Thanks for the nice update! +11
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.