如何使用phpmailer

时间:2018-03-21 11:42:36

标签: javascript php html email phpmailer

我尝试使用PHPMailer为我的网站创建联系表单,但它不起作用。请帮助我告诉我,我在哪个方面做错了。

错误:我没有收到用户以邮件收件箱中的联系方式提交的邮件。

HTML代码:

<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代码:

<?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
?>

我需要2个功能:

  1. 用户提交表单后,我的邮件会收到提醒。
  2. 提交文件的用户应在邮件中收到默认的感谢信息。

0 个答案:

没有答案