gpt4 book ai didi

使用带有 Gmail 的 smtp 的 Phpmailer 无法正常工作 - 连接超时

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:00:58 24 4
gpt4 key购买 nike

我查看了以下链接:

phpmailer send gmail smtp timeout

send email using Gmail SMTP server through PHP Mailer

http://uly.me/phpmailer-and-gmail-smtp/

...并尝试为自己实现这些组合,但是...大多数时候它会发送此消息...

Message could not be sent.

Mailer Error: SMTP connect() failed.

然而,当我在“tls”和“ssl”之间进行试验时,有一次它发送了这个......

SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Message could not be sent.

Mailer Error: SMTP connect() failed.

附上我的代码...我是不是遗漏了什么?我询问了 Web 托管服务,他们是否阻止并给了他们我的代码模板 - 他们说服务器允许连接到 Gmail 的 SMTP。

    require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail -> IsSMTP();
$mail -> SMTPDebug = 2;
$mail -> SMTPAuth = 'true';
$mail -> SMTPSecure = 'tls';
$mail -> SMTPKeepAlive = true;
$mail -> Host = 'smtp.gmail.com';
$mail -> Port = 587;
$mail -> IsHTML(true);

$mail -> Username = "myemail@gmail.com";
$mail -> Password = "mypassword";
$mail -> SingleTo = true;

$to = xxx;
$from = xxx;
$fromname = xxx;
$subject = xxx;
$message = xxx
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";

$mail -> From = $from;
$mail -> FromName = $fromname;
$mail -> AddAddress($to);

$mail -> Subject = $subject;
$mail -> Body = $message;

if(!$mail -> Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail-> ErrorInfo;
exit;
}

最佳答案

我深入研究了它。使用 php 自带的 fsocketopen 来测试连接并消除大部分潜在问题。用这个写一个简单的 php 页面:

    $host = "smtp.gmail.com";
$port = "587";
$checkconn = fsockopen($host, $port, $errno, $errstr, 5);
if(!$checkconn){
echo "($errno) $errstr";
} else {
echo 'ok';
}

你应该返回“ok”。如果不是,您知道您遇到了与 Phpmailer 无关的连接问题。如果是这样的话,它可能是托管公司。如果不是,那么您的本地主机和托管公司之间的区别可能很简单,例如不同版本的 php。

我怀疑这个脚本不会建立连接

关于使用带有 Gmail 的 smtp 的 Phpmailer 无法正常工作 - 连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19216335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com