lhcpr Posted June 28, 2008 Share Posted June 28, 2008 Hello all - just FYI: total php newbie. I have been using phpformgenerator (sourceforge project) to design a form and php code. Everything seems to work just fine. The form data is designed to be sent to an email recipient (x@yahoo.com). BUT... when the form data is received by the recipient, the senders email address is xyz@domainhost.com. What I wish to do is change the sender/from address (i.e currently xyz@domainhost.com) to something like xyz@gmail.com. The reason why I wish to do this is that becasue when my email receives the form data by email, it is identified as spam / junk. I have posted my code and the output below. Is there any way I can change the address that sends the form data (in line with the above)? Thanks Graham <?php $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/")); session_start(); if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) ) { // Checkbox handling $field_5_opts = $_POST['field_5'][0]; // the value of $_POST['field_6'] is xyz@gmail.com $email = $_POST['field_6']; mail("x@yahoo.com","No development at Turrella Reserve","Form data: " . $_POST['field_1'] . " " . $_POST['field_2'] . " says no to development at Turrella Reserve" . " Email address: " . $_POST['field_3'] . " This is what " . $_POST['field_1'] . " has to say: " . $_POST['field_4'] . " Check if you wish to receive updates on development progress: $field_5_opts ",$email); include("confirm.html"); } else { echo "Invalid Captcha String."; } ?> ---------------------------------------------------------------------------------------------- The output is: x@gmail.com Form data: Kittles Smith says no to development at Turrella Reserve Email address: kittles@buchanan.com This is what Kittle has to say: fjeskfjdks Check if you wish to receive updates on development progress: Yes Quote Link to comment Share on other sites More sharing options...
phpzone Posted June 28, 2008 Share Posted June 28, 2008 <?php mail( $recepient, $subject, $mail_body, "From: {$sender}"); ?> Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 28, 2008 Share Posted June 28, 2008 lhcpr , that's an extremely messy way to code your mail tag. Preferably you want to be assigning the message portion to a variable. You also need to collect the result of the mail function and act accordingly. $headers = "from: $sender\n"; $res = mail($recipient, $subject, $message, $headers); if ($res){ echo "Message Sent"; } else { echo "Message not sent"; } Quote Link to comment Share on other sites More sharing options...
lhcpr Posted June 29, 2008 Author Share Posted June 29, 2008 Hi there everyone - thanks to phpzone and br0ken for your assistance. I have tidied up the code as suggested and have the following. Will this work with regards to changing the senders email address? CHeers, Graham <?php $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/")); session_start(); if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) ) { // Checkbox handling $field_5_opts = $_POST['field_5'][0]; // Sender email is predefined as a hidden html field $email = $_POST['field_6']; // Message body $message = $_POST['field_1'] . " " . $_POST['field_2'] . " says no to development at Turrella Reserve" . " Email address: " . $_POST['field_3'] . " This is what " . $_POST['field_1'] . " has to say: " . $_POST['field_4'] . " Check if you wish to receive updates on development progress: $field_5_opts"; //Mail function mail("xyz@yahoo.com","No development at Turrella Reserve","From: $email"); include("confirm.html"); } else { echo "Invalid Captcha String."; } ?> Quote Link to comment Share on other sites More sharing options...
lhcpr Posted June 29, 2008 Author Share Posted June 29, 2008 Oopps - realised that I didn't put $message in mail function Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 Rather than asking us if it will work, run the code and tell us whether or not it will work. If not tell us the error message returned and we can look into it for you Quote Link to comment Share on other sites More sharing options...
lhcpr Posted June 29, 2008 Author Share Posted June 29, 2008 I'm sorry - that was kinda lazy wasn't it!! In anycase, the code works. I am looking to extend the currently working code to include an elseif statment that changes the mail handling depending on whether or not a check box is checked as "Yes". I have modified the code but now getting the following error message: Parse error: syntax error, unexpected T_STRING in processor.php on line 46 My code is: <?php $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/")); session_start(); //Validation and handling if check box is equal to Yes if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) && ($_POST['field_5']=="Yes") ) { // Checkbox handling $field_5_opts = $_POST['field_5'][0]; // Sender email is predefined as a hidden html field $email = $_POST['field_6']; // Message body $message = $_POST['field_1'] . " " . $_POST['field_2'] . " says no to development at Turrella Reserve" . " Email address: " . $_POST['field_3'] . " This is what " . $_POST['field_1'] . " has to say: " . $_POST['field_4'] . " Check if you wish to receive updates on development progress: $field_5_opts"; //Mail function mail("xyz@gmail.com,xyz@yahoo.com","No development at Turrella Reserve",$message,"From: $email"); include("confirm.html"); } //Validation and handling if check box is not equal to Yes elseif( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) && ($_POST['field_5']!="Yes") ) { // Checkbox handling $field_5_opts = $_POST['field_5'][0]; // Sender email is predefined as a hidden html field $email = $_POST['field_6']; // Message body $message = $_POST['field_1'] . " " . $_POST['field_2'] . " says no to development at Turrella Reserve" . " Email address: " . $_POST['field_3'] . " This is what " . $_POST['field_1'] . " has to say: " . $_POST['field_4'] . "; // Mail function mail("xyz@gmail.com","No development at Turrella Reserve",$message,"From: $email"); include("confirm.html"); } else { echo "Invalid Captcha String."; } ?> Any ideas about where I have gone wrong? Thanks, Graham Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 Give this a try. <?php $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/")); session_start(); //Validation and handling if check box is equal to Yes if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) && ($_POST['field_5']=="Yes") ) { // Checkbox handling $field_5_opts = $_POST['field_5'][0]; // Sender email is predefined as a hidden html field $email = $_POST['field_6']; // Message body $message = $_POST['field_1'] . " " . $_POST['field_2'] . " says no to development at Turrella Reserve" . " Email address: " . $_POST['field_3'] . " This is what " . $_POST['field_1'] . " has to say: " . $_POST['field_4'] . " Check if you wish to receive updates on development progress: $field_5_opts"; //Mail function mail("xyz@gmail.com,xyz@yahoo.com","No development at Turrella Reserve",$message,"From: $email"); include("confirm.html"); } elseif( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) && ($_POST['field_5']!="Yes") ) { // Checkbox handling $field_5_opts = $_POST['field_5'][0]; // Sender email is predefined as a hidden html field $email = $_POST['field_6']; // Message body $message = $_POST['field_1']." ".$_POST['field_2']." says no to development at Turrella Reserve\nEmail address: " $_POST['field_3']."This is what ".$_POST['field_1'] . " has to say: ".$_POST['field_4']; // Clean message string $message = strip_tags($message); // Mail function mail("xyz@gmail.com","No development at Turrella Reserve",$message,"From: $email"); include("confirm.html"); } else { echo "Invalid Captcha String."; } ?> Quote Link to comment Share on other sites More sharing options...
lhcpr Posted June 30, 2008 Author Share Posted June 30, 2008 Hi br0ken - thanks for the feedback. I take your point about formatting and see a silly error in my code ( " . $_POST['field_4'] . "; changed to " . $_POST['field_4']; ). After all this, my php code does not operate correctly. What I am trying to do is as follows (in laymans terms): If Validation = true and check box == "Yes" (or checked) mail("wxy@gmail.com,xyz@gmail.com",$Subject,$message,"From: $email") Elseif Validation = true and check box != "Yes" (not checked) mail("wxy@gmail.com",$Subject,$message,"From: $email") Else ..... After fixing my mistake (described above) and tidying up, I tested the form and found: 1) It appears to work fine) 2) When the check box is checked, the mail is not sent to both emails as directed (only the first) 3) It appears to work fine when the check box is not checked (which is no supprise) Is there something wrong with my IF statement? Any help would be super!!! Graham Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted June 30, 2008 Share Posted June 30, 2008 put session_start(); right after the <?php for a start. reorder this: <?php if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) && ($_POST['field_5']=="Yes") ) and put the !empty $_POST before the session security code equals post security code Quote Link to comment Share on other sites More sharing options...
lhcpr Posted July 2, 2008 Author Share Posted July 2, 2008 Hello all, Issues related to a conditional mail function seem to be decreasing; which is great news! Most of the issues were borne from poor code formatting. While the code works, are there any other suggestions or issues that I need to be aware of? Code as follows: <?php session_start(); // Setup code $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/")); // Checkbox handling $field_5_opts = $_POST['field_5'][0]; // Sender email is predefined as a hidden html field $email = $_POST['field_6']; // Message body $message = $_POST['field_1'] . " " . $_POST['field_2'] . " says no to development at Turrella Reserve" . " Email address: " . $_POST['field_3'] . " This is what " . $_POST['field_1'] . " has to say: " . $_POST['field_4']; //Validation and handling if check box is equal to Yes if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) && ($field_5_opts=="Yes") ) { //Mail function if check box is equal to Yes mail("wxy@gmail.com,xyz@gmail.com","No development at Turrella Reserve",$message,"From: $email"); include("confirm.html"); } //Validation and handling if check box is not equal to Yes elseif( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) && ($field_5_opts!="Yes") ) { // Mail function mail("wxy@gmail.com","No development at Turrella Reserve",$message,"From: $email"); include("confirm.html"); } else { echo "Invalid Captcha String."; } ?> Thanks, Graham Quote Link to comment Share on other sites More sharing options...
br0ken Posted July 2, 2008 Share Posted July 2, 2008 Your code is still formatted quite poorly and you use no validation on your user input. <?php // Validate Integer $id = (int)$_GET['id']; // Validate String $var = strip_tags($_GET['var']); // Validate String For Database $var = mysql_real_escape_string($_GET['var']); // Return mail() result and check $res = mail($to, $subject, $message, $headers); if ($res){ echo "Message Sent OK"; } else { echo "The message could not be sent"; } // Load up message string $msg = "Email Address: $email\r\n"; $msg .= "Name: $name\r\n"; // Use this format rather than one long concatenated string ?> This is my way of doing things, I could be wrong in which case I'd be happy for someone to point it out for me Quote Link to comment 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.