2

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

}

7
  • Set $mail->SMTPDebug = 2; so you can see what the server is saying. TLS won't work to port 465; try 587. Also, read the troubleshooting guide the error message points you at.
    – Synchro
    Commented Aug 1, 2017 at 22:24
  • Using that port gives a timeout. Mailtrap says to use 25, 465 or 2525 for SMTP connections. I will edit this post with debugging level 2 information under port 465. Commented Aug 1, 2017 at 22:34
  • Maybe this could help
    – Yolo
    Commented Aug 1, 2017 at 22:57
  • In that case set SMTPSecure = 'ssl' with Port = 465, though note that this is exceedingly unlikely to work correctly when Host = 'localhost'. All of this is covered in the guide.
    – Synchro
    Commented Aug 1, 2017 at 23:12
  • So it doesn't like your ID or password?
    – Synchro
    Commented Aug 1, 2017 at 23:15

3 Answers 3

1

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.

1

This is what worked for me:

Disable SMTPSecure

$mail->SMTPSecure = false;

And change the SMTP Port to 2525

$mail->Port = 2525;
0

did you try to allow Less secure app access in you gmail account : https://myaccount.google.com/lesssecureapps?pli=1

Not the answer you're looking for? Browse other questions tagged or ask your own question.