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 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Email Sending Script.

    Hi Guys,

    I have written (or tried to!) a simple email sending form for a site i am finishing.

    My HTML code for the form is:

    Code:
    	<form action="email.php" method="post" enctype="text/plain">
    
    	<div align="left" style="padding-left: 25px;"><b>Name:&nbsp;&nbsp;</b><input type="text" name="name" size="40" /></div>
    	<br />
    	<div align="left" style="padding-left: 25px;"><b>Email:&nbsp;&nbsp;</b><input type="text" name="email" size="40" /></div>
    	<br />
    	<div align="left" style="padding-left: 25px;"><b>Comment:</b><textarea name="comment" cols="40" wrap="virtual" rows="10"></textarea>
    		</div>
    <br /><br />
    
    	<div alignt="left" style="padding-left: 20px;">
    	<input type="submit" name="Submit" value="Send">
    	<input type="reset" name="reset" value="Reset">
    	</div>
    	
    
    </form>
    And the email.php script is:
    Code:
    <html>
    <head><title>Sending Email...</title></head>
    <body>
    <?php
    
    $to = "someone@something.com";
    $email = $HTTP_POST_VARS['email'];
    $name = $HTTP_POST_VARS['name'];
    $message = $HTTP_POST_VARS['message'];
    
    
    if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
      echo "<h4>Invalid email address</h4>";
      echo "<a href='javascript:history.back(1);'>Back</a>";
    } elseif ($subject == "") {
      echo "<h4>No subject</h4>";
      echo "<a href='javascript:history.back(1);'>Back</a>";
    }
    
    /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
    elseif (mail($to,$email,$name,$message)) {
      echo "<h4>Thank you for sending email</h4>";
    } else {
      echo "<h4>Can't send email to $email</h4>";
    }
    ?>
    </body>
    </html>
    The problem is, it doesent seem to work. It just comes back and says invalid email everytime i try to send one.

    Does anyone have any idea?

    Many Thanks.

    NB: someone@something.com, i put this in to replace the other persons email address.

    Jon
    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    I would seriously consider looking to PEAR for email validation, actually any validation. Although getting it installed can be a nightmare most of them work as separate classes in the document ROOT.
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Now that was confusing!! hehe

    Ive used this PHP method before and it seemed to work okay, this is not going to be a website where millions of people will me mailing, its not going to be used that often...

    To keep things simple, for now, I woul probably prefer to get the PHP version working.

    Thanks for you advice though

    Jon
    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #4
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Hehe, sorry let me explain! PEAR is a PHP extension its all in PHP
    download this. extract it and Insert it in your document root as a folder.

    PHP Code:
    <PHP
    include($_SERVER['DOCUMENT_ROOT'].'/Validate-0.7.0/validate.php');
    $valid=__emailRFC822($email,$options);
    ?> 
    best thing about PEAR is it is securer for validation
    You can not say you know how to do something, until you can teach it to someone else.

  • #5
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Im not sure i understand what you mean

    If i add that to my current script, surely it still wont work because it doesent work now. If all PEAR does is validation will that fix the problem of it not working at the moment?

    Thanks again.

    Jon
    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Your script is failing because something is wrong with the regular expression used to check the e-mail address. tim's suggestion was to use a PEAR module to check the e-mail instead. However, you don't seem to know enough about PHP/PEAR to handle this, so I would suggest you do a search of this forum, and the internet for an alternative e-mail validation regex.

  • #7
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    OK sorry try this PS loving your quotes
    Code:
    <html>
    <head><title>Sending Email...</title></head>
    <body>
    <?php
    
    $to = "someone@something.com";
    $email = $HTTP_POST_VARS['email'];
    $name = $HTTP_POST_VARS['name'];
    $message = $HTTP_POST_VARS['message'];
    
    
    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*
    (\.(com|net|org|edu|biz|gov|info|aero|cat|coop|int|jobs|mil|mobi|museum|name|pro|tel|travel|[a-z]{2})$", $email)) {
      echo "<h4>Invalid email address</h4>";
      echo "<a href='javascript:history.back(1);'>Back</a>";
    } elseif ($subject == "") {
      echo "<h4>No subject</h4>";
      echo "<a href='javascript:history.back(1);'>Back</a>";
    }
    
    /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
    elseif (mail($to,$email,$name,$message)) {
      echo "<h4>Thank you for sending email</h4>";
    } else {
      echo "<h4>Can't send email to $email</h4>";
    }
    ?>
    </body>
    </html>
    You can not say you know how to do something, until you can teach it to someone else.

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    $HTTP_POST_VARS are depreciated and are turned off by default in all current versions of PHP5.

    Check your web server log for errors and/or turn on full php error reporting to get php to help you.

    If you are getting any notice/warning/error messages, find and fix them.

    Start by replacing any $HTTP_POST_VARS with $_POST
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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