Когда я запускаю мой PHP Mailer, мой бэкэнд не отправляет ответ - PullRequest
0 голосов
/ 31 марта 2020

У меня есть MVC PHP бэкэнд, где все работает нормально, но когда я запускаю любую функцию, требующую отправки электронного письма, я не получаю никакого ответа, хотя электронная почта всегда успешно доставляется.

Мой класс Mailer с PHP Mailer:

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;


class Mailer{

    public function sendMail($data){
        if(isset($data->email)){$email = htmlspecialchars($data->email);}else{return false;}
        if(isset($data->subject)){$subject = htmlspecialchars($data->subject);}else{return false;}
        if(isset($data->bodyText)){$bodyText = htmlspecialchars($data->bodyText);}else{return false;}
        if(isset($data->bodyHtml)){$bodyHtml = $data->bodyHtml;}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_once __DIR__ . '/../vendor/autoload.php';
        // Replace sender@example.com with your "From" address.
        // This address must be verified with Amazon SES.
        $sender = 'xyz';
        $senderName = 'xyz';

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

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

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

        // 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 = 'xyz';
        $port = 587;

        // The HTML-formatted body of the email

        $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;
            error_log("true returned");
            return true;
        } 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.
        }
        return false;
    }
}

Так я вызываю функцию, которая вызывает класс Mailer:

public function sendActivationEmail($data){
                //some code 
                $this->sendNewEmail($data);
                $this->setResult("OK");
                return true;
    }

Затем я вызываю Mailer отсюда:

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

Функция setResult всегда работает, если я вызываю ее в любой другой функции!

...