我已经经历了多个这样的问题,但似乎没有什么可以解决我的问题。我已将SMTP调试选项设置为2,检查了我的凭据(更改为用于发布的示例),还检查了smtp服务器和端口,并使用gmail和tls以及端口465。我使用ajax来单击提交表单数据在提交按钮上。
<?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]);