php email error

Discussion in 'PHP' started by webber09, Feb 3, 2011.

  1. #1
    hi all, I have been given this script however its not working!! Can anyone see whats going on?

    <?
    	  if (isset($_POST['submitted'])) {
    		  if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments'])) { 
    		  $body = "Name:
    		  {$_POST['name']}\n\nComments:
    		  {$_POST['comments']}";
    		  mail('myemail@myemail.co.uk',
    			   'Contact Form Submission', $body,
    			   "From: {$_POST['email']}");
    			   echo '<p><em>Thank you for contacting us.</em></p>';
    			   $_POST =array();
    		  } else {
    			  echo '<p style="font-weight:
    			  bold; color: #C00">Please
    			  fill out the form
    			  compleatly.</p>';
    		  }
    	  }
    	  ?>
          <p>Please fill out this form to contact us.</p>
          <form action="email.php" method="post">
          <p>Name: <input type="text" name="name" size="30" maxlength="60" value="<? if (isset($_POST['name'])) echo $_POST['name']; ?>" /></p>
          <p>Email Address: <input type="text" name="email" size="30" maxlength="80" value="<? if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
          <p>Comments: 
            <textarea name="comments" rows="5" cols="30"><? if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></p>
          <p><input type="submit" name="submit" value="Send!" /></p>
         <input type="hidden" name="submitted" value="TRUE" />
         </form>
    PHP:

     
    webber09, Feb 3, 2011 IP
  2. TheWiseGuy

    TheWiseGuy Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #2
    Could you post what error you're getting?
    It would save 90% of the troubleshooting time
    If no error is displayed on your browser please check your error log
     
    TheWiseGuy, Feb 3, 2011 IP
  3. moads

    moads Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Check to see if your server allows for PHP mail. I had this problem once and it drove me crazy trying to figure it out.
     
    moads, Feb 3, 2011 IP
  4. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #4
    there is no error being shown, should the script work as it is ?
     
    webber09, Feb 3, 2011 IP
  5. moads

    moads Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #5
    Yes, the script seems like it's fine. You should output everything to account for all the variables. If that doesn't work it's your mail server.
     
    moads, Feb 3, 2011 IP
  6. TheWiseGuy

    TheWiseGuy Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #6
    troubleshooting email is kind of hard, the problem could as well be on the recipient side.
    try adding something like this to understand if the email is being sent:
    
    $mail_sent = @mail('myemail@myemail.co.uk',
                   'Contact Form Submission', $body,
                   "From: {$_POST['email']}");
    if($mail_sent)
    {
    header('Location:contactus.php?msg=yes');
    }
    else
    {
    header('Location:contactus.php?msg=no');
    }
    
    PHP:
    adapted from http://php.net/manual/en/function.mail.php
     
    TheWiseGuy, Feb 3, 2011 IP
  7. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #7
    That won't work. This checks if php calls that function, it can't check if server sent that mail ;)
    try to add

    error_reporting(E_ALL);

    just after <?php and try to reply here with errors
     
    G3n3s!s, Feb 3, 2011 IP
  8. TheWiseGuy

    TheWiseGuy Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #8
    php mail() returns trueif the mail was successfully accepted for delivery, false otherwise, hence it actually checks if the email was sent from php, then if there's no mail server that can deliver it it's another problem.

    If it this tells you the email was sent you can stop searching for the problem in your PHP code and configuration.
     
    TheWiseGuy, Feb 3, 2011 IP
  9. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #9
    right guys all i can do is apologize, i have been a complete idiot and to be honest i can believe i have done it. In the action of the form it was trying to go to email.php, THIS DOES NOT EXIST! I am doing it all on new_index.php. i have now change this action to new_index.php and it all works :D so sorry to waste your time everyone :eek:
     
    webber09, Feb 4, 2011 IP
  10. TheWiseGuy

    TheWiseGuy Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #10
    good to hear you found the problem and, at least, you came back and explained where the it was
     
    TheWiseGuy, Feb 4, 2011 IP
  11. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #11
    i hang my head in shame, such a stupid mistake. sorry to waste your time all
     
    webber09, Feb 4, 2011 IP
  12. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #12
    that's not true. Try to run localhost and try to mail it with that metod you psoted. It returns true in most cases, but I didn't received mail (never)
     
    G3n3s!s, Feb 4, 2011 IP