[英]Sending mail with PHPMailer SMTP on localhost fails to send
我想在我的網站上有一個簡單的 email 驗證系統。
有人告訴我使用 PHPMailer 來執行此操作,但發送實際的 email 似乎總是失敗,我無法弄清楚很多配置可能性實際上在做什么。
這是我得到的錯誤:
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
如果我嘗試使用 localhost 執行此操作,這是我得到的錯誤。 我正在使用 XAMPP 與 Apache 和 MySql 但我還沒有安裝“水星”,因為我真的不明白這一切是如何工作的。
最后,我希望在這里在線托管: 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 上。
您在這里擁有所需的一切:
535-5.7.8 用戶名和密碼不被接受。 在 535 5.7.8 https://support.google.com/mail/?p=BadCredentials l19sm817499edb.50 - gsmtp 了解更多信息
您需要設置您的登錄名並從您的 gmail 框中傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.