I wrote a script that will take data and post it to a database no problem, but at the same time I need the data to be emailed to two different email addresses and I never get an email. My code looks pretty solid and I am not sure what else to do since it brings me to my success page but I still don't get an email. (I changed the email address for security so they are real emails). Any idea's? Thanks.

<?php

function post_to_db() {
$conn = mysql_connect("db2401.snip.net", "dbo328063853", "idirect2");

mysql_select_db("db328063853", $conn);

if (!$_POST['fname'] | !$_POST['lname'] | !$_POST['addr1'] | !$_POST['city'] | !$_POST['state'] | !$_POST['zip'] | !$_POST['phone1'] | !$_POST['email'] | !$_POST['dob'] | !$_POST['esttotal']) {
die('ERROR: You left a field blank!  Go back and try again');
}

$fname = mysql_real_escape_string ($_POST['fname']);
$lname = mysql_real_escape_string ($_POST['lname']);
$addr1 = mysql_real_escape_string ($_POST['addr1']);
$city = mysql_real_escape_string ($_POST['city']);
$state = mysql_real_escape_string ($_POST['state']);
$zip = mysql_real_escape_string ($_POST['zip']);
$phone1 = mysql_real_escape_string ($_POST['phone1']);
$email = mysql_real_escape_string ($_POST['email']);
$dob = mysql_real_escape_string ($_POST['dob']);
$esttotal = mysql_real_escape_string ($_POST['esttotal']);
$ip=$_SERVER['REMOTE_ADDR'];

global $fname, $lname, $addr1, $city, $state, $zip, $phone1, $email, $dob, $esttotal, $ip;

$sql="INSERT INTO form_data (fname, lname, addr1, city, state, zip, phone1, email, dob, esttotal, ip)
VALUES
('$fname','$lname','$addr1','$city','$state','$zip','$phone1','$email','$dob','$esttotal','$ip')";

if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }
}

post_to_db();

mysql_close($conn);

function emailAll() {

	function checkEmail($email) {
		if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {return false;}
		return true;
		}
		
		$_ip=$_SERVER['REMOTE_ADDR'];
		
	if(isset($_POST["submitForm"])){
		
		$_fname = ($_POST["fname"]);
		$_lname = ($_POST["lname"]);
		$_addr1 = ($_POST["addr1"]);
		$_city = ($_POST["city"]);
		$_state = ($_POST["state"]);	
		$_zip = ($_POST["zip"]);
		$_phone1 = ($_POST["phone1"]);
		$_email = ($_POST["email"]);
		$_dob = ($_POST["dob"]);
		$_esttotal = ($_POST["esttotal"]);
		
		$_body = "Message has been sent from data collection form\n\n";
		$_subject = "E-Mail from your web form";
		
		if($_fname){
			$_body .= "First Name: $_fname\n\n";
		}
		
		if($_lname){
			$_body .= "Last Name: $_lname\n\n";
		}
		
		if($_addr1){
			$_body .= "Address 1: $_addr1\n\n";
		}
		
		if($_city){
			$_body .= "City: $_city\n\n";
		}
		
		if($_state){
			$_body .= "State: $_state\n\n";
		}
		
		if($_zip){
			$_body .= "Zip Code: $_zip\n\n";
		}
		
		if($_phone1){
			$_body .= "Phone 1: $_phone1\n\n";
		}

		if($_email){
			$_body .= "Email Address: $_email\n\n";
		}

		if($_dob){
			$_body .= "Date of Birth: $_dob\n\n";
		}

		if($_esttotal){
			$_body .= "Estimated Total: $_esttotal\n\n";
		}
	
		if($_ip){
			$_body .= "IP Address of User: $_ip\n\n";
		}
		
		$_to = array();

		//get recipients
		$_to[] = "name@email.com" . ", ";
		$_to[] .= "name@email.com";
		
		//set the headers
				
		$headers = "From: <name@domain.com>";
    	
    	mail($_to[$i], $_subject, $_text, $_headers);
			
	}
}

emailAll();

header('Location: success.html');

?>
$_to = array();

//get recipients
$_to[] = "name@email.com" . ", ";
$_to[] .= "name@email.com";

//set the headers
$headers = "From: <name@domain.com>";

mail($_to[$i], $_subject, $_text, $_headers);

I don't see $i in the above code. And why are you using an array? Change $_to[] to $_to and remove the line $_to = array();

Personally I like to test if the mail is really send like so:

$mail_sent = @mail($_to,...... 
if (!$mail_sent) { // through in an error }

Ok i made the changes. I was using the array and $i for counting the amount of emails allowed and never changed it because I am stupid. I added in some error messages and everything still seems to work fine, and the email is sent and I get a message in my inbox, but when you open it nothing is there...just an empty message. Updated code below.

<?php

function post_to_db() {
$conn = mysql_connect("db2401.perfora.net", "dbo328063853", "idirect2");

mysql_select_db("db328063853", $conn);

if (!$_POST['fname'] | !$_POST['lname'] | !$_POST['addr1'] | !$_POST['city'] | !$_POST['state'] | !$_POST['zip'] | !$_POST['phone1'] | !$_POST['email'] | !$_POST['dob'] | !$_POST['esttotal']) {
die('ERROR: You left a field blank!  Go back and try again');
}

$fname = mysql_real_escape_string ($_POST['fname']);
$lname = mysql_real_escape_string ($_POST['lname']);
$addr1 = mysql_real_escape_string ($_POST['addr1']);
$city = mysql_real_escape_string ($_POST['city']);
$state = mysql_real_escape_string ($_POST['state']);
$zip = mysql_real_escape_string ($_POST['zip']);
$phone1 = mysql_real_escape_string ($_POST['phone1']);
$email = mysql_real_escape_string ($_POST['email']);
$dob = mysql_real_escape_string ($_POST['dob']);
$esttotal = mysql_real_escape_string ($_POST['esttotal']);
$ip=$_SERVER['REMOTE_ADDR'];

global $fname, $lname, $addr1, $city, $state, $zip, $phone1, $email, $dob, $esttotal, $ip;

$sql="INSERT INTO form_data (fname, lname, addr1, city, state, zip, phone1, email, dob, esttotal, ip)
VALUES
('$fname','$lname','$addr1','$city','$state','$zip','$phone1','$email','$dob','$esttotal','$ip')";

if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }
}

post_to_db();

mysql_close($conn);

function emailAll() {

	function checkEmail($email) {
		if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {return false;}
		return true;
		}
		
		$_ip=$_SERVER['REMOTE_ADDR'];
		
	if(isset($_POST["submitForm"])){
		
		$_fname = ($_POST["fname"]);
		$_lname = ($_POST["lname"]);
		$_addr1 = ($_POST["addr1"]);
		$_city = ($_POST["city"]);
		$_state = ($_POST["state"]);	
		$_zip = ($_POST["zip"]);
		$_phone1 = ($_POST["phone1"]);
		$_email = ($_POST["email"]);
		$_dob = ($_POST["dob"]);
		$_esttotal = ($_POST["esttotal"]);
		
		$_body = "Message has been sent from data collection form\n\n";
		$_subject = "E-Mail from your web form";
		
		if($_fname){
			$_body .= "First Name: $_fname\n\n";
		}
		
		if($_lname){
			$_body .= "Last Name: $_lname\n\n";
		}
		
		if($_addr1){
			$_body .= "Address 1: $_addr1\n\n";
		}
		
		if($_city){
			$_body .= "City: $_city\n\n";
		}
		
		if($_state){
			$_body .= "State: $_state\n\n";
		}
		
		if($_zip){
			$_body .= "Zip Code: $_zip\n\n";
		}
		
		if($_phone1){
			$_body .= "Phone 1: $_phone1\n\n";
		}

		if($_email){
			$_body .= "Email Address: $_email\n\n";
		}

		if($_dob){
			$_body .= "Date of Birth: $_dob\n\n";
		}

		if($_esttotal){
			$_body .= "Estimated Total: $_esttotal\n\n";
		}
	
		if($_ip){
			$_body .= "IP Address of User: $_ip\n\n";
		}

		//get recipients
		$_to = "name@email.com" . ", ";
		$_to .= "name@email.com";
		
		//set the headers
				
		$headers = "From: <name@domain.com>";
    	
    	$mail_sent= @mail($_to, $_subject, $_text, $_headers);
    	if (!$mail_sent) { echo "SOMETHING WENT WRONG"; }
    		else {
	    		echo "IT WORKED!";
    		}
	}
}

emailAll();

?>

The mail looks like a text only one to me, so change the headers part in your code to something like this:

//set the headers
$headers = 'From: webmaster@example.com\n';
$headers .= 'Reply-To: webmaster@example.com\n';
$headers .= 'Return-Path: webmaster@example.com\n';
$headers .= "Message-ID: <comming from ".$_SERVER['SERVER_NAME'].">\n";
$headers .= "X-Mailer: PHP v".phpversion()."\n";
$msg = "";
$msg .= "Content-Type: text/plain; charset=iso-8859-1\n";
$msg .= "Content-Transfer-Encoding: 8bit\n";
$msg .= $_text."\n\n";

All those headers are strictly speaking not needed, but it will help mail-servers when mail is send but cannot be delivered.
Almost forgot, change the @mail line also:

$mail_sent= @mail($_to, $_subject, $msg, $_headers);
if (!$mail_sent) {

Added those lines. Sends the email but is still empty....Appreciate the help but I am still stuck with this.

Added those lines. Sends the email but is still empty....Appreciate the help but I am still stuck with this.

Did test the code on my own server. It doesn't work the way it should work, because some variables are mixed.

You use $_body for the text in the body and I used $msg .= $_text. Also noticed that

$msg = "";
$msg .= "Content-Type: text/plain; charset=iso-8859-1\n";
$msg .= "Content-Transfer-Encoding: 8bit\n";
$msg .= $_text."\n\n";

this isn't needed. Only need this when you send multipart-mime (html + text) e-mail.
So, to solve it, remove the above code and change your @mail line to

$mail_sent= @mail($_to, $_subject, $_body, $_headers);

That way the mail is send out and the body contains text.

Very nice! That did it! Appreciate the help everything came through the email perfectly.

can you share your final code?

This is my final code alot of people have been asking....

<?php

function post_to_db() {
$conn = mysql_connect("localhost", "username", "password");

mysql_select_db("nameofdb", $conn);

if (!$_POST['fname'] | !$_POST['lname'] | !$_POST['addr1'] | !$_POST['city'] | !$_POST['state'] | !$_POST['zip'] | !$_POST['phone1'] | !$_POST['email'] | !$_POST['dob'] | !$_POST['esttotal']) {
die('ERROR: You left a field blank!  Go back and try again');
}

$fname = mysql_real_escape_string ($_POST['fname']);
$lname = mysql_real_escape_string ($_POST['lname']);
$addr1 = mysql_real_escape_string ($_POST['addr1']);
$city = mysql_real_escape_string ($_POST['city']);
$state = mysql_real_escape_string ($_POST['state']);
$zip = mysql_real_escape_string ($_POST['zip']);
$phone1 = mysql_real_escape_string ($_POST['phone1']);
$email = mysql_real_escape_string ($_POST['email']);
$dob = mysql_real_escape_string ($_POST['dob']);
$esttotal = mysql_real_escape_string ($_POST['esttotal']);
$ip=$_SERVER['REMOTE_ADDR'];

$sql="INSERT INTO form_data (fname, lname, addr1, city, state, zip, phone1, email, dob, esttotal, ip)
VALUES
('$fname','$lname','$addr1','$city','$state','$zip','$phone1','$email','$dob','$esttotal','$ip')";

if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }
}

post_to_db();

mysql_close($conn);

function emailAll() {

	function checkEmail($email) {
		if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {return false;}
		return true;
		}
		
		$_ip=$_SERVER['REMOTE_ADDR'];
		
	if(isset($_POST["submitForm"])){
		
		$_fname = ($_POST["fname"]);
		$_lname = ($_POST["lname"]);
		$_addr1 = ($_POST["addr1"]);
		$_city = ($_POST["city"]);
		$_state = ($_POST["state"]);	
		$_zip = ($_POST["zip"]);
		$_phone1 = ($_POST["phone1"]);
		$_email = ($_POST["email"]);
		$_dob = ($_POST["dob"]);
		$_esttotal = ($_POST["esttotal"]);
		
		$_body = "Message has been sent from data collection form\n\n";
		$_subject = "E-Mail from your web form";
		
		if($_fname){
			$_body .= "First Name: $_fname\n\n";
		}
		
		if($_lname){
			$_body .= "Last Name: $_lname\n\n";
		}
		
		if($_addr1){
			$_body .= "Address 1: $_addr1\n\n";
		}
		
		if($_city){
			$_body .= "City: $_city\n\n";
		}
		
		if($_state){
			$_body .= "State: $_state\n\n";
		}
		
		if($_zip){
			$_body .= "Zip Code: $_zip\n\n";
		}
		
		if($_phone1){
			$_body .= "Phone 1: $_phone1\n\n";
		}

		if($_email){
			$_body .= "Email Address: $_email\n\n";
		}

		if($_dob){
			$_body .= "Date of Birth: $_dob\n\n";
		}

		if($_esttotal){
			$_body .= "Estimated Total: $_esttotal\n\n";
		}
	
		if($_ip){
			$_body .= "IP Address of User: $_ip\n\n";
		}

		//get recipients
		$_to = "email1@website.com" . ", ";
		$_to .= "email2@website.com";
		
		//set the headers
				
		$headers = "From: <webmaster@example.com>";
		$headers .= 'Reply-To: webmaster@example.com\n';
		$headers .= 'Return-Path: webmaster@example.com\n';
		$headers .= "Message-ID: <comming from ".$_SERVER['SERVER_NAME'].">\n";
		$headers .= "X-Mailer: PHP v".phpversion()."\n";

    	$mail_sent= @mail($_to, $_subject, $_body, $_headers);
    	if (!$mail_sent) { echo "SOMETHING WENT WRONG"; }
    		else {
	    		echo "IT WORKED!";
    		}
	}
}

emailAll();

?>

Thanks very much. I'm having a problem though. I'm getting a parse error on line 17: (I called the file sample.php)
Parse error: syntax error, unexpected T_DNUMBER in /home1/xxxxx/public_html/xxxxx/sample.php on line 17

Have you found this error?

thanks, okrobie

Nah never got that error. Are you using all of the same name fields as in the code? (fname, lname, etc) If not make sure that they are all the same. Also make sure you copied the code right with all of the quotes in the right spots.

Look above line 17 for errors on your code.

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.