我使用的是phpmailer最新版本。我使用以下代码:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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 = 'sultan.ahmed.sagor@gmail.com'; // SMTP username
$mail->Password = 'simplePass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SMTPDebug=2;
$mail->setFrom('sultan.ahmed.sagor@gmail.com', 'Mailer');
$mail->addAddress('zakir@era.com.bd', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
但是当我运行这段代码时,我给出了以下输出:
2017-02-09 11:20:34 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP w70sm17914821wrc.47 - gsmtp
2017-02-09 11:20:34 CLIENT -> SERVER: EHLO localhost
2017-02-09 11:20:34 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [50.118.143.87] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHU*KING 250 SMTPUTF8
2017-02-09 11:20:34 CLIENT -> SERVER: STARTTLS
2017-02-09 11:20:37 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2017-02-09 11:20:38 CLIENT -> SERVER: EHLO localhost
2017-02-09 11:20:39 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [50.118.143.87] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
2017-02-09 11:20:39 CLIENT -> SERVER: AUTH LOGIN
2017-02-09 11:20:39 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2017-02-09 11:20:39 CLIENT -> SERVER: xxx==
2017-02-09 11:20:40 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2017-02-09 11:20:40 CLIENT -> SERVER: xxx==
2017-02-09 11:20:40 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials w70sm17914821wrc.47 - gsmtp
2017-02-09 11:20:40 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials w70sm17914821wrc.47 - gsmtp
2017-02-09 11:20:40 SMTP Error: Could not authenticate.
2017-02-09 11:20:40 CLIENT -> SERVER: QUIT
2017-02-09 11:20:41 SERVER -> CLIENT: 221 2.0.0 closing connection w70sm17914821wrc.47 - gsmtp
2017-02-09 11:20:41 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
电子邮件未发送。我如何使用phpmailer发送电子邮件?
答案 0 :(得分:0)
这可以解决您的问题:
$mail->AuthType = 'LOGIN';
https://www.emailarchitect.net/easendmail/sdk/html/authtype.htm