Search code examples
smtpenvironment-variablesphpmailer

SMTP error when using $_ENV for credentials in PHPMailer


When using hard-coded username / email / password I have no problem getting a message sent with phpmailer. But when I use $_ENV to hide the credentials I get the smtp error as shown here:

    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.

I don't wan to hardcode the credentials. Any idea how to get rid of this error?

Here's the code:

// initiate 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('aaa@bbbbbbbbbbb.com', 'aabbcc');
                $mail->addAddress('mmmmmmmmm@bbbbbbbbbbb.com');

                // 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}";
            }

Thanks!


Solution

  • Debug one thing at a time. There's no point in looking at error in your email when you know you know you have a problem before it ever gets that far. PHPMailer uses whatever you give it, so you need to be sure you're giving it the right thing.

    You could reduce the code to debug in this case by cutting it back to:

    var_dump($_ENV);
    

    Once you know that you're setting the contents of $_ENV correctly (whether from real env vars, from a dotenv script, your php.ini config, etc), you can then start using the values in your email code.