简体   繁体   English

我在尝试发送电子邮件验证时遇到错误

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

I am trying to send a confirmation email on signup from my flutter app using PHP as my backend.我正在尝试使用 PHP 作为后端从我的颤振应用程序发送注册确认电子邮件。 My PHP backend is hosted on HostGator.我的 PHP 后端托管在 HostGator 上。

So this is my PHP script responsible for sending the mail.所以这是我负责发送邮件的 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

            ]);         

        }
}
?>

But I get the error below in my flutter app console when the script above runs.但是当上面的脚本运行时,我在我的颤振应用程序控制台中收到以下错误。

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'

What could be the cause?可能是什么原因?

The problem is that you uncommented some comments, and these are just strings dangling in PHP Space.问题是您取消了一些注释的注释,而这些只是在 PHP 空间中悬空的字符串。 The comments I added here, and check the rest of your code for the same thing.我在此处添加的注释,并检查您的其余代码是否相同。

        // 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.

相关问题 为什么在尝试使用图形 API 发送电子邮件时出现以下不受支持的媒体错误? - Why I am getting the following unsupported media error while trying to send the email using graph api? 尝试使用 Laravel 发送电子邮件时出错 - Getting error while trying to send an email with Laravel 为什么我在尝试使用phpmailer发送电子邮件激活新用户帐户时收到此错误 - Why am i getting this error when trying to send email for activation of new user account with phpmailer 尝试在 localhost php 中发送电子邮件时出错 - Getting error while trying to send email in localhost php 我正在尝试使用 laravel 发送电子邮件,但出现此错误“stream_socket_client(): 无法连接到 tcp://smtp.gmail.com:587” - I am trying to send email using laravel and i am getting this error “stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587” 为什么在尝试加载模型时出现应用程序错误? - Why am I getting Application error while trying to load a model? 尝试连接到本地主机phpmyadmin时出现错误 - I am getting error while trying to connect to localhost phpmyadmin 尝试包含reader.php时出现错误 - I am getting an error while trying to include reader.php 尝试发送电子邮件时PHP错误 - Error in php while trying to send email 尝试使用PHPMAILER发送电子邮件时出错 - Error while trying to send email using PHPMAILER
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM