Form Responses to MySql and Email

I have always either written form responses to a mysql db or popped off an email on submit. In this case I need to do both. I am successfully writing the responses to the db. My form posts to an external page (snippet attached) that contains the script. I need to incorporate the mechanism to send an email with the form responses into this script.

I am also concerned about the potential for a sql injection attack, I know my security is slack.

I would be happy to make the latter part of this question a new question.

Thanks!
<?php
	$con = mysql_connect("localhost","dbuser","dbpass");
	if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
	mysql_select_db("dbname", $con);
	
	$sql="INSERT INTO contact (fname, lname, email, phone, address1, address2, title, company, referrer, list, message)
	VALUES
	('$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[phone]','$_POST[address1]','$_POST[address2]','$_POST[title]','$_POST[company]','$_POST[referrer]','$_POST[list]','$_POST[message]')";
	
	if (!mysql_query($sql,$con))
		{
			die('Error: ' . mysql_error());
		}
		
	echo "<script>alert('Thanks, your message has been sent.');</script>";
	echo"<script language='javascript'>window.location.href='/contact.php'</script>";
	mysql_close($con)
?>

Open in new window

LVL 16
s8webAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AielloJCommented:
s8web:

After the line that inserts the data into your table, add a call to the PHP mail() function as show below.

  $sql="INSERT INTO contact (fname, lname, email, phone, address1, address2, title, company, referrer, list, message)
 VALUES
  ('$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[phone]','$_POST[address1]','$_POST[address2]','$_POST[title]','$_POST[company]','$_POST[referrer]','$_POST[list]','$_POST[message]')";

// First, wrap lines longer than 70 characters as mail function does not like long lines.
  $strMessage = wordwrap($_POST[message], 70);

// Send email
mail($_POST[address1], $_POST[title], $strMessage);

This should provide the basic outline for you.  If you only want to send the email on a successful database write, then I would add the code in an ELSE statement in your if statement on line 13.

Regards,

AielloJ

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AielloJCommented:
s8web:

You can also, and may be required to by your hosting company, send the optional header with the sender and reply to email information.

$strHeaders = 'From: <insert your email address here>' . "\r\n" .
    'Reply-To: <insert your email address here>' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($_POST[address1], $_POST[title], $strMessage, $strHeader);

If you need to send the email to both addresses listed, then create the send to string with a comma separating the addresses and substitute for the first parameter as shown.

$strEmailTo = $_POST[address1] . ', ' $_POST[address2];
mail($strEmailTo, $_POST[title], $strMessage, $strHeader);

AielloJ
s8webAuthor Commented:
Thanks Aiello J.

$strMessage = wordwrap($_POST[message], 70); didn't fly for some reason though.

I will post my final script next. I posted a related question - http://www.experts-exchange.com/Database/MySQL/Q_26549718.html

I will post my final script in a moment.
s8webAuthor Commented:
Here is my final script. It works well but needs a little more security.
<?php
	$con = mysql_connect("localhost","dbuser","dbpass");
	if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
	
	mysql_select_db("dbname", $con);
	
		$sql="INSERT INTO contact (fname, lname, email, phone, address1, address2, title, company, referrer, list, message)
		VALUES
		('$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[phone]','$_POST[address1]','$_POST[address2]','$_POST[title]','$_POST[company]','$_POST[referrer]','$_POST[list]','$_POST[message]')";
	
	$emailFrom = "$email";
	$emailTo = "email@email.com";
	$subject = "A message has been sent.";
	
	$fname = strip_tags($_POST['fname']); 
	$lname = strip_tags($_POST['lname']); 
	$email = strip_tags($_POST['email']);
	$phone = strip_tags($_POST['phone']); 
	$address1 = strip_tags($_POST['address1']); 
	$address2 = strip_tags($_POST['address2']);
	$title = strip_tags($_POST['title']); 
	$company = strip_tags($_POST['company']); 
	$referrer = strip_tags($_POST['referrer']);
	$list = strip_tags($_POST['list']);
	$message = strip_tags(stripslashes($_POST['message'])); 
	
	$body = "Name: ".$fname."\n";
	$body .= "Email: ".$email."\n";
	$body .= "Phone: ".$phone."\n";
	$body .= "Street Address: ".$address1."\n";
	$body .= "City, State, Zip: ".$address2."\n";
	$body .= "Title: ".$title."\n";
	$body .= "Company: ".$company."\n";
	$body .= "Referrer: ".$referrer."\n";
	$body .= "List: ".$list."\n";
	$body .= "Message: ".$message."\n";
	
	$headers = "From: ".$email."\n";
	$headers .= "Reply-To:".$email."\n";
	
	mail($emailTo, $subject, $body, $headers);
	
	if (!mysql_query($sql,$con))
		{
			die('Error: ' . mysql_error());
		}
	echo "<script>alert('Thanks, your message has been sent.');</script>";
	echo"<script language='javascript'>window.location.href='/contact.php'</script>";
	mysql_close($con)
?> 

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.