PHPmailer 不使用 gmail 的 SMTP 从本地主机发送电子邮件

PHPmailer not sending emails from localhost using gmail's SMTP

您好,我正在尝试使用 PHPmailer 和 SMTP 方法发送电子邮件,它在停止发送电子邮件之前工作正常..

我尝试调试,我得到的错误是这个

2021-01-14 14:46:47 Connection: opening to smtp.gmail.com:587, timeout=300, options=array()
2021-01-14 14:46:47 Connection: opened
2021-01-14 14:46:48 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP n16sm10096874wrj.26 - gsmtp
2021-01-14 14:46:48 CLIENT -> SERVER: EHLO localhost
2021-01-14 14:46:48 SERVER -> CLIENT: 550 Transaction failed; HELO/EHLO argument not accepted
2021-01-14 14:46:48 SMTP ERROR: EHLO command failed: 550 Transaction failed; HELO/EHLO argument not accepted
2021-01-14 14:46:48 CLIENT -> SERVER: HELO localhost
2021-01-14 14:46:48 SERVER -> CLIENT: 421 Local Error, closing transmission channel
2021-01-14 14:46:48 SMTP ERROR: HELO command failed: 421 Local Error, closing transmission channel
2021-01-14 14:46:48 CLIENT -> SERVER: STARTTLS
2021-01-14 14:46:48 SERVER -> CLIENT:
2021-01-14 14:46:48 SMTP ERROR: STARTTLS command failed:
SMTP Error: Could not connect to SMTP host.
2021-01-14 14:46:48 SMTP NOTICE: EOF caught while checking if connected
2021-01-14 14:46:48 Connection: closed
SMTP Error: Could not connect to SMTP host.

从错误看来,从本地主机发送到 Gmail 的 EHLO 参数没有被接受,但我没有办法或想法来解决这个问题

这是我的 PHPmailer 代码,如果有帮助的话。

require(ROOT_PATH . 'phpmailer/src/Exception.php'); 

require(ROOT_PATH . 'phpmailer/src/PHPMailer.php'); 

require(ROOT_PATH . 'phpmailer/src/SMTP.php');

// Instantiation and passing `true` enables exceptions

$mail = new PHPMailer(true);


//Server settings

$mail->SMTPDebug = SMTP::DEBUG_SERVER;   // Enable verbose debug output


$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Username = "browynlouis2@gmail.com";
$mail->Password = '081';
 


//Recipients

$mail->setFrom('browynlouis2@gmail.com', 'Sativa');
    $mail->addAddress($email);               // Name is optional
    $mail->addReplyTo('browynlouis2@gmail.com', 'Sativa Merchandise');


// Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Sativa Merch Newsletter';
    $mail->AddEmbeddedImage('admin/assets/img/logo.jpg', 'logo');
    $mail->Body    = '<div style="background:whitesmoke;padding:10px"><div style="background:white;padding:5px;"><div><img src="cid:logo"><h2 style="text-align:center">Sativa Merchandise</h2></div><div style="padding:10px;padding-top:5px; text-align:center"><p style="color:grey;">You are getting the mail because you just recently subscribed to Sativa Merchandise newsletter. You can continue shopping with us below, thanks for your patronage.</p> <br><br><a href="<?php echo BASE_URL; ?>" style="background-color:black; padding:10px; color:white">Continue Shopping</a><br><br></div></div></div>';
    $mail->AltBody = 'You are getting the mail because you just recently subscribed to Sativa Merchandise newsletter. You can continue shopping with us below, thanks for your patronage.';


if (!$mail->send()) {


$subscribe = "Unable to Subscribe you to our newsletter.";

} else {


$subscribe = " You have successfully subscribed to our newsletter. Thank You. ";

}

正在抱怨这个:

2021-01-14 14:46:48 CLIENT -> SERVER: EHLO localhost

localhost 不是外部可路由的名称,因此他们无法对其进行查找以确定它是否与 SMTP 连接来自的 IP 相匹配。 PHPMailer 试图自动计算出这个名称,但有时它不能。在这些情况下,您可以通过 Hostname 属性 手动设置它,如下所示:

$mail->Hostname = 'my.host.example.com';

将此设置为您服务器的真实主机名。下一步是确保您的 DNS 解析器可以向前和向后工作,因此如果您使用以下命令查找您的名字:

host my.host.example.com

您将获得一个 IP 地址,如果您这样做:

host <ip address>

您应该以开始时使用的主机名结束。