我应该如何从PHP中的另一个文件执行另一个功能?

创建新用户以发送验证电子邮件后,我试图从另一个类执行PHPMailer函数,我想知道为什么什么都没发生。

我的邮件程序配置正在100%运行,因此该函数的调用可能存在问题。谢谢!

文件1:

我的createUser函数:

public function createUser($data){
        if(isset($data->firstname)){$firstname = htmlspecialchars($data->firstname);}else{return false;}
        if(isset($data->lastname)){$lastname = htmlspecialchars($data->lastname);}else{return false;}
        if(isset($data->email)){$email = htmlspecialchars($data->email);}else{return false;}
        if(isset($data->password)){$password = $data->password;}else{return false;}

        /*if($this->existEmail($data)){
            $this->setResult("EXIST");
            return true;   
        }*/

        $hash = md5(rand(0,1000));
        $password = password_hash($data->password,PASSWORD_BCRYPT);

        $sql = "INSERT INTO users (firstname,lastname,email,password,hash) 
        VALUES(:firstname,:lastname,:email,:password,:hash)";

        $stmt = $this->database->getPDO()->prepare($sql);

        $stmt->bindParam(':firstname',$firstname);
        $stmt->bindParam(':lastname',$lastname);
        $stmt->bindParam(':email',$email);
        $stmt->bindParam(':password',$password);
        $stmt->bindParam(':hash',$hash);
        if($stmt->execute()){
            $this->setResult("OK");
            sendVerificationEmail();
            return true;
        }else{
            return false;
        }
    }

我从createUser调用以发送电子邮件的函数:

public function sendVerificationEmail(){
        $mailer = new Mailer();
        if($mailer->sendMail($data)){
            $this->setResult("OK");
            return true;
        }else{
            return false;
        }
    }

文件2:

我的邮件班:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;


class Mailer{

    public function sendMail($data){

        //if(isset($data->name)){$name = htmlspecialchars($data->name);}else{return false;}
        //if(isset($data->email)){$email = htmlspecialchars($data->email);}else{return false;}
        //if(isset($data->hash)){$hash = htmlspecialchars($data->hash);}else{return false;}

        // If necessary,modify the path in the require statement below to refer to the
        // location of your Composer autoload.php file.
        require 'vendor/autoload.php';

        // Replace sender@example.com with your "From" address.
        // This address must be verified with Amazon SES.
        $sender = '###';
        $senderName = '###';

        // Replace recipient@example.com with a "To" address. If your account
        // is still in the sandbox,this address must be verified.
        $recipient = '###';

        // Replace smtp_username with your Amazon SES SMTP user name.
        $usernameSmtp = '###';

        // Replace smtp_password with your Amazon SES SMTP password.
        $passwordSmtp = '###';

        // Specify a configuration set. If you do not want to use a configuration
        // set,comment or remove the next line.
        //$configurationSet = 'ConfigSet';

        // If you're using Amazon SES in a region other than US West (Oregon),// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
        // endpoint in the appropriate region.
        $host = '###';
        $port = 587;

        // The subject line of the email
        $subject = 'Amazon SES test (SMTP interface accessed using PHP)';

        // The plain-text body of the email
        $bodyText =  "Email Test\r\nThis email was sent through the
            Amazon SES SMTP interface using the PHPMailer class.";

        // The HTML-formatted body of the email
        $bodyHtml = '<p>Dear,</p>
            <p>Thank you for registering to <a>###</a>.</p>';

        $mail = new PHPMailer(true);

        try {
            // Specify the SMTP settings.
            $mail->isSMTP();
            $mail->setfrom($sender,$senderName);
            $mail->username   = $usernameSmtp;
            $mail->Password   = $passwordSmtp;
            $mail->Host       = $host;
            $mail->Port       = $port;
            $mail->SMTPAuth   = true;
            $mail->SMTPSecure = 'tls';
            //$mail->addCustomHeader('X-SES-CONFIGURATION-SET',$configurationSet);

            // Specify the message recipients.
            $mail->addAddress($recipient);
            // You can also add CC,BCC,and additional To recipients here.

            // Specify the content of the message.
            $mail->isHTML(true);
            $mail->Subject    = $subject;
            $mail->Body       = $bodyHtml;
            $mail->AltBody    = $bodyText;
            $mail->Send();
            echo "Email sent!",PHP_EOL;
        } catch (phpmailerException $e) {
            echo "An error occurred. {$e->errorMessage()}",PHP_EOL; //Catch errors from PHPMailer.
        } catch (Exception $e) {
            echo "Email not sent. {$mail->ErrorInfo}",PHP_EOL; //Catch errors from Amazon SES.
        }
    }
}
heiheiliangliang 回答:我应该如何从PHP中的另一个文件执行另一个功能?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2610384.html

大家都在问