Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在尝试发送电子邮件时,phpmailer会出现此错误?_Php_Email_Smtp_Gmail_Phpmailer - Fatal编程技术网

为什么在尝试发送电子邮件时,phpmailer会出现此错误?

为什么在尝试发送电子邮件时,phpmailer会出现此错误?,php,email,smtp,gmail,phpmailer,Php,Email,Smtp,Gmail,Phpmailer,错误: 我的代码: 2021-03-19 15:17:50 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP t24sm3903607qto.23 - gsmtp 2021-03-19 15:17:50 CLIENT -> SERVER: EHLO localhost 2021-03-19 15:17:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [24.233.165.10

错误:

我的代码:

2021-03-19 15:17:50 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP t24sm3903607qto.23 - gsmtp
2021-03-19 15:17:50 CLIENT -> SERVER: EHLO localhost
2021-03-19 15:17:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [24.233.165.10]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-03-19 15:17:50 CLIENT -> SERVER: STARTTLS
2021-03-19 15:17:50 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2021-03-19 15:17:50 CLIENT -> SERVER: EHLO localhost
2021-03-19 15:17:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [24.233.165.10]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-03-19 15:17:50 CLIENT -> SERVER: AUTH LOGIN
2021-03-19 15:17:50 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-03-19 15:17:50 CLIENT -> SERVER: [credentials hidden]
2021-03-19 15:17:50 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-03-19 15:17:50 CLIENT -> SERVER: [credentials hidden]
2021-03-19 15:17:51 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials t24sm3903607qto.23 - gsmtp
2021-03-19 15:17:51 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 t24sm3903607qto.23 - gsmtp
SMTP Error: Could not authenticate.
2021-03-19 15:17:51 CLIENT -> SERVER: QUIT
2021-03-19 15:17:51 SERVER -> CLIENT: 221 2.0.0 closing connection t24sm3903607qto.23 - gsmtp
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
就是这个打字错误:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
 
require 'PHPMailer/PHPMailer/src/Exception.php';
require 'PHPMailer/PHPMailer/src/PHPMailer.php';
require 'PHPMailer/PHPMailer/src/SMTP.php';
 
// Instantiation and passing [ICODE]true[/ICODE] enables exceptions
$mail = new PHPMailer(true);
 
try {
    //Server settings
    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'myemail@gmail.com';                     // SMTP username
    $mail->Password   = '*************';                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
    $mail->Port       = 587;                                    // TCP port to connect to
 
    //Recipients
    $mail->setFrom('myemail.com', 'Mailer');
    $mail->addAddress('myemail@gmail.com', 'Recipients');     // Add a recipient
    //$mail->addAddress('');
    $mail->addReplyTo('myemai@gmail.com', 'Mailer');
    //$mail->addCC('');
    //$mail->addBCC('');
 
    // Attachments
    //$mail->addAttachment('');         // Add attachments
    //$mail->addAttachment('');
 
    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
 
    $mail->send();
    echo 'Message has been sent';
 
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
这应该是:

$mail->Host       = 'smpt.gmail.com';

错误消息本身表示找不到主机名,这是完全可以预料的,因为它是错误的。

您多次编辑了您的问题,每次都更改了它的含义,这在以后生成注释音时应该避免。下次请阅读错误信息,并在询问之前修复它们。你做错了什么(按外貌顺序)

  • 不要只为将来使用没有值的赋值方法,如
    $mail->addAddress(“”)
    $mail->addCC(“”)
    等,它将导致错误
  • 使用正确的SMTP服务器地址
  • 使用正确的凭证。当然,登录名和密码区分大小写
  • 如果您的Gmail帐户仍然存在凭据错误问题,请启用“不太安全的应用程序”。可以在屏幕截图所示的“安全”选项卡上完成

  • 什么
    $mail->addAddress(“”)是否在您的代码中?只允许更多收件人我相信更多没有电子邮件地址和姓名的收件人?感觉如何?好的,我删除了它,很可能其他没有值的添加也会导致类似的问题,请先清理它。不要使用本案例中不应使用的方法。已启用不太安全的应用程序。谢谢提示。下次我会删除空的内容,然后发布我真正的问题,但我仍然收到一个错误我用我的凭据尝试了你的代码,邮件到达(在设置不太安全的东西之前没有)TBH没有更多的想法。好的。在你打开不太安全的应用程序后,你等了多久它才开始工作
    
    $mail->Host       = 'smtp.gmail.com';