GMAIL SMTP连接在服务器上失败,但在本地可用。

huangapple go评论48阅读模式
英文:

GMAIL SMTP Connect Fails On Server But Works On Local

问题

I am using the Chilkat library to send email. It works perfectly on my local server/PC, but on the live server, it fails to connect to SMTP.

I've tried both SSL and TLS ports (465/587), but both fail with the same error message.

Here is the relevant part of the code to connect to Google SMTP server:

// init
$smtp = new CkMailMan();
$smtp->put_VerboseLogging(true);
$smtp->put_SmtpHost('smtp.gmail.com');
$smtp->put_SmtpPort(465);
$smtp->put_SmtpSsl(true);

// connect
if (!$smtp->SmtpConnect()) {
    file_put_contents('./log/smtp_error_' . $GLOBALS['unixTime'] . '.log', $smtp->lastErrorXml());
    unset($smtp);
    echo '!failed!';
    exit();
}

And here is the log file:

<ChilkatLog>
  <SmtpConnect ms="30066">
    <DllDate>Jun 25 2022</DllDate>
    <ChilkatVersion>9.5.0.91</ChilkatVersion>
    <!-- ... (log details) ... -->
    <error>Failed.</error>
  </SmtpConnect>
</ChilkatLog>

I've consulted with server tech, and they confirmed that all ports (465, 587, 25) are open. However, it's not working on the Linux server with Plex.

英文:

I am using chilkat library to send email, it works perfectly on my local server/pc, but on live server it fails on smtp connect.

I tried both SSL and tls port.. (465/587) but both fails with same kind of error message..

following is the part of code to connect to google smtp server

//init
$smtp = new CkMailMan();
$smtp-&gt;put_VerboseLogging(true);
$smtp-&gt;put_SmtpHost(&#39;smtp.gmail.com&#39;);
$smtp-&gt;put_SmtpPort(465);
$smtp-&gt;put_SmtpSsl(true);
//connect
if(!$smtp-&gt;SmtpConnect())
{
    file_put_contents(&#39;./log/smtp_error_&#39; . $GLOBALS[&#39;unixTime&#39;] . &#39;.log&#39;, $smtp-&gt;lastErrorXml());
    unset($smtp);
    echo &#39;!failed!&#39;;
    exit();
}

and here is the log file:

  &lt;ChilkatLog&gt;
  &lt;SmtpConnect ms=&quot;30066&quot;&gt;
    &lt;DllDate&gt;Jun 25 2022&lt;/DllDate&gt;
    &lt;ChilkatVersion&gt;9.5.0.91&lt;/ChilkatVersion&gt;
    &lt;UnlockPrefix&gt;NONE&lt;/UnlockPrefix&gt;
    &lt;Architecture&gt;Little Endian; 64-bit&lt;/Architecture&gt;
    &lt;Language&gt;Linux PHP&lt;/Language&gt;
    &lt;VerboseLogging&gt;1&lt;/VerboseLogging&gt;
    &lt;ensureSmtpConnection ms=&quot;30066&quot;&gt;
      &lt;smtpParams&gt;
        &lt;SmtpHost&gt;smtp.gmail.com&lt;/SmtpHost&gt;
        &lt;SmtpPort&gt;465&lt;/SmtpPort&gt;
        &lt;SmtpUsername&gt;&lt;/SmtpUsername&gt;
        &lt;SmtpSsl&gt;1&lt;/SmtpSsl&gt;
        &lt;StartTLS&gt;0&lt;/StartTLS&gt;
      &lt;/smtpParams&gt;
      &lt;smtpConnect ms=&quot;30066&quot;&gt;
        &lt;smtpHostname&gt;smtp.gmail.com&lt;/smtpHostname&gt;
        &lt;smtpPort&gt;465&lt;/smtpPort&gt;
        &lt;connectionIsReady&gt;
          &lt;info&gt;SMTP host changed.&lt;/info&gt;
          &lt;info&gt;SMTP port changed.&lt;/info&gt;
          &lt;info&gt;Need new SMTP connection, something changed!&lt;/info&gt;
        &lt;/connectionIsReady&gt;
        &lt;smtpSocketConnect ms=&quot;30066&quot;&gt;
          &lt;socket2Connect ms=&quot;30066&quot;&gt;
            &lt;connect2 ms=&quot;30066&quot;&gt;
              &lt;hostname&gt;smtp.gmail.com&lt;/hostname&gt;
              &lt;port&gt;465&lt;/port&gt;
              &lt;ssl&gt;True&lt;/ssl&gt;
              &lt;connectImplicitSsl ms=&quot;30066&quot;&gt;
                &lt;info&gt;Clearing TLS client certificates.&lt;/info&gt;
                &lt;connectSocket ms=&quot;30066&quot;&gt;
                  &lt;domainOrIpAddress&gt;smtp.gmail.com&lt;/domainOrIpAddress&gt;
                  &lt;port&gt;465&lt;/port&gt;
                  &lt;connectTimeoutMs&gt;30000&lt;/connectTimeoutMs&gt;
                  &lt;connect_ipv6_or_ipv4 ms=&quot;30066&quot;&gt;
                    &lt;info&gt;Single-threaded domain to IP address resolution&lt;/info&gt;
                    &lt;info&gt;connecting to IPV4 address...&lt;/info&gt;
                    &lt;ipAddress&gt;172.253.115.108&lt;/ipAddress&gt;
                    &lt;createSocket&gt;
                      &lt;info&gt;Setting SO_SNDBUF size&lt;/info&gt;
                      &lt;sendBufSize&gt;262144&lt;/sendBufSize&gt;
                      &lt;info&gt;Setting SO_RCVBUF size&lt;/info&gt;
                      &lt;recvBufSize&gt;4194304&lt;/recvBufSize&gt;
                    &lt;/createSocket&gt;
                    &lt;connect ms=&quot;30065&quot;&gt;
                      &lt;info&gt;Waiting for the connect to complete...&lt;/info&gt;
                      &lt;connectTimeoutMs&gt;30000&lt;/connectTimeoutMs&gt;
                      &lt;error&gt;timeout waiting for connect to complete;&lt;/error&gt;
                      &lt;timeoutMs&gt;30000&lt;/timeoutMs&gt;
                      &lt;failedWaitToConnect&gt;Socket operation timeout.&lt;/failedWaitToConnect&gt;
                    &lt;/connect&gt;
                  &lt;/connect_ipv6_or_ipv4&gt;
                &lt;/connectSocket&gt;
                &lt;error&gt;pmConnect failed.&lt;/error&gt;
              &lt;/connectImplicitSsl&gt;
              &lt;connectFailReason&gt;6&lt;/connectFailReason&gt;
              &lt;ConnectFailReason&gt;Timeout&lt;/ConnectFailReason&gt;
              &lt;error&gt;A few possible causes for a connection timeout are:&lt;/error&gt;
              &lt;error&gt;- The remote host is not reachable (perhaps a firewall is blocking the connection on the remote end).&lt;/error&gt;
              &lt;error&gt;- Your ISP is blocking the outbound connection (this is common for SMTP port 25).&lt;/error&gt;
              &lt;error&gt;- If your timeout is too short, then maybe the server is just too slow to accept the connection.&lt;/error&gt;
            &lt;/connect2&gt;
          &lt;/socket2Connect&gt;
          &lt;error&gt;Failed to connect to SMTP server..&lt;/error&gt;
        &lt;/smtpSocketConnect&gt;
      &lt;/smtpConnect&gt;
    &lt;/ensureSmtpConnection&gt;
    &lt;error&gt;Failed.&lt;/error&gt;
  &lt;/SmtpConnect&gt;
&lt;/ChilkatLog&gt;

i have consulted with server tech and they said, all the port 465,587,25 all is open.

so, i am so confused why it is not working?

info: it's linux server with plex

答案1

得分: 1

无论你的服务器技术如何说,你都应该验证端口是否开放,并至少排除一个可能性。

以下是一个简单的脚本,尝试通过各种端口连接,并为每个端口返回成功或错误消息。

<?php
$servers = array(
    array("ssl://www.google.com", 443),
    array("smtp.gmail.com", 465),
    array("smtp.gmail.com", 587),
);

foreach ($servers as $server) {
    list($server, $port) = $server;
    echo "<h1>尝试连接到 <tt>$server:$port</tt></h1>\n";
    flush();
    $socket = fsockopen($server, $port, $errno, $errstr, 10);
    if(!$socket) {
      echo "<p>错误:$server:$port - $errstr ($errno)</p>\n";
    } else {
      echo "<p>成功:$server:$port - 正常</p>\n";
    }
    flush();
}
?>
英文:

Regardless of what your server tech says, you should validate that the ports are open and eliminate at least one of the possibilities.

Here's a simple script that attempts to connect through various ports, and returns a SUCCESS or ERROR message for each.

&lt;?php
$servers = array(
    array(&quot;ssl://www.google.com&quot;, 443),
    array(&quot;smtp.gmail.com&quot;, 465),
    array(&quot;smtp.gmail.com&quot;, 587),
);

foreach ($servers as $server) {
    list($server, $port) = $server;
    echo &quot;&lt;h1&gt;Attempting connect to &lt;tt&gt;$server:$port&lt;/tt&gt;&lt;/h1&gt;\n&quot;;
    flush();
    $socket = fsockopen($server, $port, $errno, $errstr, 10);
    if(!$socket) {
      echo &quot;&lt;p&gt;ERROR: $server:$portsmtp - $errstr ($errno)&lt;/p&gt;\n&quot;;
    } else {
      echo &quot;&lt;p&gt;SUCCESS: $server:$port - ok&lt;/p&gt;\n&quot;;
    }
    flush();
}

huangapple
  • 本文由 发表于 2023年5月26日 07:50:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76336862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定