unplugged_web Posted November 19, 2007 Share Posted November 19, 2007 I have a form that I'm using to send information to a database, but is it possible to send the information in an email at the same time as well?Thanks Link to comment Share on other sites More sharing options...
Ingolme Posted November 19, 2007 Share Posted November 19, 2007 In the same page that precesses the form data you can send an email with the PHP mail() function.Just inclusd the data within the message body text. Link to comment Share on other sites More sharing options...
unplugged_web Posted November 19, 2007 Author Share Posted November 19, 2007 In the same page that precesses the form data you can send an email with the PHP mail() function.Just inclusd the data within the message body text.Sorry to be really stupid, but how you I do that? To send the information to the database I'm using<?php$con = mysql_connect("server IP","username","password");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("databasename", $con);$sql="INSERT INTO table (company, address, number, email, message)VALUES('$_POST[company]','$_POST[address]','$_POST[number]','$_POST[email]','$_POST[message]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }mysql_close($con)?> Link to comment Share on other sites More sharing options...
Ingolme Posted November 19, 2007 Share Posted November 19, 2007 Just add the email script to it, something like this: <?php$con = mysql_connect("server IP","username","password");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("databasename", $con);$sql="INSERT INTO table (company, address, number, email, message)VALUES('$_POST[company]','$_POST[address]','$_POST[number]','$_POST[email]','$_POST[message]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }mysql_close($con);/* Modify this line to format the sent message to your liking: */$message = $_POST['message'] . "\n" . $_POST['company'] . "\n" . $_POST['address'];/* Your email needs a subject */$send = mail($_POST['email'],"Subject goes here",$message);if(!$send) {echo "There was an error sending the E-Mail";}?> Link to comment Share on other sites More sharing options...
unplugged_web Posted November 19, 2007 Author Share Posted November 19, 2007 Just add the email script to it, something like this:<?php$con = mysql_connect("server IP","username","password");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("databasename", $con);$sql="INSERT INTO table (company, address, number, email, message)VALUES('$_POST[company]','$_POST[address]','$_POST[number]','$_POST[email]','$_POST[message]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }mysql_close($con);/* Modify this line to format the sent message to your liking: */$message = $_POST['message'] . "\n" . $_POST['company'] . "\n" . $_POST['address'];/* Your email needs a subject */$send = mail($_POST['email'],"Subject goes here",$message);if(!$send) {echo "There was an error sending the E-Mail";}?> Where do I put the email address of where the information will be sent? Link to comment Share on other sites More sharing options...
Ingolme Posted November 19, 2007 Share Posted November 19, 2007 Actually, I wasn't sure how to manage the data you sent.I wrote this:$send = mail($_POST['email'],"Subject goes here",$message);However, I thought $_POST['email'] was the email you weere sending it to.Here's an example of how to use the mail() function:mail(to,subject,message,headers);mail("me@mydomain.com","A subject","Hello, this is the message body","From: sender@domain.com"); Link to comment Share on other sites More sharing options...
unplugged_web Posted November 19, 2007 Author Share Posted November 19, 2007 Actually, I wasn't sure how to manage the data you sent.I wrote this:$send = mail($_POST['email'],"Subject goes here",$message);However, I thought $_POST['email'] was the email you weere sending it to.Here's an example of how to use the mail() function:mail(to,subject,message,headers);mail("me@mydomain.com","A subject","Hello, this is the message body","From: sender@domain.com");Thanks for that Link to comment Share on other sites More sharing options...
unplugged_web Posted November 19, 2007 Author Share Posted November 19, 2007 So would something like: <?php$con = mysql_connect("server IP","username","password");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("databasename", $con);$sql="INSERT INTO table (company, address, number, email, message)VALUES('$_POST[company]','$_POST[address]','$_POST[number]','$_POST[email]','$_POST[message]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }mysql_close($con);ini_set("sendmail_from", 'email@mydomain.com');mail("email@mydomain.com","feedback from website",$_POST['company'],$_POST['address'],$_POST['number'],$_POST['email'],$_POST['message'],-fuser@mydomain.com");?> work then? Link to comment Share on other sites More sharing options...
Ingolme Posted November 19, 2007 Share Posted November 19, 2007 No, whatever you want to add to to message you concatenate it as a string using the concatenation operator ( . ):$message = $_POST['company'] . $_POST['address'] . $_POST['number'] . $_POST['email'] . $_POST['message'] . "-fuser@mydomain.com";mail("email@mydomain.com","feedback from website",$message"); Link to comment Share on other sites More sharing options...
unplugged_web Posted November 20, 2007 Author Share Posted November 20, 2007 No, whatever you want to add to to message you concatenate it as a string using the concatenation operator ( . ):$message = $_POST['company'] . $_POST['address'] . $_POST['number'] . $_POST['email'] . $_POST['message'] . "-fuser@mydomain.com";mail("email@mydomain.com","feedback from website",$message");Thanks, I changed the code and it now works although everything came through in one long line, no spaces, commas or anything. Is it possible to get it to come through with a space (or return) after every field? Link to comment Share on other sites More sharing options...
Synook Posted November 20, 2007 Share Posted November 20, 2007 Why not - just insert them as per a normal string $message = "{$_POST['company']}\n{$_POST['address']}\n{$_POST['number']}\n{$_POST['email']}\n{$_POST['message']}\n-fuser@mydomain.com";mail("email@mydomain.com","feedback from website",$message") Link to comment Share on other sites More sharing options...
unplugged_web Posted November 20, 2007 Author Share Posted November 20, 2007 Why not - just insert them as per a normal string$message = "{$_POST['company']}\n{$_POST['address']}\n{$_POST['number']}\n{$_POST['email']}\n{$_POST['message']}\n-fuser@mydomain.com";mail("email@mydomain.com","feedback from website",$message") Thanks Link to comment Share on other sites More sharing options...
unplugged_web Posted November 20, 2007 Author Share Posted November 20, 2007 I tested the code and it worked fine, but when I added it to the rest of the code it didn't work. It just returned an error saying: Parse error: parse error, unexpected T_VARIABLE in \\NAS37ENT\domains\s\mydomain.com\user\htdocs\test\contactForm.php on line 22Line 22 is were the code starts. The full code is:<?php$con = mysql_connect("server IP","username","password");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("databasename", $con);$sql="INSERT INTO table (company, address, number, email, message)VALUES('$_POST[company]','$_POST[address]','$_POST[number]','$_POST[email]','$_POST[message]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }mysql_close($con);$message = "{$_POST['company']}\n{$_POST['address']}\n{$_POST['number']}\n{$_POST['email']}\n{$_POST['message']}\n-fuser@mydomain.com";ini_set("sendmail_from", 'info@mydomain.com');mail("info@ mydomain.com","contact via website","$message");?> Link to comment Share on other sites More sharing options...
Ingolme Posted November 20, 2007 Share Posted November 20, 2007 There has to be some PHP code before that line. And in the code before you forgot a semi-colon (; ) at the end of the instruction. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 20, 2007 Share Posted November 20, 2007 It looks like your code contains a "[/code]" or something in it that stopped that codebox. You probably copied too much. Link to comment Share on other sites More sharing options...
unplugged_web Posted November 21, 2007 Author Share Posted November 21, 2007 There has to be some PHP code before that line. And in the code before you forgot a semi-colon (; ) at the end of the instruction.I added a semi-colon to the end of 'mysql_close($con)' and I didn't get any errors at all. I've checked the database and all of the information is there, but has been emailed as well? Link to comment Share on other sites More sharing options...
Synook Posted November 22, 2007 Share Posted November 22, 2007 Umm... check the receiving email address (info@mydomain.com) - or rather, change it to an email address that you can access... I doubt the staff of mydomain.com will appreciate your mail . Link to comment Share on other sites More sharing options...
unplugged_web Posted November 22, 2007 Author Share Posted November 22, 2007 Umm... check the receiving email address (info@mydomain.com) - or rather, change it to an email address that you can access... I doubt the staff of mydomain.com will appreciate your mail .Thanks the email has come through now. I put the email address as info@mydomain.com because the person I was building the site for didn't want me to give out their email address, thanks for pointing out though Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.