我构建了一个联系表单,我想使用phpmailer发送电子邮件。我想使用gmail作为主机。我输入了正确的用户名和密码,但我不断收到错误消息。我也在gmail帐户中访问了不太安全的内容。我不知道出了什么问题。
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "truelabeltestingemail@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "*********";
//Set who the message is to be sent from
$mail->setFrom('truelabeltestingemail@gmail.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('noreplyt@truelabel.com', 'True Label');
//Set who the message is to be sent to
$mail->addAddress('truelabeltestingemail@gmail.com', 'Contact Form');
//Set the subject line
$mail->Subject = 'True Label Contact Form';
//Replace the plain text body with one created manually
$mail->Body = 'Message!!';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
这是我不断收到的错误消息
SMTP -> FROM SERVER:220 mx.google.com ESMTP z195sm7240232iod.33 - gsmtp
SMTP -> FROM SERVER: 250-mx.google.com at your service, [67.211.175.4] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
SMTP -> FROM SERVER:220 2.0.0 Ready to start TLS
SMTP -> FROM SERVER: 250-mx.google.com at your service, [67.211.175.4] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
SMTP -> ERROR: Password not accepted from server: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 z195sm7240232iod.33 - gsmtp
SMTP -> FROM SERVER:250 2.1.5 Flushed z195sm7240232iod.33 - gsmtp
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.