Page 1 of 1

PHP email - I can't interact with the server

Posted: Sun Jan 24, 2010 9:46 am
by skytreader
Hi all. I'm trying out the code given at http://www.w3schools.com/php/php_secure_mail.asp . However, I can't seem to get it to work. My page calls on itself repeatedly, without ever sending the email :C.

Code: Select all

 
<?php
    
       function checkUserEmail($email){
                   $email = filter_var($email, FILTER_SANITIZE_EMAIL);
                    
                   if(filter_var($email, FILTER_VALIDATE_EMAIL)){
                         return TRUE;
                   }
                   else{
                         return FALSE;
                    }
       }
                    
       if(isset($_REQUEST['email']) && isset($_REQUEST['name']) && isset($_REQUEST['comment'])){
                
                    if(checkUserEmail($_REQUEST['email'])){
                            echo "<p>An error occurred while processing your comment!</p>";
                            echo "<p>Are you sure than you entered everything correctly? Or that you are not making any unnecessary inputs?</p>";                       
                     }
                     else{
                            $headers = "From: " . $_REQUEST['uname'] . "\r\n" . "Email: " . $_REQUEST['email'] . "\r\n" . "Website: " . $_REQUEST['url'];
                            
                            mail("myemailadd@mywebsite.com", "TEST Website Comment", wordwrap($_REQUEST['comment'], 70), $headers);
                            
                            echo "<p>Your comment has been sent.</p>";
                     }
              }
       else{
               //CHECK OUT THIS ECHO AND action ATTRIBUTE
               echo "<form method='post' action='email.php' class='pMirror'>
               <p>Fields marked with an asterisk (*) are required.</p>
               Name*: <input type='text' name='uname' id='uname' /><br />
               Email*: <input type='text' name='email' id='email' /><br />
               Website: <input type='test' name='url' id='url' /><br />
               Comment*:<br />
               <textarea name='comment' id='comment' rows='10' cols='56'></textarea><br />
               <input type='submit'>
               </form>";
        }
                
?>
I guess I'm having problems with the action attribute of my form. I tried the following combinations:
  • First I tried to create a separate php file for the code above (named, say, foo.php). In my email page (say, email.php) I embedded foo.php using a <?php include("foo.php"); ?> statement. It did not work as expected. All that happened is that I got to a "bare bones" page of the form (i.e. no formatting, etc. Well, I know it is because the browser loaded foo.php)
  • Then I tried to change the action attribute at foo.php so that is says action='email.php' . Still doesn't work but I got redirected to to email.php . (As users will be entering info from email.php, the unaquainted will get the feeling that the page got "refreshed"). Again, for reasons obvious to me.
  • Finally, I tried to embed the code itself (i.e., I copy-pasted) of foo.php to email.php, the way W3schools did it. Still to no avail.
Am I missing something here? Any clues on why it isn't sending? Thanks much in advance! (--,)

Re: PHP email - I can't interact with the server

Posted: Sun Jan 24, 2010 9:51 am
by requinix
Do you get the "Your comment has been sent" message?

Re: PHP email - I can't interact with the server

Posted: Mon Jan 25, 2010 8:28 am
by skytreader
No. Not at all. :C

Re: PHP email - I can't interact with the server

Posted: Mon Jan 25, 2010 8:55 am
by requinix

Code: Select all

if(isset($_REQUEST['email']) && isset($_REQUEST['name']) && isset($_REQUEST['comment'])){
Compare that with what you have in your form.

Re: PHP email - I can't interact with the server

Posted: Mon Jan 25, 2010 10:12 am
by AbraCadaver
Also, next problem, you are displaying an error if the email DOES validate. Change:

Code: Select all

if(checkUserEmail($_REQUEST['email']))
To:

Code: Select all

if(checkUserEmail($_REQUEST['email']) === false)
Or:

Code: Select all

if(!checkUserEmail($_REQUEST['email']))

Re: PHP email - I can't interact with the server

Posted: Sat Jan 30, 2010 11:15 am
by skytreader
Haven't monitored the replies for quite sometime as I've been away from the net.

Anyway, thanks everyone for pointing out my glaring errors (especially AbraCadaver). I've figured out some workaraounds by myself and it's working fine now. Again, much thanks. (--,)