Angelojoseph17 Posted January 8, 2010 Share Posted January 8, 2010 I have an form , which has a few fields, when filled, the form calls a php file called insert.php which inserts the records from the form into a database and also what I'm trying to is send an email of the records within the same script. I'm new to php can anyone help. below is the code. <?php $con = mysql_connect("localhost","me","mypassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("final_project", $con); $sql="INSERT INTO Persons (Username,FirstName, LastName, Department,Issue,Category) VALUES ('$_POST[username]', '$_POST[Firstname]','$_POST[Lastname]','$_POST[Department]','$_POST[issue]','$_POST[Category]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con) $email = $HTTP_POST_VARS['email']; $Firstname = $HTTP_POST_VARS['Firstname']; $Lastname = $HTTP_POST_VARS['Lastname']; $Issue = $HTTP_POST_VARS['Issue']; $Category = $HTTP_POST_VARS['Category']; if( mail($email,$Firstname,$Lastname,$Issue,$Category)){ print ("your mail was sucessfully sent to $email"); }else print "unable to send mail to $to"; echo "<br>"; echo "You records have been updated"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/187668-using-email-function-to-send-email-not-working-help/ Share on other sites More sharing options...
JAY6390 Posted January 8, 2010 Share Posted January 8, 2010 Could it be that HTTP_POST_VARS is deprecated http://www.php.net/manual/en/reserved.variables.post.php Quote Link to comment https://forums.phpfreaks.com/topic/187668-using-email-function-to-send-email-not-working-help/#findComment-990756 Share on other sites More sharing options...
Angelojoseph17 Posted January 8, 2010 Author Share Posted January 8, 2010 Ive Got another form which uses this feature and it works fine. Quote Link to comment https://forums.phpfreaks.com/topic/187668-using-email-function-to-send-email-not-working-help/#findComment-990759 Share on other sites More sharing options...
r-it Posted January 8, 2010 Share Posted January 8, 2010 i concur with jay that http_post_vars may be deprecated, i do not understand that you used $_POST for database insertion and not for storing into variables, and you are doing double the work, here is how i would code it: <?php $con = mysql_connect("localhost","me","mypassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("final_project", $con); $username = $_POST['Username']; $email = $_POST['email']; $Firstname = $_POST['Firstname']; $Lastname = $_POST['Lastname']; $Issue = $_POST['Issue']; $Category = $_POST['Category']; $Dept = $_POST[Department]; $sql="INSERT INTO Persons (Username,FirstName, LastName, Department,Issue,Category) VALUES ('".$username."', '".$Firstname."','".$Lastname."','".$Dept."','".$Issue."','".$Category."')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con) if( mail($email,$Firstname,$Lastname,$Issue,$Category)){ print ("your mail was sucessfully sent to $email"); }else print "unable to send mail to $email"; echo "<br>"; echo "You records have been updated"; ?> check your code against this code. You had a $to variable which was not assigned anything, you should also sanitize your inputs, use functions such as trim, addshashes and strip_tags, hope you get it sorted Quote Link to comment https://forums.phpfreaks.com/topic/187668-using-email-function-to-send-email-not-working-help/#findComment-990843 Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2010 Share Posted January 8, 2010 You also did not provide any clue as to what your code is actually doing. Is it outputting the "your mail was successfully sent ..." message or the "unable to send mail ..." message? For debugging purposes, add the following two lines of code immediately after the first opening <?php tag on the page to get php to show all errors it detects when your code runs - ini_set("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/187668-using-email-function-to-send-email-not-working-help/#findComment-990913 Share on other sites More sharing options...
Angelojoseph17 Posted January 8, 2010 Author Share Posted January 8, 2010 Thanks for your help so far guys. The aim for the code is to insert the data from the form into the database, then send an email with all of the data from the form to the email variable in the form field. So far the data is inserted into the database and if statement seems to run and tells me it is succesful, but i recieve no email. Quote Link to comment https://forums.phpfreaks.com/topic/187668-using-email-function-to-send-email-not-working-help/#findComment-990927 Share on other sites More sharing options...
r-it Posted January 8, 2010 Share Posted January 8, 2010 are you working on localhost or a live webserver, if(localhost) upload your stuff to a web server, you'll test it better there else make sure your smtp is adequately setup in your php.ini file Quote Link to comment https://forums.phpfreaks.com/topic/187668-using-email-function-to-send-email-not-working-help/#findComment-990929 Share on other sites More sharing options...
Angelojoseph17 Posted January 8, 2010 Author Share Posted January 8, 2010 i'm working on a live webserver i have at my house. As im using ssmtp which is setup on that machine. So i'm at adds really to where it's going wrong. Quote Link to comment https://forums.phpfreaks.com/topic/187668-using-email-function-to-send-email-not-working-help/#findComment-990931 Share on other sites More sharing options...
r-it Posted January 8, 2010 Share Posted January 8, 2010 dude, you're doing the mail wrong check this link: http://php.net/manual/en/function.mail.php mail($email,$Firstname,$Lastname,$Issue,$Category) should be: $msg = $Firstname.'\n'.$Lastname.'\n'.$Issue.'\n'.$Category; $subj = "Posted data"; mail($email, $subj, $msg) that should work Quote Link to comment https://forums.phpfreaks.com/topic/187668-using-email-function-to-send-email-not-working-help/#findComment-990932 Share on other sites More sharing options...
Angelojoseph17 Posted January 8, 2010 Author Share Posted January 8, 2010 thanks r-it that worked, but the way i did it before worked too, seemed my email account just rejected all those emails. Thanks ever so much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/187668-using-email-function-to-send-email-not-working-help/#findComment-990933 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.