【问题标题】:SMTP Error: Could not authenticate. while sending email in phpSMTP 错误:无法验证。在用php发送电子邮件时
【发布时间】: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 工作正常,收到了我测试过的另一个帐户的电子邮件

标签: php phpmailer


【解决方案1】:

始终查看您面前的信息。

阅读错误消息中链接的故障排除指南 - 它准确地告诉您如何处理这个确切的问题 - 您很可能需要执行“显示验证码解锁”步骤。

这不仅仅是“不太安全的应用程序”设置(您可以通过实施 XOAUTH2 来避免),而且 gmail 在通过新机制(即您的脚本)登录时会施加额外的身份验证步骤,这在他们在回复中提供的链接。

【讨论】:

    猜你喜欢
    • 2012-09-29
    • 2016-08-24
    • 2013-11-24
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多