?
Solved

How do i send html form data via email using php?

Posted on 2009-04-16
10
Medium Priority
?
438 Views
Last Modified: 2012-05-06
I created a simple  html form and a php file but each time I submit the form and i open the email , its empty. Asa result i have block my if statement to that checks to make sure certain variable are not empty because even i enter data in the form it was coming up empty hence redirecting to the form again. can someone help please?


HTMLl form :
<html>
FORM action="scripts/contact_form.php" method="post" enctype="text/plain" name="contactform" id="Contactform">
				
				<tr ><td align="right">Reason for contact</td> <td align="left">
				  <select name="reason[]" multiple="yes">
				    <option value="passport"> Passport</option>
				    <option value="trade">Trade</option>
				    <option value="tourism">Tourism</option>
				    <option value="visa">Visa</option>
			      </select>
				  </td>
				</tr>
				<tr ><td align="right"><span class="style2">*</span>Full Name</td> 
				<td align="left"><input type="text" name="fname1" size="52"  value=""></td></tr>
                <tr >
                  <td align="right">Address</td> 
                  <td align="left"><input type="text" name="address" size ="52" ></td></tr>
				<tr >
				  <td align="right"><span class="style2">*</span>Phone</td> 
				  <td align="left"><input type="text" name="phone" size ="52"></td></tr>
				<tr ><td align="right">Fax</td> <td align="left"><input type="text" name="fax" size ="52"></td></tr>
				<tr >
				  <td align="right"><span class="style2">*</span>Email</td> 
				  <td align="left"><input type="text" name="semail" size ="52"></td></tr>
				<tr >
				  <td align="right"><span class="style2">*</span>Comments</td>
				  <td align="left"><textarea type="text" name="comments" cols=5 rows=4 ></textarea></td></tr>
                
				<tr> <td colspan="2" align="center"><input type="submit" value="Submit" />   <input type="reset" /></td></tr>
	</FORM>  
        </table>
 
</html>
 
------------------------------------------------------------------------
 
PHP file:
 
<?php
 
// Makes sure required fields are not empty - currently blocked
 
/*
 if (($_POST[fname] == "") || ($_POST[semail] == "") || ($_POST[comments] == "")) {
header("Location: ../tzecontact.html");
exit;
}
*/
 
$msg = "Contact Request from Website\n";
$msg .= "Sender's Name:  $_POST[fname1]\n";
$msg .= "Sender's Address:  $_POST[address]\n";
$msg .= "Sender's Phone:  $_POST[phone]\n";
$msg .= "Sender's Fax:  $_POST[fax]\n";
$msg .= "Sender's Email:  $_POST[semail]\n";
$msg .= "Reason:  $_POST[reason]\n"; 
$msg .= "Message:  $_POST[comments]\n";
$to = "jonny007@hotmail.com";
$subject = "Contact Request from Website";
$mailheaders = "From: Tan <info@tan.org>\n";
$mailheaders .= "Reply to:  $_POST[$semail]\n";
 
mail ($to, $subject, $msg, $mailheaders);
header( "Location: ../thankyou.html" );
 
?>
 
----------------------------------------
 
Email results:
 
 
Contact Request from Website
From: 	Tan (info@tan.org)	
Sent: 	Fri 4/17/09 1:42 AM
To: 	jonny007@mail.com
 
Reply to:  
 
 
Contact Request from Website
Sender's Name:  
Sender's Address:  
Sender's Phone:  
Sender's Fax:  
Sender's Email:  
Reason:  
Message:

Open in new window

0
Comment
Question by:motomoto
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 180 total points
ID: 24164479
Try changing the lines in the php file from:

$msg .= "Sender's Phone:  $_POST[phone]\n";

to:

$msg .= "Sender's Phone:  " . $_POST["phone"] . "\n";

Do that for each line that contains a $_POST variable.
0
 

Author Comment

by:motomoto
ID: 24165007
i updated the php code as suggested, the code looked promising but the result was still the same .
Is there something else I'm missing?

btw why the extra dots and "" ?

<?php
 
// Makes sure required fields are not empty
 
/*
 if (($_POST[fname] == "") || ($_POST[semail] == "") || ($_POST[comments] == "")) {
header("Location: ../tzecontact.html");
exit;
}
*/
 
$msg = "Contact Request from Website\n";
$msg .= "Sender's Name:  " . $_POST["fname1"] . "\n";
$msg .= "Sender's Address: " .  $_POST["address"] . "\n";
$msg .= "Sender's Phone:  " . $_POST["phone"] . "\n";
$msg .= "Sender's Fax:  " . $_POST["fax"] . "\n";
$msg .= "Sender's Email:  " . $_POST["semail"] . "\n";
$msg .= "Reason:  " . $_POST["reason"] . "\n"; 
$msg .= "Message:  " . $_POST["comments"] . "\n";
$to = "jonny007@hotmail.com";
$subject = "Contact Request from Website";
$mailheaders = "From: Tan<info@tanzaniaembassy.org>\n";
$mailheaders .= "Reply to:  " . $_POST["semail"] . "\n";
 
mail ($to, $subject, $msg, $mailheaders);
header( "Location: ../thankyou.html" );
 
?>

Open in new window

0
 
LVL 11

Assisted Solution

by:Chris Gralike
Chris Gralike earned 510 total points
ID: 24165849
Is there a reason you are using two seperate files to handle the mail?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Assisted Solution

by:Chris Gralike
Chris Gralike earned 510 total points
ID: 24166143
The code below should be saved in one file like mail.php

Just open the "mail.php" file and have a look..

Rgrds,
<?php
if(isset($_POST['submit'])){
        // If something is posted, lets validate for empty, not checking format at the moment so everything is accepted...  //
        $values['fname1'] = (!empty($_POST['fname1'])) ? trim(htmlspecialchars($_POST['fname1'])) : false;
        $values['address'] = (!empty($_POST['address'])) ? trim(htmlspecialchars($_POST['address'])) : "&nbsp;";
        $values['phone'] = (!empty($_POST['phone'])) ? trim(htmlspecialchars($_POST['phone'])) : false;
        $values['fax'] = (!empty($_POST['fax'])) ? trim(htmlspecialchars($_POST['fax'])) : "&nbsp;";
        $values['semail'] = (!empty($_POST['semail'])) ? trim(htmlspecialchars($_POST['semail'])) : false;
        $values['reason'] = (!empty($_POST['reason'])) ? trim(htmlspecialchars($_POST['reason'])) : false;
        $values['comments'] = (!empty($_POST['comments'])) ? trim(htmlspecialchars($_POST['comments'])) : false;
 
        // Lets generate and print error messages if any //
        echo "<div id='errors'> \r\n";
        $error = false;
        foreach($values as $key => $value){
                if(!$value){
                        echo "<div class='e_message'>$key is a required field, it looks like its empty</div>\r\n";
                        $error = true;
                }
        }
        echo "</div> \r\n";
 
        // Lets see if there where any errors if so show the form again, else lets mail this :D//
        if(!$error){
                $msg = "Contact Request from Website\n";
                $msg .= 'Sender\'s Name: '.$values['fname1']."\r\n";
                $msg .= 'Sender\'s Address:'.$values['address']."\r\n";
                $msg .= 'Sender\'s Phone: '.$values['phone']."\r\n";
                $msg .= 'Sender\'s Fax: '.$values['fax']."\r\n";
                $msg .= 'Sender\'s Email: '.$values['semail']."\r\n";
                $msg .= 'Reason: '.$values['reason'] ."\r\n";
                $msg .= 'Message:'.$values['comments']."\r\n";
                $to = 'jonny007@hotmail.com';
                $subject = 'Contact Request from Website';
                $mailheaders = "From: Tan <info@tan.org> \r\n";
                $mailheaders .= 'Reply to: '.$_POST['$semail']."\r\n";
 
                if(mail ($to, $subject, $msg, $mailheaders)){
                        echo "<div>All the Thankyou HTML</div>";
                        exit;
                }else{
                        echo "<div>OEPS Something went wrong, please come back later to resend the mail!";
                        exit;
                }
        }
 
 
}else{
        // Make an empty array if the post button isnt clicked (first visit)
        $values = array(
                'fname1'        => '',
                'address'       => '',
                'phone'         => '',
                'fax'           => '',
                'semail'        => '',
                'reason'        => '',
                'comments'      => '');
}
 
?>
<html>
        <FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" name="contactform" id="Contactform">
                <table>
                                <tr ><td align="right">Reason for contact</td> <td align="left">
                                  <select name="reason">
                                  <?php //  Generate the reasons and show the one selected on error //
 
                                  $reasons = array('passport','trade','tourism','visa');
 
                                  foreach($reasons as $key){
                                        if($key == $values['reason']){
                                                echo "<option value='$key' SELECTED>$key</option>";
                                    }else{
                                                echo "<option value='$key'>$key</option>";
                                        }
                                  }
                                  ?>
                              </select>
                                  </td>
                                </tr>
                                <tr ><td align="right"><span class="style2">*</span>Full Name</td>
                                <td align="left"><input type="text" name="fname1" size="52"  value="<?php echo $values['fname1']; ?>"/></td></tr>
                <tr >
                  <td align="right">Address</td>
                  <td align="left"><input type="text" name="address" size ="52" value="<?php echo $values['address']; ?>"/></td></tr>
                                <tr >
                                  <td align="right"><span class="style2">*</span>Phone</td>
                                  <td align="left"><input type="text" name="phone" size ="52" value="<?php echo $values['phone']; ?>"></td></tr>
                                <tr ><td align="right">Fax</td> <td align="left"><input type="text" name="fax" size ="52" value="<?php echo $values['fax']; ?>"></td></tr>
                                <tr >
                                  <td align="right"><span class="style2">*</span>Email</td>
                                  <td align="left"><input type="text" name="semail" size ="52" value="<?php echo $values['semail']; ?>"></td></tr>
                                <tr >
                                  <td align="right"><span class="style2">*</span>Comments</td>
                                  <td align="left"><textarea type="text" name="comments" cols=5 rows=4 ><?php echo $values['comments']; ?></textarea></td></tr>
 
                                <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="submit" />   <input type="reset" /></td></tr>
        </table>
        </FORM>
</html>

Open in new window

0
 
LVL 11

Assisted Solution

by:Chris Gralike
Chris Gralike earned 510 total points
ID: 24166157
You still need to correct / alter the the Mailing part, i notice there are still $_POST[] vars in there, these should be altered to $values['semail']

Rgrds,
0
 
LVL 16

Assisted Solution

by:rbudj
rbudj earned 60 total points
ID: 24167129
what you have is fine.  you do not need to concatenate anymore than you already are on your msg variables.  You do, however, need single quotes inside your brackets such as:

$msg .= "Sender's Address:  $_POST['address']\n";
$msg .= "Sender's Phone:  $_POST['phone']\n";
0
 
LVL 16

Expert Comment

by:rbudj
ID: 24167200
also, I always declare my variables first:

$fname1 = $_POST['fname1'];
$address = $_POST['address'];

then you can write:
$msg .= "Sender's Name:  $fname1\n";
$msg .= "Sender's Address:  $address\n";

I'm not sure if it is a requirement but my form template works great this way.
0
 
LVL 11

Accepted Solution

by:
Chris Gralike earned 510 total points
ID: 24167811
The reason you form isnt working (passing post values to the next file) is because you added this in the <form> field....

enctype="text/plain"enctype="text/plain"

remove it and it should actually post something ;)

rgrds,
0
 

Author Closing Comment

by:motomoto
ID: 31571290
Most of the solutions provided were easy to understand but one was a bit hard to get. Overall the help provided was great,  Thanks!
0
 

Author Comment

by:motomoto
ID: 24173043
Thanks Guys for all your help. I really appreciate it.
I got my script working now, in case you are interested of how it looks see below....
<?php
 
// Makes sure required fields are not empty
 
 if (($_POST[fname] == "") || ($_POST[phone] == "") || ($_POST[semail] == "") || ($_POST[comments] == "")) {
header("Location: ../requiredcontact.html");
exit;
}
 
// Checked "reason" presence and sets to $value
 
if (!empty($_POST["reason"])) {
 
foreach ($_POST["reason"] as $value ) {
 
 
//Set email body variables
 
$msg  = "Name:  " . $_POST["fname"] . "\n";
$msg .= "Address: " . $_POST["address"] . "\n";
$msg .= "Phone:  " . $_POST["phone"] . "\n";
$msg .= "Fax:  " . $_POST["fax"] . "\n";
$msg .= "Email:  " . $_POST["semail"] . "\n\n";
$msg .= "Reason:  " . $value . "\n\n"; 
$msg .= "Message:  " . $_POST["comments"] . "\n";
 
$subject = "Contact Request from: " . $_POST["fname"] . " ";
$mailheaders = "From: Tan-USA <info@tan.org>\n";
$mailheaders .= "Reply to:  " . $_POST["semail"] . "\n";
 
 
//Set delivery email variables
 
$passport_email = "passport@tan.org";
$trade_email = "trade@tan.org";
$tourism_email = "tourism@tan.org";
$visa_email = "visa@tan.org";
 
 
//Delivery conditions driven from "reason"
 
if ($value == "Passport") {
mail ($passport_email, $subject, $msg, $mailheaders);
}
elseif ($value == "Trade") {
mail ($trade_email, $subject, $msg, $mailheaders);
}
elseif ($value == "Tourism") {
mail ($tourism_email, $subject, $msg, $mailheaders);
}
elseif ($value == "Visa") {
mail ($visa_email, $subject, $msg, $mailheaders);
exit;
}
 
 
//Redirect user to thank you page after successful delivery
 
if (($value == "Passport") || ($value == "Trade") || ($value == "Visa") || ($value == "Tourism")) {
header( "Location: ../thankyou.html" );
exit;
}
 
}
exit;
}
 
?>

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month16 days, 1 hour left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question