Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question email form not sending - no errors

    PHP Code:
    <?php
    if(isset($_POST['query'])) {
         
        
    // EDIT THE 2 LINES BELOW AS REQUIRED
        
    $email_to "myemail@address.com";
        
    $email_subject "Query Form from me";
         
         
        function 
    died($error) {
            
    // your error code can go here
            
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo 
    "These errors appear below.<br /><br />";
            echo 
    $error."<br /><br />";
            echo 
    "Please go back and fix these errors.<br /><br />";
            die();
        }
         
        
    // validation expected data exists
        
    if(!isset($_POST['Link']) ||
            !isset(
    $_POST['FirstName']) ||
            !isset(
    $_POST['MyEmail']) ||
            !isset(
    $_POST['MyQuery'])) {
            
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
        }
         
        
    $Link $_POST['Link'];
        
    $FirstMame $_POST['FirstName']; // required
        
    $MyEmail $_POST['MyEmail']; // required
        
    $MyQuery $_POST['MyQuery']; // required
        
        
    echo $Link.'<br />';
            echo 
    $FirstMame.'<br />';
                echo 
    $MyEmail.'<br />';
                    echo 
    $MyQuery.'<br />';
        
         
        
    $error_message "";
        
    $email_exp '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!
    preg_match($email_exp,$email_from)) {
        
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
        
    $string_exp "/^[A-Za-z .'-]+$/";
      if(!
    preg_match($string_exp,$first_name)) {
        
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
      if(!
    preg_match($string_exp,$last_name)) {
        
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
      if(
    strlen($comments) < 2) {
        
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
      }
      if(
    strlen($error_message) > 0) {
        
    died($error_message);
      }
        
    $email_message "Form details below.\n\n";
         
        function 
    clean_string($string) {
          
    $bad = array("content-type","bcc:","to:","cc:","href");
          return 
    str_replace($bad,"",$string);
        }
        
        
    $email_message .= "Link: ".clean_string($Link)."\n"
        
    $email_message .= "First Name: ".clean_string($FirstName)."\n";
        
    $email_message .= "Email: ".clean_string($MyEmail)."\n";
        
    $email_message .= "Query: ".clean_string($MyQuery)."\n";
         
         
    // create email headers
    $headers 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' phpversion();
    @
    mail($email_to$email_subject$email_message$headers); 
    ?>
    <? 
    }?>
    my form submits to this page but it's blank?!

    please help

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That would be a good thing. This script generates output only on failure, so no output means either everything was successful, or you have a parse error.

    Make sure you check the results of a mail call. If it checks out successful, PHP did its job; the problem will now be the sendmail on the host machine. Make sure you take off the @ as well, or to a trace back of the error message from error_get_last().

  • #3
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    hmm, its not showing errors?!

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,338
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    mail() won't show any errors because you've supressed them using the @ in front.

    If your page is showing up as blank then it may well be a parser error - check your error logs.

    As usual jarv, my previous advice still stands. You need to seriously improve your debugging skills especially if you intend to take on paid projects.

    Your call to mail() will return a boolean result (thats true or false). You can easily get it by doing this:

    $Result = mail($email_to, $email_subject, $email_message, $headers);

    var_dump($Result);

    Thats called debugging jarv. You print things out to the screen and see what they contain. In this case, you're looking for the var_dump() to report a boolean with a true or 1 contained in it.

    Try that, if it gives you a false / 0 result then mail() is failing for some reason.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    yep, in which case you can use the error_get_last() function or the $php_errormsg to retrieve the last error triggered. If there is none, and the mail pulls up as successful, the issue is beyond PHP's level and you'll need to look into the sendmail logs.

  • Users who have thanked Fou-Lu for this post:

    tangoforce (10-18-2012)

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,338
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by Fou-Lu View Post
    you can use the error_get_last() function
    I've just learned something new today
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    ikr! I just learned the other day that SQL actually has a standard that says you're supposed to use ' and not " in queries O.o
    Never knew that before!

  • #8
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    still stuck on this, can't get it to display any errors?!

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by jarv View Post
    still stuck on this, can't get it to display any errors?!
    Which sounds to me that you have none.
    You can even put a simple echo 'done.'; at the bottom of the script. If that shows, and assuming you've followed the advice here by checking the results of the mail() call check out as successful, you're diving into the sendmail logs to find out why your OS didn't send the mail.

  • #10
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question

    my emailer still doesn't work?!

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set'display_errors','1');

    echo 
    'hello';
    if(isset(
    $_POST['query'])) {
         
        
    // EDIT THE 2 LINES BELOW AS REQUIRED
        
    $email_to 'me@gmail.com';
        
    $email_subject 'Query Form ';     
         
        function 
    died($error) {
            
    // your error code can go here
            
    echo 'We are very sorry, but there were error(s) found with the form you submitted.';
            echo 
    'These errors appear below.<br /><br />';
            echo 
    $error.'<br /><br />';
            echo 
    'Please go back and fix these errors.<br /><br />';
            die();
        }
         
        if(!isset(
    $_POST['Link']) ||
            !isset(
    $_POST['FirstName']) ||
            !isset(
    $_POST['MyEmail']) ||
            !isset(
    $_POST['MyQuery'])) {
            
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
        }
         
        
    $Link $_POST['Link'];
        
    $FirstName $_POST['FirstName']; // required
        
    $MyEmail $_POST['MyEmail']; // required
        
    $MyQuery $_POST['MyQuery']; // required
        
         
    echo $Link;
        
    $error_message "";
        
    $email_exp '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!
    preg_match($email_exp,$MyEmail)) {
        
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
        
    $string_exp "/^[A-Za-z .'-]+$/";
      if(!
    preg_match($string_exp,$FirstName)) {
        
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
      if(
    strlen($MyQuery) < 2) {
        
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
      }
      if(
    strlen($error_message) > 0) {
        
    died($error_message);
      }
        
    $email_message "Form details below.\n\n";
         
        function 
    clean_string($string) {
          
    $bad = array("content-type","bcc:","to:","cc:","href");
          return 
    str_replace($bad,"",$string);
        }
         
        
    $email_message .= 'Link: '.clean_string($Link).'\n'
        
    $email_message .= 'First Name: '.clean_string($FirstName).'\n';
        
    $email_message .= 'Email: '.clean_string($MyEmail).'\n';
        
    $email_message .= 'Query: '.clean_string($MyQuery).'\n';
        echo 
    $email_message;
        
    end
         
    // create email headers
    $headers 'From: '.$MyEmail.'\r\n'.
    'Reply-To: '.$MyEmail.'\r\n' .
    'X-Mailer: PHP/' phpversion();
    @
    mail($email_to$email_subject$email_message$headers); 
    echo 
    'done.';
    ?>

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,338
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    jarv did you read my previous advice?

    Quote Originally Posted by tangoforce View Post
    Your call to mail() will return a boolean result (thats true or false). You can easily get it by doing this:

    $Result = mail($email_to, $email_subject, $email_message, $headers);

    var_dump($Result);
    Well? Did you try it?

    Here is another way:
    PHP Code:
    if (mail($email_to$email_subject$email_message$headers))
       {
       echo 
    'done.'
       }
    else
       {
       echo 
    'error';
       } 
    If it now prints 'done' and you still do not get the email then it is an smtp / sendmail issue or a spam filter somewhere.

    Please jarv, read and try things that people say to you.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #12
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    Notice: Undefined variable: email_to in D:\aandheurope.com\wwwroot\send_form_email.php on line 556

    Notice: Undefined variable: email_subject in D:\aandheurope.com\wwwroot\send_form_email.php on line 556

    Notice: Undefined variable: email_message in D:\aandheurope.com\wwwroot\send_form_email.php on line 556

    Notice: Undefined variable: headers in D:\aandheurope.com\wwwroot\send_form_email.php on line 556

    Warning: mail() [function.mail.php]: SMTP server response: 503 5.5.2 Need Rcpt command. in D:\aandheurope.com\wwwroot\send_form_email.php on line 556
    error

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,338
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Jarv,

    Those messages say it all!

    You're using variables called:
    $email_to, $email_subject, $email_message and they're not containing any values. Your call to mail()then fails and has been outputting error messages that you were hiding using @ symbol. That is why you should not use @ - it hides problems!

    This line of code:
    PHP Code:
    if(isset($_POST['query'])) { 
    Does not contain your call to mail() yet this is where you are setting those variables - inside this block of code. Your call to mail() is outside it at the end so even if your variables are not set, you are still trying to send an email.

    Therefore, move your call to mail() inside the { braces } at the end for your if(isset($_POST['query'])) { line of code.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    jarv (11-06-2012)

  • #14
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    Thanks!!!! it works... next problem, just not getting the email?! oh well

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by jarv View Post
    Thanks!!!! it works... next problem, just not getting the email?! oh well
    Like I said, if the result of the mail() is successful, PHP has done its job. You'll be hitting the sendmail logs to find out if it sent them or not.
    If its not successful, then use the error_get_last() to determine if its a variable issue, or if it has any more information to share. If the variables all check out, then you need to verify sendmail is available, or that smtp is configured if its a windows machine.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •