【发布时间】:2020-08-04 21:18:02
【问题描述】:
我想通过 fetch API 向 index.php 发送一个按钮点击表单,并在其中验证表单。如果表单中没有错误,我想用 PHPMailer 向客户端发送一封电子邮件。由于某种原因,客户端没有收到电子邮件。 我已经搜索了几个小时的答案,但我无法解决问题。
这里是javascript代码:
const form = document.querySelector("#myForm");
form.addEventListener("submit", (e) => {
e.preventDefault();
const formData = new FormData(form);
fetch("index.php", {
method: 'post',
body: formData
}).then((resp) => resp.json())
.then(function (text) {
console.log(text); //Do something with the response, which is an array
if(text !== undefined && text.length > 0) { //The array isn't empty
//Show errors
const formdiverror = document.querySelector(".col-100-form-error");
const colform = document.querySelector(".col-100-form"); //showing error
colform.style.display = "block";
formdiverror.innerHTML = "";
text.forEach(t => formdiverror.innerHTML += t + "</br>");
} else {
//array is empty, no errors
const colform = document.querySelector(".col-100-form");
if(colform !== null || colform !== undefined) colform.style.display = "none";
alert("You succesfully bought the product!");
//window.location.replace("index.html"); //if there was no error redirect to index.html
}
});
})
php:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'Exception.php';
require 'PHPMailer.php';
require 'SMTP.php';
require 'PHPMailerAutoload.php';
require "class.phpmailer.php";
require "class.smtp.php";
$errors = [];
if(isset($_POST["name"])) {
/*I took out the validation to simplify the code*/
if (!empty($errors)) {
echo json_encode($errors);
} else {
echo json_encode([]);
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "mymail@gmail.com"; //paste one generated by Mailtrap
$mail->Password = 'mypasswd';
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Port = 587;
$mail->addAddress("customer@gmail.com", 'First Last');
$mail->addReplyTo('mymail@gmail.com', 'First Last');
$mail->setFrom('mymail@gmail.com', 'John Doe');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->isHTML(true);
$mailContent = "<h1>Send HTML Email using SMTP in PHP</h1>
<p>This is a test email I’m sending using SMTP mail server with PHPMailer.</p>";
$mail->Body = $mailContent;
$mail->send();
//If I try this way it gives an errror: Unexpected token in JSON at position
/* if($mail->send()){
echo ('Message has been sent');
}else{
echo ('Message could not be sent.');
echo ('Mailer Error: ' . $mail->ErrorInfo);
}*/
}
}
?>
我没有收到任何错误,所以我不知道问题出在哪里。 我已禁用双向验证,并允许访问不太安全的应用程序。
【问题讨论】:
-
尝试添加 $mail->SMTPDebug = 2;在您的 PHP 文件中并检查日志。它将对这个问题给出一个公平的想法。
-
为什么是这条线?
echo json_encode([]);?这肯定会解释错误If I try this way it gives an errror: Unexpected token in JSON at position... -
@ProfessorAbronsius 我使用 echo json_encode([]);这样我就知道在客户端我在表单验证中没有任何错误。
-
如果您在发送标头之前输出内容,您通常会遇到错误,并且由于 Fetch 函数需要 json,因此来自 PHP 的响应应该是 json 而不是像上面那样的纯文本.. 否则更改
(resp) => resp.json()对于(resp) => resp.text() -
@ProfessorAbronsius 如果我使用 resp.json(),那么每次在 php 中都需要使用 echo json_encode() 吗?我将数组发送到客户端,其中包含验证的错误(字符串)。如果我使用 resp.text() 和 console.log()-it,它只会打印“array”。
标签: javascript php fetch phpmailer