簡體   English   中英

我在嘗試發送電子郵件驗證時遇到錯誤

[英]I am getting error while trying to send email verification

我正在嘗試使用 PHP 作為后端從我的顫振應用程序發送注冊確認電子郵件。 我的 PHP 后端托管在 HostGator 上。

所以這是我負責發送郵件的 PHP 腳本。

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
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

            ]);         

        }
}
?>

但是當上面的腳本運行時,我在我的顫振應用程序控制台中收到以下錯誤。

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 = '************';             

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM