我得到错误,而试图发送电子邮件验证



我试图发送一个确认电子邮件从我的应用程序使用PHP作为我的后端注册。我的PHP后端托管在HostGator上。

这是我的PHP脚本负责发送邮件

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
require_once "vendor/autoload.php";
function create_token($id, $db, $email, $name, $smtp_host, $mail_username, 
$mail_password, $mail_port,$mail_from, $company_name,$web_url, $email_expire_time)
{
$token = bin2hex(random_bytes(16));
$created_time=strtotime(date("Y-m-d h:i:sa"));
$expire_time =strtotime("+$email_expire_time day");
$mail = new PHPMailer(true);

//SMTP host name                          
$mail->Host = 'https://webhostexample.app';
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
// username and password     
$mail->Username = 'notify@webhostexample.app';                 
$mail->Password = '************';                           
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to
$mail->Port = 465;                                   
$mail->setFrom($mail_from , $company_name);

$mail->addAddress($email, $name);
$mail->isHTML(true);
$mail->Subject = "Email Confirmation";
$message = file_get_contents("confirm-mail.php");
$variables = array(
"{{company_name}}" => $company_name,
"{{confirm_link}}" => $web_url."email-verification.php?token=" .$token,
"{{expire_date}}" => $email_expire_time,
);
foreach($variables as $key=>$value ){
$message = str_replace($key, $value, $message);
}

$mail->msgHTML($message);
try {
$mail->send();
$stmt = $db->prepare("INSERT INTO email_verification (s_n, uid, token, created_time, expire_time) VALUES (?, ? , ? , ? , ?)");
$stmt->execute([NULL, $id, $token, $created_time, $expire_time]);    

} catch (Exception $e) {
echo json_encode([
'status' => "failed",
'message' => "Mailer Error: " . $mail->ErrorInfo,
'exception'=> $e
]);         
}
}
?>

但是当上面的脚本运行时,我在我的flutter应用程序控制台得到下面的错误。

I/flutter ( 6653): FormatException: Unexpected character (at character 98)
I/flutter ( 6653): ...iate mail function.","exception":{}}{"status":"success","message":"Check...
I/flutter ( 6653):                                        ^
I/flutter ( 6653): type 'Null' is not a subtype of type 'bool'

原因是什么?

问题是您取消了一些注释,而这些注释只是在PHP空间中悬挂的字符串。我在这里添加的注释,并检查您的代码的其余部分是否相同。

// SMTP host name                         
$mail->Host = 'https://webhostexample.app';
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
// username and password     
$mail->Username = 'notify@webhostexample.app';                 
$mail->Password = '************';             

最新更新