使用$ _ENV作为phpmailer中的凭据时的smpt错误

问题描述

使用硬编码的用户名/电子邮件/密码时,使用PHPmailer发送消息没有问题。但是,当我使用$ _ENV隐藏凭据时,出现smtp错误,如下所示:

    2020-09-08 15:50:51 SERVER -> CLIENT: 220 dd45234.kasserver.com ESMTP
    2020-09-08 15:50:51 CLIENT -> SERVER: EHLO browsegenres-f3.loc
    2020-09-08 15:50:51 SERVER -> CLIENT: 250-dd45234.kasserver.com250-PIPELINING250-SIZE 102400000250-VRFY250-ETRN250-STARTTLS250-AUTH PLAIN LOGIN250-AUTH=PLAIN LOGIN250-ENHANCEDSTATUSCODES250-8BITMIME250 DSN
    2020-09-08 15:50:51 CLIENT -> SERVER: STARTTLS
    2020-09-08 15:50:51 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
    2020-09-08 15:50:51 CLIENT -> SERVER: EHLO xxxxxxxxxxxxxxxxxxxx.loc
    2020-09-08 15:50:51 SERVER -> CLIENT: 250-xxxxxxxx.[SERVER].com250-PIPELINING250-SIZE 102400000250-VRFY250-ETRN250-AUTH PLAIN LOGIN250-AUTH=PLAIN LOGIN250-ENHANCEDSTATUSCODES250-8BITMIME250 DSN
    2020-09-08 15:50:51 CLIENT -> SERVER: AUTH LOGIN
    2020-09-08 15:50:51 SERVER -> CLIENT: 334 VXNlcm5hbWU6
    2020-09-08 15:50:51 CLIENT -> SERVER: [credentials hidden]
    2020-09-08 15:50:53 SERVER -> CLIENT: 535 5.7.8 Error: authentication Failed: VXNlcm5hbWU6
    2020-09-08 15:50:53 SMTP ERROR: Username command Failed: 535 5.7.8 Error: authentication Failed: VXNlcm5hbWU6
    SMTP Error: Could not authenticate.
    2020-09-08 15:50:53 CLIENT -> SERVER: QUIT
    2020-09-08 15:50:53 SERVER -> CLIENT: 221 2.0.0 Bye
    SMTP Error: Could not authenticate.
    Message Could not be sent. Mailer Error: SMTP Error: Could not authenticate.

我不希望对凭证进行硬编码。知道如何消除此错误吗?

这是代码

//启动PHPMailer $ mail = new PHPMailer(true);

            // see config file
            $mailSenderName = $_ENV['MAILER_CONTACT_USERNAME'];
            $masterPassword = $_ENV['MAILER_CONTACT_PASSWORD'];
            $masterEmail = $_ENV['MAILER_CONTACT_EMAIL'];
            $recipient = $_ENV['MAILER_CONTACT_RECIPIENT'];


            try {
                //Server settings
                $mail->SMTPDebug  = SMTP::DEBUG_SERVER;
                $mail->isSMTP();
                $mail->Host       = 'xxxxxxx.[SERVER].com';
                $mail->SMTPAuth   = true; 
                $mail->Username   = $masterEmail;
                $mail->Password   = $masterPassword;
                $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
                $mail->Port       = 25;


                //Recipients
                $mail->setFrom('[email protected]','aabbcc');
                $mail->addAddress('[email protected]');

                // Content
                $mail->isHTML(true);
                $mail->Subject = 'Message Received (Contact Page)';
                $emailbody =
                    'There is a new message from: <br>' .
                    '==================================== <br>' .
                    $senderName . '<br>' .
                    $senderEmail . '<br' .
                    '====================================' .
                    $message . '<br>' .
                    '====================================';

                $mail->Body    = $emailbody;
                $mail->send();
                // success,show thank you
                $f3->reroute('/contact/thankyou'); //todo
            } catch (\Exception $e) {
                echo "Message Could not be sent. Mailer Error: {$mail->ErrorInfo}";
            }

谢谢!

解决方法

一次调试一件事。当您知道自己有问题之前,查看电子邮件中的错误是没有意义的。 PHPMailer会使用您提供的任何内容,因此您需要确保提供正确的内容

在这种情况下,您可以减少代码以减少调试:

AudioServiceBackground.setStart

一旦您知道自己正确设置了var_dump($_ENV); 的内容(无论是从真实的环境变量,dotenv脚本,php.ini配置等),就可以然后开始使用您电子邮件代码中的值。

,

安装dotenv(vlucas)后,我只是没有正确地将它包含在ContactController中。这就是为什么var_dump($ _ ENV)总是导致NULL的原因。我将自己的设置与另一条路线NewsletterController进行了比较。区别在于,在此路由中,我查询数据库,并且在模型构造函数(已设置db连接)中,我正确地“使用”了dotenv类,这就是$ _ENV充满数据的原因。我根本没看到。

因此,在ContactController中,我设置了:

    use \Dotenv;

并且在初始化phpmailer之后,我添加了:

            $mail = new PHPMailer(true);
            
            $dotenv = Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT']);
            $dotenv->load();

与Models类的区别(数据库连接):

            namespace Models;

            use \Dotenv;

            abstract class Model
            {
                protected $db;

                public function __construct()
                {
                    $dotenv = Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT']);
                    $dotenv->load();

                    $this->db = new \DB\SQL(
                        'mysql:host='. $_ENV['DB_HOST'] .';port='.$_ENV['DB_PORT'].';dbname='.$_ENV['DB_NAME'],$_ENV['DB_USERNAME'],$_ENV['DB_PASSWORD']
                    );
                }
            }