Thread: Contact Form

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Contact Form


    Hi
    I followed a tutorial on YouTube and created a php contact form.
    With the form fully filled in it will redirect to a thank you message but i don't receive the email and i get this message



    "Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\TokyoCorner\HTML\Contact_Us.p h p on line 51"


    A solution or any ideas would be most helpful

    Thank you


    This is my code

    PHP Code:
    <?php

    // Set email variables
    $email_to 'kieran.smith1990@yahoo.co.uk';
    $email_subject 'Form submission';

    // Set required fields
    $required_fields = array('fullname','email','comment');

    // set error messages
    $error_messages = array(
        
    'fullname' => 'Please enter a Name to proceed.',
        
    'email' => 'Please enter a valid Email Address to continue.',
        
    'comment' => 'Please enter your Message to continue.'
    );

    // Set form status
    $form_complete FALSE;

    // configure validation array
    $validation = array();

    // check form submittal
    if(!empty($_POST)) {
        
    // Sanitise POST array
        
    foreach($_POST as $key => $value$_POST[$key] = remove_email_injection(trim($value));
        
        
    // Loop into required fields and make sure they match our needs
        
    foreach($required_fields as $field) {        
            
    // the field has been submitted?
            
    if(!array_key_exists($field$_POST)) array_push($validation$field);
            
            
    // check there is information in the field?
            
    if($_POST[$field] == ''array_push($validation$field);
            
            
    // validate the email address supplied
            
    if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation$field);
        }
        
        
    // basic validation result
        
    if(count($validation) == 0) {
            
    // Prepare our content string
            
    $email_content 'New Website Comment: ' "\n\n";
            
            
    // simple email content
            
    foreach($_POST as $key => $value) {
                if(
    $key != 'submit'$email_content .= $key ': ' $value "\n";
            }
            
            
    // if validation passed ok then send the email
            
    mail($email_to$email_subject$email_content);
            
            
    // Update form switch
            
    $form_complete TRUE;
        }
    }

    function 
    validate_email_address($email FALSE) {
        return (
    preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i'$email))? TRUE FALSE;
    }

    function 
    remove_email_injection($field FALSE) {
       return (
    str_ireplace(array("\r""\n""%0a""%0d""Content-Type:""bcc:","to:","cc:"), ''$field));
    }

    ?>









        <script type="text/javascript">
    var nameError = '<?php echo $error_messages['fullname']; ?>';
            var emailError = '<?php echo $error_messages['email']; ?>';
            var commentError = '<?php echo $error_messages['comment']; ?>';
    function MM_preloadImages() { //v3.0
      var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
        if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
    }
    </script>







    <?php if($form_complete === FALSE): ?>
    <form action="Contact_Us.php" method="post" id="comment_form">
        <div class="Row">
        <div class="Lable">Your Name</div> <!--End of Lable-->
        <div class="input">
        <input type="text" id="fullname" class="detail" name="fullname" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''?>" /><?php if(in_array('fullname'$validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?>

        </div> <!--End input-->
        <div class="Context">e.g John Smith</div><!--End Context-->
        </div> <!--End row-->
        
        <div class="Row">
        <div class="Lable">Your E-mail</div> <!--End of Lable-->
        <div class="input">
        <input type="text" id="email" class="detail" name="email" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''?>" /><?php if(in_array('email'$validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?>
        </div> <!--End input-->
        <div class="Context">We sill not spam you!</div><!--End Context-->
        </div> <!--End row-->

        <div class="Row">
          <div class="Lable">Your Message</div> <!--End of Lable-->
        <div class="input2">
        <textarea id="comment" name="comment" class="mess"><?php echo isset($_POST['comment'])? $_POST['comment'] : ''?></textarea><?php if(in_array('comment'$validation)): ?><span class="error"><?php echo $error_messages['comment']; ?></span><?php endif; ?>
        </div> <!--End input-->
        </div> <!--End row-->
        
        <div class="submit">
        <input type="submit" id="submit" Name="submit" value="send message" />
        </div><!--End of submit-->
        </form>
        <?php else: ?>
    <p style="font-style:35px; font-family:Arial, Helvetica, sans-serif; color:#255E67; margin-left:25px;">Thank you for your Message!</p>
    <script type="text/javascript">
    setTimeout('ourRedirect()', 5000)
    function ourRedirect(){
        location.href='index.php'
    }
    </script>
    <?php endif; ?>
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,136
    Rep Power
    1990
    Originally Posted by Kie21
    A solution or any ideas would be most helpful
    Your code is not the problem. You need to either install a mail server in the system that you're running the script on, or point the script to an external SMTP server.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by Catacaustic
    Your code is not the problem. You need to either install a mail server in the system that you're running the script on, or point the script to an external SMTP server.
    Hi thanks for the reply.
    Now i know what the problem is ill look further into it.
    The website is for a friend's band, its not going to be a huge site so which would you suggest i did? install the mail server or point to external?

    Thanks
  6. #4
  7. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,136
    Rep Power
    1990
    If it's only a staging site then I wouldn't install anything extra that you don't have already - unless you're going to be using it a lot more in the future. For testing just point it to an external SMTP server. You can always change that back when the site goes live on a "real" hosting server.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by Catacaustic
    If it's only a staging site then I wouldn't install anything extra that you don't have already - unless you're going to be using it a lot more in the future. For testing just point it to an external SMTP server. You can always change that back when the site goes live on a "real" hosting server.
    Brilliant! Thanks for your help buddy!
  10. #6
  11. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0
    Check your internet connection first and try again.And make sure your not working on localhost.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,031
    Rep Power
    377
    i think you specify the mail settings in your php.ini file? so i would try there. You can use google (or hotmail) SMTP settings? From experience though, I wasnt able to set them up when i tried couple of years ago

IMN logo majestic logo threadwatch logo seochat tools logo