【发布时间】:2020-01-25 09:20:56
【问题描述】:
您好,我正在为我的网站使用 PHPMailer 发送电子邮件,但收到错误为 SMTP 错误:无法验证。
这是我遇到的错误。我已正确输入密码并将允许不太安全的应用程序也设置为“开启,但仍然出现相同的错误。
020-01-25 09:11:24 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 oCoMr_x13f0BsQ-UMqe0zgPQOcVX9A7SY4fBk-WoLrRIJfT6uwspq7QpzlUjE8srm9esG534-5.7.14 73T38rlSCuGdkDRsjhkB3YQxKz3V8njuaclMAfqHskMAU3eX_1LYPH80oO9ZLkAl>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 i81sm528818ywe.82 - gsmtp
SMTP Error: Could not authenticate.
2020-01-25 09:11:24 CLIENT -> SERVER: QUIT
这是我用来发送电子邮件的代码
<?php ob_start();
if(isset($_POST['submit_contact']))
{
require 'PHPMailer/PHPMailerAutoload.php';
$address = 'testing1@gmail.com';
$name=$_POST['name'];
$email =$_POST['email'];
$subject = $_POST['subject'];
$textMessage = $_POST['message'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug =1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->CharSet = "UTF-8";
$mail->IsHTML(true);
$mail->Username = "testing@gmail.com";
$mail->Password = "PASSword1@3";
$message = array();
$message[] = 'Name : '.trim($name).' ';
$message[] = 'Email : '.trim($email).' ';
$message[] = 'Comment : '.trim($textMessage).' ';
$message = implode(',', $message);
$mail->SetFrom($email);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress($address);
if (!$mail->Send()) {
$msg = "Error while sending email";
$msgclass = 'bg-danger';
} else {
$msg = 'Thank you for contacting us.Will get back to you soon.....';
$msgclass = 'bg-success';
header("Location: contact.php");
}
}
?>
这是我写的 HTML 代码
<form class="form-horizontal" action="contactus" method="post" role="form" enctype="multipart/form-data">
<?php if(isset( $msg)) {?>
<div class="<?php echo $msgclass; ?>" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<div class="form-group">
<input type="text" name="name" id="name" class="form-control input-field" placeholder="Name" required="">
<input type="email" name="email" id="email" class="form-control input-field" placeholder="Email ID" required="">
<input type="text" name="subject" id="subject" class="form-control input-field" placeholder="Subject" required="">
</div>
<textarea name="message" id="message" class="textarea-field form-control" rows="4" placeholder="Enter your message" required=""></textarea>
<button type="submit" id="btnSend" value="Submit" class="btn center-block" name="submit_contact" >Send message</button>
<!-- Contact results -->
</form>
【问题讨论】:
-
您是否尝试过使用 gmail 帐户发送电子邮件?
-
@DhavalGol 是的,我只使用了 from 和 to 两个 gmail 的地址,但没有用
-
您的代码看起来不错。如果用户名和密码正确,它应该可以工作。与github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps比较
-
@user9131066 使用 smtp 用户名和密码登录 gmail 并发送邮件检查它是否正常工作?
-
@DhavalGol 工作正常,收到了我测试过的另一个帐户的电子邮件