PHP email - I can't interact with the server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skytreader
Forum Newbie
Posts: 7
Joined: Wed Sep 09, 2009 12:31 pm

PHP email - I can't interact with the server

Post 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! (--,)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Do you get the "Your comment has been sent" message?
skytreader
Forum Newbie
Posts: 7
Joined: Wed Sep 09, 2009 12:31 pm

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

Post by skytreader »

No. Not at all. :C
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Code: Select all

if(isset($_REQUEST['email']) && isset($_REQUEST['name']) && isset($_REQUEST['comment'])){
Compare that with what you have in your form.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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']))
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
skytreader
Forum Newbie
Posts: 7
Joined: Wed Sep 09, 2009 12:31 pm

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

Post 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. (--,)
Post Reply