From:  pear-qa@lists.php.net ("eso@mail.ru")
Date:  23 Mar 2017 00:00:10 Hong Kong Time
Newsgroup:  news.php.net/php.pear.bugs
Subject:  

[PEAR-BUG] Bug #20513 [Com]: Mail_smtp::send() doesn't close socket for smtp connection

NNTP-Posting-Host:  null

Edit report at https://pear.php.net/bugs/bug.php?id=20513&edit=1

 ID:               20513
 Comment by:       eso at mail dot ru
 Reported By:      eso at mail dot ru
 Summary:          Mail_smtp::send() doesn't close socket for smtp
                    connection
 Status:           Open
 Type:             Bug
 Package:          Mail
 Package Version:  1.2.0
 PHP Version:      5.4.33
 Roadmap Versions: 
 New Comment:

I don't know why the patch became unavailable. It definitely was here (2
years ago).
I've managed to find the file on my old broken HDD. Not sure if it is
still applicable.


Previous Comments:
------------------------------------------------------------------------

[2017-03-22 11:54:58] e79ene

Added #patch bug:20513;patch:Fix-Bug-20513_again;revision:1490198098;.

------------------------------------------------------------------------

[2017-03-22 05:42:29] jkingsnorth

Confirmed the issue. Unfortunately there seems to be a patch attached to
this that doesn't exist any more?

------------------------------------------------------------------------

[2015-03-09 11:20:09] e79ene

Added #patch bug:20513;patch:Fix-Bug-20513;revision:1425900009;.

------------------------------------------------------------------------

[2015-03-08 15:33:50] e79ene

Description:
------------
Mail::send() for smtp backend doesn't disconnect Net_Socket in 
case of error.

I use Mail::send() with smtp backend to send mails from php 
script on my FreeBSD server.
It works ok while there are no errors.
But if there's an error (E.g. SMTP authentication failure) 
Mail::send() finishes and leaves tcp connection with smtp server 
open.
My script tried to send thousands of mails and thus Mail::send() 
opened thousands of socket connections and hit the system limit 
of open files. As a result other services (such as database) 
became unavailable too.

AFAIU the reason of the situation is in the source:
https://github.com/pear/Mail/blob/trunk/Mail/smtp.php

Mail_smtp::send() calls disconnect() in the end of function body 
in case of success. However the function contains plenty of error 
checks which perform return without call to disconnect().

Test script:
---------------
 'test@example.com',
    'To' => 'test@example.com',
    'Subject' => 'Text message subject',
);

$mailer = Mail::factory('smtp', array(
    'host' => 'smtp.gmail.com',
    'auth' => true,
    'username' => 'fake@gmail.com',
    'password' => 'the_password',
));

if (PEAR::isError($mailer))
    die($mailer);

$res = $mailer->send($headers['to'], $headers, 'Test messge body');

if ($res === true)
    echo "Mail sent.\n";
else
    echo "send() failed.\n";

if (isset($mailer->_smtp)) {
    echo "Socket status:\n";
    print_r($mailer->_smtp->_socket->getStatus());
} else
    echo "Connection closed.\n";
?>

Expected result:
----------------
send() failed.
Connection closed.


Actual result:
--------------
send() failed.
Socket status:
Array
(
    [stream_type] => tcp_socket
    [mode] => r+
    [unread_bytes] => 0
    [seekable] =>
    [timed_out] =>
    [blocked] => 1
    [eof] =>
)

------------------------------------------------------------------------


-- 
Edit this bug report at https://pear.php.net/bugs/bug.php?id=20513&edit=1