如何从PHP中的另一个文件执行另一个函数?

如何从PHP中的另一个文件执行另一个函数?,php,amazon-web-services,function,phpmailer,mailer,Php,Amazon Web Services,Function,Phpmailer,Mailer,在创建新用户以发送验证电子邮件之后,我试图从另一个类执行PHPMailer函数,我想知道为什么什么都没有发生 我的邮件程序配置100%正常工作,因此函数调用可能有问题。谢谢大家! 文件1: 我的createUser函数: public function createUser($data){ if(isset($data->firstname)){$firstname = htmlspecialchars($data->firstname);}else{return f

在创建新用户以发送验证电子邮件之后,我试图从另一个类执行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.
        }
    }
}
使用PHPMailer\PHPMailer\PHPMailer;
使用PHPMailer\PHPMailer\Exception;
类邮递员{
公共函数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;}
//如有必要,请修改下面require语句中的路径,以引用
//Composer autoload.php文件的位置。
需要“vendor/autoload.php”;
//替换sender@example.com用你的“发件人”地址。
//必须通过Amazon SES验证此地址。
$sender='###'';
$senderName='####';
//替换recipient@example.com带有“收件人”地址。如果您的帐户
//仍在沙箱中,必须验证此地址。
$recipient=';
//将smtp_用户名替换为您的Amazon SES smtp用户名。
$usernameSmtp='####';
//将smtp_密码替换为您的Amazon SES smtp密码。
$passwordSmtp='###'';
//指定配置集。如果不想使用配置
//设置、注释或删除下一行。
//$configurationSet='ConfigSet';
//如果您在美国西部(俄勒冈州)以外的地区使用亚马逊SES,
//将email-smtp.us-west-2.amazonaws.com替换为Amazon SES smtp
//端点位于适当的区域。
$host='####'';
$port=587;
//电子邮件的主题行
$subject='amazonses测试(使用PHP访问SMTP接口)';
//电子邮件的纯文本正文
$bodyText=“电子邮件测试\r\n此电子邮件是通过
Amazon SES SMTP接口使用PHPMailer类。“;
//电子邮件的HTML格式正文
$bodyHtml='亲爱的

感谢您注册到####。

'; $mail=新的PHPMailer(true); 试一试{ //指定SMTP设置。 $mail->isSMTP(); $mail->setFrom($sender,$senderName); $mail->Username=$usernamemtp; $mail->Password=$passwordSmtp; $mail->Host=$Host; $mail->Port=$Port; $mail->SMTPAuth=true; $mail->SMTPSecure='tls'; //$mail->addCustomHeader('X-SES-CONFIGURATION-SET',$configurationSet); //指定邮件收件人。 $mail->addAddress($recipient); //您还可以在此处向收件人添加抄送、密件抄送和其他内容。 //指定消息的内容。 $mail->isHTML(true); $mail->Subject=$Subject; $mail->Body=$bodyHtml; $mail->AltBody=$bodyText; $mail->Send(); echo“Email sent!”,PHP_EOL; }捕获(phpmailerException$e){ echo“发生错误。{$e->errorMessage()}”,PHP_EOL;//从PHPMailer捕获错误。 }捕获(例外$e){ echo“Email not sent.{$mail->ErrorInfo}”,PHP_EOL;//从Amazon SES捕获错误。 } } }
您的用户是在数据库中创建的吗?SQL是否按预期执行?$data是如何传递给Mailer::sendMail的?您正在将sendVerificationEmail()作为函数调用,而不是作为对象的方法调用。谢谢您的回答。是的,整个create user函数也工作得很好,我调用了$this->sendVerificationEmail()这样的方法,但不知怎的,它仍然不工作,但我会设法解决它。