在本地主机上使用PHPMailer STMP发送邮件无法发送

时间:2019-10-10 09:17:04

标签: php xampp localhost phpmailer

我想在我的网站上建立一个简单的电子邮件验证系统。

有人告诉我使用PHPMailer进行此操作,但是发送实际的电子邮件似乎总是失败,并且我无法弄清楚实际上有很多配置可能性。

这是我遇到的错误:

2019-10-10 09:05:53 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP l19sm817499edb.50 - gsmtp
2019-10-10 09:05:53 CLIENT -> SERVER: EHLO localhost
2019-10-10 09:05:53 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2a02:aa10:e27d:d780:d905:c422:7d5d:365d]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-10-10 09:05:53 CLIENT -> SERVER: STARTTLS
2019-10-10 09:05:53 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2019-10-10 09:05:53 CLIENT -> SERVER: EHLO localhost
2019-10-10 09:05:53 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2a02:aa10:e27d:d780:d905:c422:7d5d:365d]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-10-10 09:05:53 CLIENT -> SERVER: AUTH LOGIN
2019-10-10 09:05:53 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2019-10-10 09:05:53 CLIENT -> SERVER: <credentials hidden>
2019-10-10 09:05:53 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2019-10-10 09:05:53 CLIENT -> SERVER: <credentials hidden>
2019-10-10 09:05:53 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials l19sm817499edb.50 - gsmtp
2019-10-10 09:05:53 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials l19sm817499edb.50 - gsmtp
SMTP Error: Could not authenticate.
2019-10-10 09:05:53 CLIENT -> SERVER: QUIT
2019-10-10 09:05:53 SERVER -> CLIENT: 221 2.0.0 closing connection l19sm817499edb.50 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message has been sent

这是我尝试使用本地主机时遇到的错误。我正在将XAMPP与Apache和MySql一起使用,但由于没有真正理解所有工作原理,因此尚未安装“ Mercury”。

最后,我希望将其托管在这里:OpenStringStudios,所以我不知道“水星”与这方面的关系。

这是我正在使用的php代码:

<?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;

    // echo "Hello World!";

    if (isset($_POST['submit'])) {
        $con = new mysqli('localhost', 'root', '', 'osscom_users_test');

        $name = $con->real_escape_string($_POST['name']);
        $email = $con->real_escape_string($_POST['email']);
        $pass = $con->real_escape_string($_POST['pass']);

        // test if the username is already taken
        // one email addresses can have multiple users

        $token = 'qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM0123456789';
        $token = str_shuffle($token);
        $token = substr($token, 0, 10);

        $hashedPass = password_hash($pass, PASSWORD_BCRYPT);

        $con->query("INSERT INTO test_users (NAME, EMAIL, PASSWORD, VERIFIED, TOKEN)
            VALUES ('$name', '$email', '$hashedPass', '0', '$token');
        ");

        include_once "PHPMailer/PHPMailer.php";
        include_once "PHPMailer/Exception.php";
        include_once "PHPMailer/SMTP.php";

        try {
            // Server settings
            $mail = new PHPMailer();

            $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
            $mail->isSMTP();                                            // Send using SMTP
            $mail->Host       = 'smtp.gmail.com';                       // Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
            $mail->Username   = 'root';                                 // SMTP username
            $mail->Password   = '';                                     // SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
            $mail->Port       = 587;                                    // TCP port to connect to

            $mail->setFrom('openstring.studios@gmail.com', 'OpenString Team');
            $mail->addAddress($email, $name);
            $mail->isHTML(true);
            $mail->Subject = "Welcome To The OpenString Membership, Please Verify Your Account";
            $mail->Body = "
                <h1>Welcome To The OpenString Membership</h1>
                Thanks for joining, please click the link below to verify your account.<br>
                Name: $name<br>
                <a href='localhost/session/activation?email=$email&token=$token'>Click Here To Activate Your Account.</a>
                <p>
                    Legal Statements:
                    By clicking this link you agree to all our terms of services.
                </p>
            ";
            $mail->send();
            echo 'Message has been sent';
        } catch(Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrofInfo}";
        }

        // Redirect('account', false);
    }

    function Redirect($url, $permanent = false) {
        if (headers_sent() === false) {
            header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
        }
        exit();
    }
?>

我希望获得一些帮助,因为我不太了解如何解决此问题。对我来说尤其重要的是,它既可以在我的本地主机上运行,​​又可以在domain.com上托管时工作。.

希望您能帮帮我!谢谢!

1 个答案:

答案 0 :(得分:1)

您在这里拥有所需的一切:

535-5.7.8不接受用户名和密码。在535 5.7.8 https://support.google.com/mail/?p=BadCredentials l19sm817499edb.50-gsmtp

中了解更多

您需要设置登录名并通过Gmail邮箱进行传递。