1

I'm Trying to create a contact form for my site using PHPMailer but it not working. Please help me and tell me in which point i am doing wrong.

Error : I am not getting the Message submitted in user in contact form in my Mail Inbox.

HTML Code :

<div id="contactform">
    <form method="post" action="sendmail.php">
    <p><label for="name">Name:*</label> <input type="text" class="form-control" name="name" id="name" tabindex="1" /></p>
    <p><label for="email">Email:*</label> <input type="text" class="form-control" name="email" id="email" tabindex="2" /></p>
    <p><label for="comments">Message:*</label> <textarea  class="form-control" name="comments" id="comments" cols="12" rows="6" tabindex="3"></textarea></p>
    <p><input name="submit" type="submit" id="submit" class="submit" value="Send" tabindex="4" /></p>
    </form>
</div>

JavaScript :

<script>
    function clearForms() {
        var i;
        for (i = 0; (i < document.forms.length); i++) {
            document.forms[i].reset();
        }
        $('.alert.alert-success').fadeOut(200);
    }
</script>

<script>
    function clearForms() {
        var i;
        for (i = 0; (i < document.forms.length); i++) {
            document.forms[i].reset();
        }
        $('.alert.alert-success').fadeOut(200);
    }
</script>

PHP Code :

<?php
    $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    $comments = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
    $site_owners_email = 'lisamall3@protonmail.com';
    $site_owners_name = 'TEST';

    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name";
    }

    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address";
    }

    if (strlen($comments) < 3) {
        $error['comments'] = "Please leave a comment.";
    }

    if (!$error) {
        require_once ('phpMailer/class.phpmailer.php');

        $mail = new PHPMailer();
        $mail->From = $email;
        $mail->FromName = $name;
        $mail->Subject = "Contact Form";
        $mail->AddAddress($site_owners_email, $site_owners_name);
        $mail->IsHTML(true);
        $mail->Body = '<b>Name:</b> ' . $name . '<br/><b>E-mail:</b> ' . $email . '<br/><br/>' . $comments;
        $mail->Send();
        echo "<div class='alert alert-success'  role='alert'>Thanks " . $name . ". Your message has been sent.</div>";
    } // end if no error
    else {
        $response = (isset($error['name'])) ? "<div class='alert alert-danger'  role='alert'>" . $error['name'] . "</div> \n" : null;
        $response.= (isset($error['email'])) ? "<div class='alert alert-danger'  role='alert'>" . $error['email'] . "</div> \n" : null;
        $response.= (isset($error['comments'])) ? "<div class='alert alert-danger'  role='alert'>" . $error['comments'] . "</div>" : null;
        echo $response;
    } // end if there was an error sending
?>

I need 2 Functions :

  1. I get alert in my mail after user submit the form .
  2. User who submitted the file should get default thank you message in his mail.
PPL
  • 6,357
  • 1
  • 11
  • 30
Maurya Ashish
  • 39
  • 1
  • 1
  • 6
  • 2
    1: What is the error you receive? 2: for a thank you mail, start a second phpmailer instance with your standard message – Jorn Mar 21 '18 at 11:46
  • Im not receiving the message submitted by users – Maurya Ashish Mar 21 '18 at 11:48
  • http://www.kvcodes.com/2014/01/how-to-create-contact-form-with-phpmailer-to-send-mails/ check the link you will just neeed to add a alert and redirect menu – Rahul shukla Mar 21 '18 at 11:50
  • add this to your code, maybe the error message will help you: if(!$mail->send()) { echo 'Message was not sent.'; echo 'Mailer error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent.'; } – Jorn Mar 21 '18 at 11:51
  • No. Im not asking about print alert. Like user filled the form and typed questions and submitted the form. How i can check what user submitted?. I am not getting any mail in my inbox – Maurya Ashish Mar 21 '18 at 11:54

0 Answers0