사용자가 내 애플리케이션에서 자신의 계정 비밀번호를 재설정하기 위해 이메일을 입력하는 ionic에서 비밀번호 재설정 페이지를 만들려고합니다. 문제는 이메일이 자신의 이메일로 전송되었는지 여부를 사용자에게 알려주는 PHP에서 응답을 보내려는 것입니다. 일어나는 일은 이메일이 전송되었지만 응답 오류가 표시된다는 것입니다.
내 typescript 코드는 다음과 같습니다.
async sendEmail() {
var regEmail = /^([a-zA-Z0-9\.-_]+)@([a-zA-Z0-9-]+).([a-z]{2,20})$/;
if (!regEmail.test(this.resetPasswordForm.value.email)) {
this.showToast("Invalid email");
return;
}
const loading = await this.load.create({
message: 'Please wait...'
});
loading.present();
this.auth.sendEmailVerification(this.resetPasswordForm.value).subscribe((data) => {
console.log(data);
if (data == "sent") {
this.showAlert("Great!", "We have sent you an email, check your inbox.");
this.resetPasswordForm.reset();
}
else if (data == "notfound") {
this.showAlert("Error!", "This email was not found.");
} else if (data == "error") {
this.showAlert("Oupss!", "An error occured and email could not be sent.");
}
}, (err) => {
console.log(err);
this.showAlert("Error!", err.message);
});
loading.dismiss();
}
PHP 코드 :
<?php
session_start();
require("headers.php");
require("connection.php");
$userData=file_get_contents("php://input");
$request=json_decode($userData);
$to=$request->email;
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
require 'phpmailer/src/Exception.php';
$con=connect();
$checkUser="SELECT count(*) from users where email='$to'";
$res=mysqli_query($con,$checkUser);
$row=mysqli_fetch_array($res);
if($row[0]==1){
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
$randomNumber=mt_rand(100000,999999);
$_SESSION["random"]=$randomNumber;
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('[email protected]','[email protected]');
$mail->addAddress($to); // Add a recipient
print json_encode("sent");
// Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "Password reset email for account: ".$to;
$mail->Body = "Your confirmation code to reset your password is: ".$randomNumber;
//$mail->AltBody =;
$mail->send();
} catch (Exception $e) {
print json_encode("error");
}
}else print json_encode("notfound");
mysqli_close($con);
?>
내가 얻는 오류는 다음과 같습니다.
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://127.0.0.1/interactive-contact-list/sendEmail.php", ok: false, …}
error:
error: SyntaxError: Unexpected number in JSON at position 6 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:8100/vendor.js:7290:51) at ZoneDelegate.invokeTask (http://localhost:8100/polyfills.js:3505:35) at Object.onInvokeTask (http://localhost:8100/vendor.js:64524:33) at ZoneDelegate.invokeTask (http://localhost:8100/polyfills.js:3504:40) at Zone.runTask (http://localhost:8100/polyfills.js:3273:51) at ZoneTask.invokeTask [as invoke] (http://localhost:8100/polyfills.js:3586:38) at invokeTask (http://localhost:8100/polyfills.js:4727:18) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:8100/polyfills.js:4764:25)
message: "Unexpected number in JSON at position 6"
stack: "SyntaxError: Unexpected number in JSON at position 6↵ at JSON.parse (<anonymous>)↵ at XMLHttpRequest.onLoad (http://localhost:8100/vendor.js:7290:51)↵ at ZoneDelegate.invokeTask (http://localhost:8100/polyfills.js:3505:35)↵ at Object.onInvokeTask (http://localhost:8100/vendor.js:64524:33)↵ at ZoneDelegate.invokeTask (http://localhost:8100/polyfills.js:3504:40)↵ at Zone.runTask (http://localhost:8100/polyfills.js:3273:51)↵ at ZoneTask.invokeTask [as invoke] (http://localhost:8100/polyfills.js:3586:38)↵ at invokeTask (http://localhost:8100/polyfills.js:4727:18)↵ at XMLHttpRequest.globalZoneAwareCallback (http://localhost:8100/polyfills.js:4764:25)"
__proto__: Error
constructor: ƒ SyntaxError()
arguments: (...)
caller: (...)
length: 1
name: "SyntaxError"
prototype: Error {name: "SyntaxError", message: "", constructor: ƒ}
__proto__: ƒ Error()
[[Scopes]]: Scopes[0]
message: ""
name: "SyntaxError"
__proto__:
constructor: ƒ Error()
message: ""
name: "Error"
toString: ƒ toString()
__proto__: Object
text: ""sent"2020-12-27 13:33:10 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP s6sm56953899wro.79 - gsmtp<br>↵2020-12-27 13:33:10 CLIENT -> SERVER: EHLO 127.0.0.1<br>↵2020-12-27 13:33:11 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2a00:6920:f0ef:dc7f:e42f:c0d7:fb12:ac22]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: STARTTLS<br>↵2020-12-27 13:33:11 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: EHLO 127.0.0.1<br>↵2020-12-27 13:33:11 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2a00:6920:f0ef:dc7f:e42f:c0d7:fb12:ac22]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: AUTH LOGIN<br>↵2020-12-27 13:33:11 SERVER -> CLIENT: 334 VXNlcm5hbWU6<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: [credentials hidden]<br>↵2020-12-27 13:33:11 SERVER -> CLIENT: 334 UGFzc3dvcmQ6<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: [credentials hidden]<br>↵2020-12-27 13:33:11 SERVER -> CLIENT: 235 2.7.0 Accepted<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: MAIL FROM:<[email protected]><br>↵2020-12-27 13:33:11 SERVER -> CLIENT: 250 2.1.0 OK s6sm56953899wro.79 - gsmtp<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: RCPT TO:<[email protected]><br>↵2020-12-27 13:33:11 SERVER -> CLIENT: 250 2.1.5 OK s6sm56953899wro.79 - gsmtp<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: DATA<br>↵2020-12-27 13:33:11 SERVER -> CLIENT: 354 Go ahead s6sm56953899wro.79 - gsmtp<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: Date: Sun, 27 Dec 2020 13:33:10 +0000<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: To: [email protected]<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: From: "[email protected]" <[email protected]><br>↵2020-12-27 13:33:11 CLIENT -> SERVER: Subject: Password reset email for account: [email protected]<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: Message-ID: <[email protected]><br>↵2020-12-27 13:33:11 CLIENT -> SERVER: X-Mailer: PHPMailer 6.1.7 (https://github.com/PHPMailer/PHPMailer)<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: MIME-Version: 1.0<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: Content-Type: text/html; charset=iso-8859-1<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: <br>↵2020-12-27 13:33:11 CLIENT -> SERVER: Your confirmation code to reset your password is: 805919<br>↵2020-12-27 13:33:11 CLIENT -> SERVER: <br>↵2020-12-27 13:33:11 CLIENT -> SERVER: .<br>↵2020-12-27 13:33:12 SERVER -> CLIENT: 250 2.0.0 OK 1609075990 s6sm56953899wro.79 - gsmtp<br>↵2020-12-27 13:33:12 CLIENT -> SERVER: QUIT<br>↵2020-12-27 13:33:12 SERVER -> CLIENT: 221 2.0.0 closing connection s6sm56953899wro.79 - gsmtp<br>↵"
__proto__:
constructor: ƒ Object()
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ ()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()
headers: HttpHeaders
lazyInit: () => {…}
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
message: "Http failure during parsing for http://127.0.0.1/interactive-contact-list/sendEmail.php"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://127.0.0.1/interactive-contact-list/sendEmail.php"
__proto__: HttpResponseBase
constructor: class HttpErrorResponse
__proto__: Object
나는 print json_encode ( "")을 사용하여 PHP에서 ionic으로 응답을 보내고 있는데, 이것은 이것을 제외한 모든 곳에서 작동합니다.
문제는 다음과 같습니다.
$mail->SMTPDebug = 2;
PHPMailer의 디버그 출력은 JSON과 호환되지 않으므로 끄십시오.
$mail->SMTPDebug = 0;
물론 자신의 디버그 출력 콜백을 삽입하여 JSON으로 변환 할 수 있지만 이것이 원하는 작업인지 의심 스럽습니다.
이와 같은 것을 디버깅 할 때는 항상 브라우저 개발 도구에서 서버로부터받는 실제 응답을 살펴보십시오. 만약 살펴 보았다면 응답에서 PHPMailer 디버그 출력을 보았을 것입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다