PHPMailer不会通过localhost

时间:2017-12-03 20:14:50

标签: php validation phpmailer

当我点击我的提交按钮以提交联系表格时,它不会做任何事情,表格保持不变,当然我没有收到电子邮件。 我在网络XHR的控制台中收到的错误是:

  

警告的:   要求(BootstrapLandinPage / LIB / PHPMailer的主/ SRC / Exception.php):   无法打开流:没有这样的文件或目录    C:\ xampp \ htdocs \ BootstrapLandinPage \ send.php 在线    3

致命错误:require():打开失败   必需'BootstrapLandinPage / lib / PHPMailer-master / src / Exception.php'   (include_path ='C:\ xampp \ php \ PEAR')in    C:\ xampp \ htdocs \ BootstrapLandinPage \ send.php 在线    3

我的send.php:

<?php

require 'BootstrapLandinPage/lib/PHPMailer-master/src/Exception.php';

require 'BootstrapLandinPage/lib/PHPMailer-master/src/PHPMailer.php';

require 'BootstrapLandinPage/lib/PHPMailer-master/src/SMTP.php';


$error_msg = array();
$success_msg = array();
$data = '';

// prevent warnings or errors from displaying, else you won't get proper json result
ini_set('display_errors',0);

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (empty($_POST["name"])) {
        $error_msg['name'] = "Name is required";
    } else {
        $name = test_input($_POST["name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Zא-ת ]*$/",$name)) {
            $error_msg['name'] = "Only letters and white space allowed";
        }
    }


    if (empty($_POST["email"])) {
        $error_msg['email'] = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error_msg['email'] = "Invalid email format";
        }
    }

    if (empty($_POST["phone"])) {
        $error_msg['phone'] = "Phone is required";
    } else {
        $phone = test_input($_POST["phone"]);
        // check if e-mail address is well-formed
        if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
            $error_msg['phone'] = "Invalid phone number";
        }
    }
    if (empty($_POST["subject"])) {
        $error_msg['subject'] = "Subject is required";
    }
    if (empty($_POST["message"])) {
        $error_msg['message'] = "Message is required";
    }

    if (empty($_POST["subject"])) {
        $subject = "";
    } else {
        $subject = test_input($_POST["subject"]);
    }

    if (empty($_POST["message"])) {
        $message = "";
    } else {
        $message = test_input($_POST["message"]);
    }
if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    }
    if (empty($error_msg)){ 
        $message_body = '';
        unset($_POST['submit']);
        foreach ($_POST as $key => $value){
            $message_body .=  "$key: $value\n";
        }

        $to = 'ilonaXXX@gmail.com';// some  email I use
        $subjectm = 'Contact Form Submit';
          $mail = new PHPMailer(true);
        try {
            //Server settings
            $mail->SMTPDebug = 2;
            $mail->isSMTP();
            $mail->Host = 'smtp.gmail.com';
            $mail->SMTPAuth = true;
            $mail->Username = 'XXXX@gmail.com'; // my gmail
            $mail->Password = '*****';// my gmail password
            $mail->SMTPSecure = 'ssl';
            $mail->Port = 465;//  my xampp port is 80 , which I tried to put here ,but it wont work anyway

            //Recipients
            $mail->setFrom('xxxxx@gmail.com', 'Ilona Sem');
            $mail->addAddress($email, $name);

            //Content
            $mail->isHTML(true);
            $mail->Subject = 'Contact Form Submit';
            $mail->Body    = $message;
            $mail->AltBody = preg_replace( "/\n\s+/", "\n", rtrim(html_entity_decode(strip_tags($message))) ); // For clients that do not support html / "spam control"

            $mail->send();

            $data = array('success'=>'Message sent, thank you for contacting us!');
            outputJson($data);

        } catch (Exception $e) {
            $data = array('mail_error'=>'Could not send email');
            outputJson($data);
            //echo 'Mailer Error: ' . $mail->ErrorInfo; // Debug message that you can see in network tab in web developer browser extension
        }

        $name = $email = $phone = $message = $subject = '';

    }
}

    // output json that you can parse with jquery
 function outputJson($data) {
    // output json that you can parse with jquery
    header('Content-Type: application/json');
    echo json_encode($data);
    exit;
}
function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

0 个答案:

没有答案