Issues

ZF-12489: Zend_Mail_Protocol_Smtp must use context to bind specific IP.

Issue Type: Improvement Created: 2012-12-20T13:27:06.000+0000 Last Updated: 2013-04-05T16:07:05.000+0000 Status: Closed Fix version(s): Reporter: Federico Gon (federico.gon@dattatec.com) Assignee: Dolf Schimmel (Freeaqingme) (freak) Tags: - Zend_Mail

  • zend_mail
  • zend_mail_protocol_smtp

Related issues: Attachments:

Description

If you have a more than 1 IP in your server, you can't really know which IP Zend_Mail_Protocol_Smtp will use.

Solution: Use stream_context_set_params to set "bindto" param

application.ini:

<pre class="highlight">
resources.mail.transport.bindto    = "xxx.xxx.xxx.xxx" ; Setting the IP or the port to 0 will let the system choose the IP and/or port. (<a href="http://php.net/manual/en/context.socket.php">http://php.net/manual/en/context.socket.php</a>)

Code: class: Zend_Mail_Protocol_Smtp

<pre class="highlight">

// new property
protected $_bindto = null;

    public function __construct($host = '127.0.0.1', $port = null, array $config = array())
    {
        ...

        if(isset($config['bindto']) && !empty($config['bindto']))
        {
            $this->_bindto = $config['bindto'];
        }
        else
        {
            $this->_bindto = gethostbyname(gethostname());
        }

        parent::__construct($host, $port);
    }


    protected function _connect($remote)
    {
        $errorNum = 0;
        $errorStr = '';

        // open connection

/* Here use de new property to build a context */
        $context = null;
        if(!is_null($this->_bindto))
        {
            $opts = array('socket' => array('bindto' => $this->_bindto));
            $context = stream_context_create($opts);
        }

        $this->_socket = @stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $context);

        if ($this->_socket === false) {
            if ($errorNum == 0) {
                $errorStr = 'Could not open socket';
            }
            /**
             * @see Zend_Mail_Protocol_Exception
             */
            require_once 'Zend/Mail/Protocol/Exception.php';
            throw new Zend_Mail_Protocol_Exception($errorStr);
        }

        if (($result = $this->_setStreamTimeout(self::TIMEOUT_CONNECTION)) === false) {
            /**
             * @see Zend_Mail_Protocol_Exception
             */
            require_once 'Zend/Mail/Protocol/Exception.php';
            throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
        }

        return $result;
    }

Comments

Posted by Ralph Schindler (ralph) on 2013-04-05T16:07:05.000+0000

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf1/issues/43

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts