如果使用SMTP客户端或者PHPMailer等组件通过gmail发送邮件,明明用户名和密码正确,但是报错误:
⚠
SMTP Error: Could not authenticate.
这是因为默认情况下,google为了安全起见,禁止第三方应用使用原始密码登录账户,你必须开启”安全性较低的应用的访问权限“选项。
解决方法,使用"应用专用密码",步骤如下:
- 首先必须把账户启用两步验证,登录你的账户,找到【管理您的谷歌账号】,【您的 Google 账号登录选项】,【两步验证】,启用即可。
- 两部验证完成后,重新打开【两步验证】,找到页面底部的【应用专用密码】
- 输入你的应用名称,然后创建一个应用密码即可,复制密码下来,然后到第三方应用如 PHPMailer 中粘贴密码即可(记住要删除中间的空格!)
此后就可以正常登录了。
一个简单的例子:
<?php
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$recaptcha_secret_key = '替换成你的 reCAPTCHA server 端密钥';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$recaptcha_response = $_POST['g-recaptcha-response'];
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_data = [
'secret' => $recaptcha_secret_key,
'response' => $recaptcha_response,
];
$recaptcha_options = [
'http' => [
'method' => 'POST',
'content' => http_build_query($recaptcha_data),
],
];
$recaptcha_context = stream_context_create($recaptcha_options);
$recaptcha_result = file_get_contents($recaptcha_url, false, $recaptcha_context);
$recaptcha_data = json_decode($recaptcha_result, true);
$send_success = false;
if (!$recaptcha_data['success']) {
$error_message = 'reCAPTCHA verification failed.';
} else {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'username@gmail.com';
$mail->Password = 'gmail app password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom($email, $name);
$mail->addAddress('收件人邮箱地址');
$mail->isHTML(false);
$mail->Subject = 'Contact Form Submission';
$mail->Body = "Name: $name\nEmail: $email\n\n$message";
$mail->send();
$send_success = true;
$error_message = "Send success";
} catch (Exception $e) {
error_log($e);
$error_message = "Send failed";
}
} }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<style>
body {
font-size: 14px;
}
form {
max-width: 600px;
margin: 0 auto;
}
label {
display: block;
margin-bottom: 5px;
}
input, textarea {
width: 100%;
padding: 8px;
margin-bottom: 10px;
box-sizing: border-box;
}
button {
padding: 10px;
background-color:
color:
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<form method="post" action="">
<label for="name">Your name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Your e-mail:</label>
<input type="email" id="email" name="email" required>
<label for="message">Your message:</label>
<textarea id="message" name="message" rows="10" required></textarea>
<div class="g-recaptcha" data-sitekey="替换成你的 reCAPTCHA 客户端 key"></div>
<button type="submit">Send</button>
<?php if ($_SERVER["REQUEST_METHOD"] == "POST"): ?>
<?php if ($send_success): ?>
<span style="color: green;"><?php echo $error_message; ?></span>
<?php else: ?>
<span style="color: red;"><?php echo $error_message; ?></span>
<?php endif; ?>
<?php endif; ?>
</form>
</body>
</html>
页面效果: