Jump to content

Trying to email form data after successful insert into mysql


lightningrod

Recommended Posts

Not sure exactly what my problem is here, but i have looked at this at least a hundred times and I just can't figure out what is going on.  Everything works great except for the sending the email part.  It displays the error "There was a problem sending the mail".  The form field checking works fine, the insert into mysql works fine - - but it won't send the email.  I have tried using double quotes and single quotes for the email information ($to, $subject, etc...) I have even eliminated the form data from the email information and it still doesn't send.  I am hopelessly stuck at this point  :shrug:  Any ideas??  Code below:

 

<html>
<body bgcolor = 'blue'>
<div align = 'center'>
<h1>test FORM</h1>
</div>
<p>
<?php

If ($_POST['submit']) //if the Submit button pressed
{
//collect form data

$fname = $_POST['FName'];
$lname = $_POST['LName'];
$email = $_POST['Email'];
$tel = $_POST['Tel'];
$mess = $_POST['Mess'];


$errorstring = "";

if (!$fname)
	$errorstring = $errorstring."First Name<br>";
if (!$lname)
	$errorstring = $errorstring."Last Name<br>";
if (!$email)
	$errorstring = $errorstring."Email<br>";

if ($errorstring !="")
	echo "<div align = 'center'><b>Please fill out the following fields:</b><br><font color = 'red'><b>$errorstring</b></font></div>";
else
{

	$fname = mysql_real_escape_string($fname);
	$lname = mysql_real_escape_string($lname);
	$email = mysql_real_escape_string($email);
	$tel = mysql_real_escape_string($tel);
	$mess = mysql_real_escape_string($mess);		

		$con = mysql_connect("localhost","user","password");
		if (!$con)
			{
				die('Could not connect: ' . mysql_error());
			}

		mysql_select_db("mec", $con);

		$sql="INSERT INTO formtest (FName, LName, Title, Tel, Email, Mess)
		VALUES ('$fname','$lname','$title','$tel','$email','$mess')";

		if (!mysql_query($sql,$con))
			{
				die('Error: ' . mysql_error());
			}
		$to = 'myemailaddress';
		$subject = 'test form';
		$message = 'Hello';
		$headers = 'From Me';
		$sent = mail($to, $subject, $message, $headers);
		if($sent)
			{print "Email successfully sent";}
		else
			{print "There was an error sending the mail";}

		mysql_close($con);

}


}

?>
<p>
<form action= 'testmecform.php' method= 'POST'>

                  
  <table width = '640' border = '0' align = 'center'>
    <tr> 
      <td align = 'right'><b>First Name</b></td>
      <td><input type = 'text' name = 'FName' value = '<?php echo $fname; ?>' size = '25'></td>
      <td><div align = 'right'><b>Telephone</b></div></td>
      <td><input type = 'text' name = 'Tel'  value = '<?php echo $tel; ?>' size = '25'></td>
    </tr>
    <tr> 
      <td align = 'right'><b>Last Name</b></td>
      <td><input type = 'text' name = 'LName' value = '<?php echo $lname; ?>' size = '25'></td>
  <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align = 'right'><b>Email</b></td>
      <td><input type = 'text' name = 'Email'  value = '<?php echo $email; ?>' size = '25'></td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <th colspan = '4'><b>Please enter any additional information here:</b></th>
    </tr>
    <tr> 
      <th colspan = '4'><textarea name = 'Mess' cols = '50' rows = '10'><?php echo $mess; ?></textarea></th>
    </tr>
    <tr> 
      <th colspan = '4'><b>Please make sure all information is correct before submitting</b></th>
    </tr>
    <tr> 
      <th colspan = '4'><input type = 'submit' name = 'submit' value = 'Submit Form'></th>
    </tr>
  </table>
</form>
</body>
</html>

Link to comment
Share on other sites

If this is what is really in the $header variable:

<?php
$headers = 'From Me';
?>

Then the header is not correct and your system is dumping the email message into the bit bucket. The "From:" header needs at least

<?php
$headers = "From: avalid@email.address.here";
?>

Ken

Link to comment
Share on other sites

If this is what is really in the $header variable:

<?php
$headers = 'From Me';
?>

Then the header is not correct and your system is dumping the email message into the bit bucket. The "From:" header needs at least

<?php
$headers = "From: avalid@email.address.here";
?>

Ken

 

I actually had

$headers = 'From: $email';

initially and it didn't work either.  Single quotes OR double quotes got same result :(

Link to comment
Share on other sites

It could be that your ISP/Host will not let email message go out of the system if the "From" address doesn't contain a valid email address for your system.

 

Try a simple script to see if you can send email at all:

<?php
mail('your@email.address.here','Test Message','This is a test email message','From: your@email.address.here');
?>

 

If that works, change the headers variable to be:

<?php
$headers = "From: your@email.address.here\nReply-to: $email";
?>

This will make the email message you send look like it's coming from your valid email address and add a Reply-to header so when you reply to the message, it will go to the correct address.

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.