Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to pass ssl_context_options for use self-signed SSL certificates. #1

Merged
merged 1 commit into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Protocol/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public function __destruct()
* @param string $host hostname or IP address of IMAP server
* @param int|null $port of IMAP server, default is 143 (993 for ssl)
* @param string|bool $ssl use 'SSL', 'TLS' or false
* @param array $ssl_context_options options, passed to stream_context_create()
* @throws Exception\RuntimeException
* @return string welcome message
*/
public function connect($host, $port = null, $ssl = false)
public function connect($host, $port = null, $ssl = false, $ssl_context_options = [])
{
$isTls = false;

Expand All @@ -87,7 +88,15 @@ public function connect($host, $port = null, $ssl = false)
}

ErrorHandler::start();
$this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION);
$context = stream_context_create($ssl_context_options);
$this->socket = stream_socket_client(
$host.':'.$port,
$errno,
$errstr,
self::TIMEOUT_CONNECTION,
STREAM_CLIENT_CONNECT,
$context
);
$error = ErrorHandler::stop();
if (!$this->socket) {
throw new Exception\RuntimeException(sprintf(
Expand Down
4 changes: 3 additions & 1 deletion src/Storage/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public function getRawContent($id, $part = null)
* - password password for user 'username' [optional, default = '']
* - port port for IMAP server [optional, default = 110]
* - ssl 'SSL' or 'TLS' for secure sockets
* - ssl_context_options Options passed to stream_context_create()
* - folder select this folder [optional, default = 'INBOX']
*
* @param array $params mail reader specific parameters
Expand Down Expand Up @@ -200,9 +201,10 @@ public function __construct($params)
$password = isset($params->password) ? $params->password : '';
$port = isset($params->port) ? $params->port : null;
$ssl = isset($params->ssl) ? $params->ssl : false;
$ssl_context_options = isset($params->ssl_context_options) ? $params->ssl_context_options: [];

$this->protocol = new Protocol\Imap();
$this->protocol->connect($host, $port, $ssl);
$this->protocol->connect($host, $port, $ssl, $ssl_context_options);
if (!$this->protocol->login($params->user, $password)) {
throw new Exception\RuntimeException('cannot login, user or password wrong');
}
Expand Down