Как мне выполнить другую функцию из другого файла в PHP? - PullRequest
0 голосов
/ 20 марта 2020

Я пытаюсь выполнить функцию 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:

Мой класс Mailer:

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