无法使用PHPMailer和Mailtrap发送邮件

时间:2017-08-01 22:10:52

标签: php mysql email phpmailer

我正在尝试将测试电子邮件发送到mailtrap.io收件箱,但是当我打开调试时,我一直收到这些错误。以前我收到用户名命令失败,直到我添加了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

以下是我尝试发送电子邮件的页面上的代码。凭据存储在Config类中,我已经多次验证用户名和密码与mailtrap收件箱提供的用户名和密码相匹配。目前端口设置为465.我已经尝试了2525以及相同的结果。端口25仅使应用程序超时。

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('myemail@gmail.com', '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"));
            }
        }
    }

}

3 个答案:

答案 0 :(得分:0)

发现了这个问题。您不能只在没有任何类型的SMTP服务器设置的情况下从localhost发送邮件,这就是我收到错误的原因。对于将来遇到此问题的任何人,Papercut for Windows可用于测试本地计算机上的邮件发送。

答案 1 :(得分:0)

这对我有用:

禁用 SMTPSecure

$mail->SMTPSecure = false;

并将 SMTP 端口更改为 2525

$mail->Port = 2525;

答案 2 :(得分:0)

您是否尝试过允许Gmail帐户中的“不安全的”应用访问: https://myaccount.google.com/lesssecureapps?pli=1