douda5706 2019-02-15 07:35
浏览 170

PHPMailer不起作用但没有错误,smtpdebug设置为2

I have gone through multiple questions like this and nothing seems to fix my problem. I have set the SMTP debug option to 2, checked my credentials (changed to example ones for posting obviously) also checked the smtp server and port, using gmail and tls with port 465. I'm using ajax to submit form data on the click on submit button.

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

/* Exception class. */
require '/home/saveyamo/public_html/PHPMailer/src/Exception.php';

/* The main PHPMailer class. */
require '/home/saveyamo/public_html/PHPMailer/src/PHPMailer.php';

/* SMTP class, needed if you want to use SMTP. */
require '/home/saveyamo/public_html/PHPMailer/src/SMTP.php';


$errorMSG = "";


/* NAME */

if (empty($_POST["firstName"])) {
    $errorMSG = "<li class='danger'>Your first name is required</<li>";
} elseif (!filter_var($_POST["firstName"], FILTER_VALIDATE_INT) === false) {
    $errorMSG = "<li class='danger'>A number is not a valid name</<li>";
} else {
    $firstname = filter_var($_POST["firstName"], FILTER_SANITIZE_STRING);
}

if (empty($_POST["lastName"])) {
    $errorMSG = "<li class='danger'>Your last name is required</<li>";
} elseif (!filter_var($_POST["lastName"], FILTER_VALIDATE_INT) === false) {
    $errorMSG = "<li class='danger'>A number is not a valid name</<li>";
} else {
    $lastname = filter_var($_POST["lastName"], FILTER_SANITIZE_STRING);
}


/* EMAIL */

if (empty($_POST["email"])) {
    $errorMSG .= "<li class='danger'>Email is required</li>";
} elseif (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
    $errorMSG .= "<li class='danger'>Invalid email format</li>";
} else {
    $email = $_POST["email"];
}

/*phone*/
/*
if (empty($_POST["phone"])) {

    $errorMSG = "<li>Your phone number is required</<li>";

} else if (is_string($_POST["phone"])) {

  $errorMSG = "<li>That is not a valid phone number</<li>";

} else if (is_int($_POST["phone"])) {

    $phone = $_POST["phone"];

}
*/

function validate_phone_number($phones)
{
    // Allow +, - and . in phone number
    $filtered_phone_number = filter_var($phones, FILTER_SANITIZE_NUMBER_INT);
    // Remove "-" from number
    $phone_to_check = str_replace("-", "", $filtered_phone_number);
    // Check the lenght of number
    // This can be customized if you want phone number from a specific country
    if (strlen($phone_to_check) < 10 || strlen($phone_to_check) > 14) {
        return false;
    } else {
        return true;
    }
}

if (validate_phone_number($_POST["phone"]) == true) {
    $phone = $_POST["phone"];
} elseif (validate_phone_number($_POST["phone"]) == false) {
    $errorMSG = "<li class='danger'>That is not a valid phone number, try again. Be sure to include your area code.</li>";
} elseif (empty($_POST["phone"])) {
    $errorMSG = "<li class='danger'>Your phone number is required</<li>";
}



/* marina */

if (empty($_POST["marina"])) {
    $errorMSG = "<li class='danger'>Your marina is required</<li>";
} else {
    $marina = filter_var($_POST["marina"], FILTER_SANITIZE_STRING);
}

/* checkboxes */

$boxes = $_POST["boxes"];


/* MESSAGE */

if (empty($_POST["message"])) {
    $errorMSG .= "<li class='danger'>Message is required</li>";
} else {
    $message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
}


if (empty($errorMSG)) {
    $message = "Name: ".$firstname." ".$lastname."<br />".", Email: ".$email."<br />".", Phone: ".$phone."<br />".", Marina: ".$marina."<br />".", Interests: ".$boxes."<br />".", Message:".$message;
    $ok = "<li class='ok'>Awesome! Your message was sent, we will be in touch shortly.</li>";
    //echo json_encode(['code'=>200, 'msg'=>$message]);
    echo json_encode(['code'=>200, 'msg'=>$ok]);

    $to = "john.e.puaoi@gmail.com";
    $subject = "Contact";

    //mail($to, $subject, $message);
    //phpmailer start

    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
        //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'example@example.com';                 // SMTP username
    $mail->Password = 'password';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
        $mail->setFrom('web@example.com', 'Contact Form');
        $mail->addAddress('example@gmail.com');               // Name is optional
        $mail->addReplyTo('$email', '$firstname');

        //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Contact Form Submission';
        $mail->Body    = '$message';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();
    } catch (Exception $e) {
        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
    }

    //phpmailer end
    exit;
}


echo json_encode(['code'=>404, 'msg'=>$errorMSG]);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
    • ¥100 连续两帧图像高速减法
    • ¥15 组策略中的计算机配置策略无法下发
    • ¥15 如何绘制动力学系统的相图
    • ¥15 对接wps接口实现获取元数据
    • ¥20 给自己本科IT专业毕业的妹m找个实习工作
    • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
    • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)