英文:
Phpmailer not sending email to gmail
问题
以下是您的代码的翻译部分:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
session_start();
if (isset($_SESSION['SESSION_EMAIL'])) {
header("Location: welcome.php");
die();
}
// 加载Composer的自动加载器
require 'vendor/autoload.php';
include 'config.php';
$msg = "";
if (isset($_POST['submit'])) {
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn, md5($_POST['password']));
$confirm_password = mysqli_real_escape_string($conn, md5($_POST['confirm-password']));
$code = mysqli_real_escape_string($conn, md5(rand()));
if (mysqli_num_rows(mysqli_query($conn, "SELECT * FROM users WHERE email='{$email}'")) > 0) {
$msg = "<div class='alert alert-danger'>{$email} - 此电子邮件地址已存在。</div>";
} else {
if ($password === $confirm_password) {
$sql = "INSERT INTO users (name, email, password, code) VALUES ('{$name}', '{$email}', '{$password}', '{$code}')";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "<div style='display: none;'>";
// 创建一个实例;传递`true`启用异常
$mail = new PHPMailer(true);
try {
// 服务器设置
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // 启用详细的调试输出
$mail->isSMTP(); // 使用SMTP发送
$mail->Host = "tls://smtp.gmail.com"; // 设置用于发送的SMTP服务器
$mail->SMTPAuth = true; // 启用SMTP身份验证
$mail->Username = 'tk23@gmail.com'; // SMTP用户名
$mail->Password = '12345678'; // SMTP密码
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // 启用隐式TLS加密
$mail->Port = 587; // 连接的TCP端口; 如果已设置`SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`,则使用587
// 收件人
$mail->setFrom('tk23@gmail.com','Mailer');
$mail->addAddress($email);
// 内容
$mail->isHTML(true);
$mail->Subject = '无回复';
$mail->Body = '这是验证链接 <b><a href="http://localhost/login/?verification='.$code.'">http://localhost/login/?verification='.$code.'</a></b>';
$mail->send();
echo '消息已发送';
} catch (Exception $e) {
echo "无法发送消息。邮件发送程序错误:{$mail->ErrorInfo}";
}
echo "</div>";
$msg = "<div class='alert alert-info'>我们已向您的电子邮件地址发送了验证链接。</div>";
} else {
$msg = "<div class='alert alert-danger'>出现了问题。</div>";
}
} else {
$msg = "<div class='alert alert-danger'>密码和确认密码不匹配</div>";
}
}
}
?>
<!DOCTYPE html>
<html lang="zxx">
<head>
<title>登录表单 - 勇敢的编码者</title>
<!-- Meta tag Keywords -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8" />
<meta name="keywords" content="登录表单" />
<!-- //Meta tag Keywords -->
<link href="//fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
<!--/Style-CSS -->
<link rel="stylesheet" href="css/otp.css" type="text/css" media="all" />
<!--//Style-CSS -->
<script src="https://kit.fontawesome.com/af562a2a63.js" crossorigin="anonymous"></script>
</head>
<body>
<!-- form section start -->
<section class="w3l-mockup-form">
<div class="container">
<!-- /form -->
<div class="workinghny-form-grid">
<div class="main-mockup">
<div class="alert-close">
<span class="fa fa-close"></span>
</div>
<div class="w3l_form align-self">
<div class="left_grid_info">
<img src="images/image2.svg" alt="">
</div>
</div>
<div class="content-wthree">
<h2>现在注册</h2>
<p> Lorem ipsum dolor sit amet consectetur adipisicing elit. </p>
<?php echo $msg; ?>
<form action="" method="post">
<input type="text" class="name" name="name" placeholder="输入您的姓名" value="<?php if (isset($_POST['submit'])) { echo $name; } ?>" required>
<input type="email" class="email" name="email" placeholder="输入您的电子邮件" value="<?php if (isset($_POST['submit'])) { echo $email; } ?>" required>
<input type="password" class="password" name="password" placeholder="输入您的密码" required>
<input type="password" class="confirm-password" name="confirm-password" placeholder="输入您的确认密码" required>
<button name="submit" class="btn" type="submit">注册</button>
</form>
<div class="social-icons">
<p>已经有账户! <a href="guest_login.php">登录</a>.</p>
</div>
</div>
</div>
</div>
<!-- //form -->
</div>
</section>
<!-- //form section start -->
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function (c) {
$('.alert-close').on('click', function (c) {
$('.main-mockup').fadeOut('slow', function (c) {
$('.main-mockup').remove();
});
});
});
</script>
</body>
</html>
请注意,我已经对代码中的 HTML 和 PHP 进行了翻译,但未包括注释。如果您需要更多帮助或有其他问题,请随时提问。
英文:
<sub>I'm making an OTP verification mail for my registration form, and I'm using [tag:phpmailer] to send a verification email to Gmail. but unfortunately, it won't send. I watched a tutorial about making an OTP verification form using phpmailer, still it wont work</sub>
here are my codes for the registration form.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
session_start();
if (isset($_SESSION['SESSION_EMAIL'])) {
header("Location: welcome.php");
die();
}
//Load Composer's autoloader
require 'vendor/autoload.php';
include 'config.php';
$msg = "";
if (isset($_POST['submit'])) {
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn, md5($_POST['password']));
$confirm_password = mysqli_real_escape_string($conn, md5($_POST['confirm-password']));
$code = mysqli_real_escape_string($conn, md5(rand()));
if (mysqli_num_rows(mysqli_query($conn, "SELECT * FROM users WHERE email='{$email}'")) > 0) {
$msg = "<div class='alert alert-danger'>{$email} - This email address has been already exists.</div>";
} else {
if ($password === $confirm_password) {
$sql = "INSERT INTO users (name, email, password, code) VALUES ('{$name}', '{$email}', '{$password}', '{$code}')";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "<div style='display: none;'>";
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = "tls://smtp.gmail.com"; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'tk23@gmail.com'; //SMTP username
$mail->Password = '12345678'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('tk23@gmail.com','Mailer');
$mail->addAddress($email);
//Content
$mail->isHTML(true);
$mail->Subject = 'no reply';
$mail->Body = 'Here is the verification link <b><a href="http://localhost/login/?verification='.$code.'">http://localhost/login/?verification='.$code.'</a></b>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
echo "</div>";
$msg = "<div class='alert alert-info'>We've send a verification link on your email address.</div>";
} else {
$msg = "<div class='alert alert-danger'>Something wrong went.</div>";
}
} else {
$msg = "<div class='alert alert-danger'>Password and Confirm Password do not match</div>";
}
}
}
?>
<!DOCTYPE html>
<html lang="zxx">
<head>
<title>Login Form - Brave Coder</title>
<!-- Meta tag Keywords -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8" />
<meta name="keywords"
content="Login Form" />
<!-- //Meta tag Keywords -->
<link href="//fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
<!--/Style-CSS -->
<link rel="stylesheet" href="css/otp.css" type="text/css" media="all" />
<!--//Style-CSS -->
<script src="https://kit.fontawesome.com/af562a2a63.js" crossorigin="anonymous"></script>
</head>
<body>
<!-- form section start -->
<section class="w3l-mockup-form">
<div class="container">
<!-- /form -->
<div class="workinghny-form-grid">
<div class="main-mockup">
<div class="alert-close">
<span class="fa fa-close"></span>
</div>
<div class="w3l_form align-self">
<div class="left_grid_info">
<img src="images/image2.svg" alt="">
</div>
</div>
<div class="content-wthree">
<h2>Register Now</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. </p>
<?php echo $msg; ?>
<form action="" method="post">
<input type="text" class="name" name="name" placeholder="Enter Your Name" value="<?php if (isset($_POST['submit'])) { echo $name; } ?>" required>
<input type="email" class="email" name="email" placeholder="Enter Your Email" value="<?php if (isset($_POST['submit'])) { echo $email; } ?>" required>
<input type="password" class="password" name="password" placeholder="Enter Your Password" required>
<input type="password" class="confirm-password" name="confirm-password" placeholder="Enter Your Confirm Password" required>
<button name="submit" class="btn" type="submit">Register</button>
</form>
<div class="social-icons">
<p>Have an account! <a href="guest_login.php">Login</a>.</p>
</div>
</div>
</div>
</div>
<!-- //form -->
</div>
</section>
<!-- //form section start -->
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function (c) {
$('.alert-close').on('click', function (c) {
$('.main-mockup').fadeOut('slow', function (c) {
$('.main-mockup').remove();
});
});
});
</script>
</body>
</html>
Somebody told me to replace my port and host to TLS 587, still it wont work. can anybody help me with this problem? thanks.
答案1
得分: 1
这确实有点混乱,如果您按照 PHPMailer 提供的 Gmail 示例 进行操作,您可能会更幸运。PHPMailer 支持 从 Host
属性设置 SMTPSecure
模式和端口号,但更清晰的做法是显式设置这些属性。您遇到问题是因为设置冲突。请改为以下方式:
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'tk23@gmail.com';
$mail->Password = '12345678';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
故障排除指南解释了各种不同的加密选项,以及它们适用的端口。现在,SMTPS 在端口 465 上是首选选项。
请注意,您仍然可能会遇到问题,因为 Gmail 不再允许简单的基于密码的身份验证;您需要研究 切换到 XOAUTH2。
英文:
This is indeed a bit of a mess, and if you had followed the gmail example provided with PHPMailer you'd have had more luck. PHPMailer does support setting the SMTPSecure
mode and the port number from the Host
property, however, it's much clearer to set those properties explicitly. You're running into a problem with it because you have conflicting settings. Do this instead:
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'tk23@gmail.com';
$mail->Password = '12345678';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
The troubleshooting guide explains all about the various different encryption options, and which ports they work on. These days, SMTPS on port 465 is the preferred option.
Note that you are still likely to run into problems because gmail no longer allows simple password-based auth; you need to look into switching to XOAUTH2.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论