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

时间:2021-01-14 15:00:55

标签: php phpmailer

您好,我正在尝试使用 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. ";

}

1 个答案:

答案 0 :(得分:2)

它在抱怨这个:

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>

你应该以你开始的主机名结束。