如何编辑CodeIgniter本机SMTP连接功能以允许通过代理连接

时间:2014-09-11 19:01:31

标签: php codeigniter proxy smtp

我正在使用Codeigniter框架在我的本地计算机上创建一个项目。我通过CI中的本机Email.php库连接到外部SMTP服务,这在我的删除开发服务器上工作正常。

不幸的是,由于公司代理,我在本地主机上遇到连接问题。它也需要凭据。

到目前为止,我已在Email.php中隔离了以下 native CI函数中的连接问题:

/**
 * SMTP Connect
 *
 * @access  protected
 * @param   string
 * @return  string
 */
protected function _smtp_connect()
{
    $ssl = NULL;
    if ($this->smtp_crypto == 'ssl')
        $ssl = 'ssl://';
    $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
                                    $this->smtp_port,
                                    $errno,
                                    $errstr,
                                    $this->smtp_timeout);

    if ( ! is_resource($this->_smtp_connect))
    {
        $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
        return FALSE;
    }

    $this->_set_error_message($this->_get_smtp_data());

    if ($this->smtp_crypto == 'tls')
    {
        $this->_send_command('hello');
        $this->_send_command('starttls');
        stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
    }

    return $this->_send_command('hello');
}

我需要找到一种通过代理和供应凭据打开此连接的方法。我将快速进行条件切换,这样就可以与不需要它的远程服务器互换。

非常感谢您对此提供任何帮助!

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。我在这里发布了解决方案:

https://github.com/pkandathil/ci_proxy_email

您必须扩展CI_Email库以接受代理。然后使用stream_socket_client而不是使用fsockopen,并在那里指定代理。

protected function _smtp_connect() {
$ssl = NULL;
if ($this->smtp_crypto == 'ssl')
  $ssl = 'ssl://';
$CI =& get_instance();
if(!empty($this->proxy)) {
  $context = stream_context_create(['http' => ['proxy' => 'tcp://' . $this->proxy]]);
  $this->_smtp_connect = stream_socket_client($ssl.$this->smtp_host . ':' . $this->smtp_port,
                $errno,
                $errstr,
                $this->smtp_timeout,
                STREAM_CLIENT_CONNECT,
                $context);
}
else {
  $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
                $this->smtp_port,
                $errno,
                $errstr,
                $this->smtp_timeout);
}
if ( ! is_resource($this->_smtp_connect))
{
  $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
  return FALSE;
}
$this->_set_error_message($this->_get_smtp_data());
if ($this->smtp_crypto == 'tls')
{
  $this->_send_command('hello');
  $this->_send_command('starttls');
  stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
}
return $this->_send_command('hello');

} }