Search code examples
phpmysqlemailphpmailer

Unable to send mail with PHPMailer and Mailtrap


I am trying to send a test e-mail to a mailtrap.io inbox, but I keep getting these errors when I turn the debugging on. Previously I was getting a Username command failed until I added AuthType = Login.

2017-08-01 22:34:46 SERVER -> CLIENT: 220 mailtrap.io ESMTP ready 
2017-08-01 22:34:46 CLIENT -> SERVER: EHLO localhost 
2017-08-01 22:34:46 SERVER -> CLIENT: 250-mailtrap.io 250-SIZE 5242880 250-PIPELINING 250-ENHANCEDSTATUSCODES 250-8BITMIME 250-DSN 250-AUTH PLAIN LOGIN CRAM-MD5 250 STARTTLS 
2017-08-01 22:34:46 CLIENT -> SERVER: STARTTLS 
2017-08-01 22:34:46 SERVER -> CLIENT: 220 2.0.0 Start TLS 
2017-08-01 22:34:47 CLIENT -> SERVER: EHLO localhost 
2017-08-01 22:34:47 SERVER -> CLIENT: 250-mailtrap.io 250-SIZE 5242880 250-PIPELINING 250-ENHANCEDSTATUSCODES 250-8BITMIME 250-DSN 250 AUTH PLAIN LOGIN CRAM-MD5 
2017-08-01 22:34:47 CLIENT -> SERVER: AUTH LOGIN 
2017-08-01 22:34:47 SERVER -> CLIENT: 334 xxxx
2017-08-01 22:34:47 CLIENT -> SERVER: xxxxxxxxxxx= 
2017-08-01 22:34:47 SERVER -> CLIENT: 334 xxxxxxxxxxxx 
2017-08-01 22:34:47 CLIENT -> SERVER: 
2017-08-01 22:34:50 SERVER -> CLIENT: 535 5.7.0 Invalid login or password 2017-08-01 22:34:50 SMTP ERROR: Password command failed: 535 5.7.0 Invalid login or password 
2017-08-01 22:34:50 SMTP Error: Could not authenticate. 
2017-08-01 22:34:50 CLIENT -> SERVER: QUIT 
2017-08-01 22:34:51 SERVER -> CLIENT: 221 2.0.0 Bye 
2017-08-01 22:34:51 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Here is my code on the page that tries to send the e-mail. The credentials are stored in the Config class, and I have verified several times that the username and password match the ones provided by the mailtrap inbox. Currently the port is set to 465. I have tried 2525 as well to the same results. Port 25 only makes the application time out.

if(isMethod('post')) {
    if (isset($_POST['email'])) {
        /*********************
         *configure PHPMailer*
         *********************/
        $mail = new PHPMailer();
        $mail->AuthType = 'LOGIN';
        $mail->SMTPDebug = 2;
        $mail->isSMTP();
        $mail->Host = Config::SMTP_HOST;
        $mail->SMTPAuth = true;
        $mail->Username = Config::SMTP_USER;
        $mail->Passowrd = Config::SMTP_PASSWORD;
        $mail->SMTPSecure = 'tls';
        $mail->Port = Config::SMTP_PORT;
        $mail->isHTML(true);
        $mail->setFrom('[email protected]', 'My Name');
        if (!getEmailDb()) {
            echo (displayMessage("E-Mail does not exist.", "warning"));
        }
        else {
            $email = getEmailDb();
            $token = bin2hex(openssl_random_pseudo_bytes(50));
            $mail->addAddress($email);
            $mail->Subject = 'This is a test email.';
            $mail->Body = 'Email body';

            if($mail->send()) {
                echo (displayMessage("E-Mail sent. It should show up in your inbox pretty soon.", "success"));
            }
            else {
                echo (displayMessage("Could not send E-Mail to ".$email, "warning"));
            }
        }
    }

}


Solution

  • Found out the issue. You can't just send mail from localhost without any kind of SMTP server set up, that's why I was getting errors. For anyone that comes across this in the future, Papercut for Windows can be used to test mail sending on your local machine.