Skip to content

Commit

Permalink
Merge pull request #1 from jeboehm/change_imap_lib
Browse files Browse the repository at this point in the history
feat: Support tls >= 1.2 by using horde_imap_client
  • Loading branch information
jeboehm authored Dec 18, 2022
2 parents 14b7ea0 + 02ff900 commit e49763a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 57 deletions.
20 changes: 3 additions & 17 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,14 @@ jobs:
tools: phive
coverage: xdebug

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
run: |
composer install --no-progress --prefer-dist --optimize-autoloader
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Install Phive dependencies
run: |
phive install --force-accept-unsigned --trust-gpg-keys 0xE82B2FB314E9906E
run: phive install --trust-gpg-keys 2DF45277AEF09A2F,E82B2FB314E9906E

- name: Compile phar
run: |
composer run build
run: composer run build

- name: Run phar
run: ./imap-tester.phar
Expand Down
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="humbug/box" version="^3.16.0" installed="3.16.0" location="./tools/box" copy="false"/>
<phar name="php-cs-fixer" version="^3.8.0" installed="3.8.0" location="./tools/php-cs-fixer" copy="false"/>
<phar name="php-cs-fixer" version="^3.8.0" installed="3.13.1" location="./tools/php-cs-fixer" copy="false"/>
</phive>
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
FROM composer:2.3 AS composer
FROM php:8.0-alpine

RUN apk --no-cache add openssl-dev imap-dev gnupg icu-dev && \
docker-php-ext-configure imap --with-imap --with-imap-ssl && \
docker-php-ext-install imap intl && \
RUN apk --no-cache add gnupg icu-dev && \
docker-php-ext-install intl && \
wget -O phive.phar "https://phar.io/releases/phive.phar" && \
wget -O phive.phar.asc "https://phar.io/releases/phive.phar.asc" && \
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x6AF725270AB81E04D79442549D8A98B29B2D5D79 && \
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# imap-tester

Provides a test utility for [docker-mailserver](https://github.com/jeboehm/docker-mailserver).
Do not use.

## Installation

Download phar from the [latest release](https://github.com/jeboehm/imap-tester/releases/tag/v1.1.0).

## Usage

```
$ imap-tester.phar list
test:count Count messages in a folder
test:move Move messages from one folder to another
```
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
"description": "Tests imap and pop3 mailboxes.",
"require": {
"php": "^8.0",
"ext-imap": "*",
"symfony/console": "~5.4",
"php-imap/php-imap": "~5.0"
"bytestream/horde-imap-client": "^2.31"
},
"license": "MIT",
"autoload": {
Expand Down
10 changes: 5 additions & 5 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ abstract class AbstractCommand extends Command
{
protected function configure(): void
{
$this->addArgument('host', InputArgument::REQUIRED);
$this->addArgument('port', InputArgument::REQUIRED);
$this->addArgument('username', InputArgument::REQUIRED);
$this->addArgument('password', InputArgument::REQUIRED);
$this->addArgument('folder', InputArgument::REQUIRED);
$this->addArgument('host', InputArgument::REQUIRED, 'POP3 / IMAP host');
$this->addArgument('port', InputArgument::REQUIRED, 'POP3 / IMAP port');
$this->addArgument('username', InputArgument::REQUIRED, 'Username');
$this->addArgument('password', InputArgument::REQUIRED, 'Password');
$this->addArgument('folder', InputArgument::REQUIRED, 'Folder to take action on (e.g. INBOX)');
}

final protected function getImapService(InputInterface $input): ImapService
Expand Down
1 change: 1 addition & 0 deletions src/Command/CountCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected function configure(): void
parent::configure();

$this->setName('test:count');
$this->setDescription('Count messages in a folder');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand Down
5 changes: 3 additions & 2 deletions src/Command/MoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ protected function configure(): void
parent::configure();

$this->setName('test:move');
$this->addArgument('messageIndex', InputArgument::REQUIRED);
$this->addArgument('targetFolder', InputArgument::REQUIRED);
$this->setDescription('Move messages from one folder to another');
$this->addArgument('messageIndex', InputArgument::REQUIRED, 'Message index (positive integer)');
$this->addArgument('targetFolder', InputArgument::REQUIRED, 'Target folder to move the mail to');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand Down
52 changes: 26 additions & 26 deletions src/Service/ImapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@

namespace Service;

use InvalidArgumentException;
use PhpImap\Mailbox;
use RuntimeException;

class ImapService
{
private Mailbox $mailbox;
/** @var \Horde_Imap_Client_Socket_Pop3|\Horde_Imap_Client_Socket */
private \Horde_Imap_Client_Base $client;

public function __construct($host, $port, $username, $password)
{
Expand All @@ -41,42 +38,45 @@ public function __construct($host, $port, $username, $password)
break;

default:
throw new InvalidArgumentException('Port not known');
throw new \InvalidArgumentException('Port not known');
}

$this->mailbox = new Mailbox(
sprintf(
'{%s:%s/%s/%s/novalidate-cert}',
$host,
$port,
$serverType,
$sslType,
),
$username,
$password
);

$this->mailbox->setConnectionArgs(CL_EXPUNGE);
$options = [
'username' => $username,
'password' => $password,
'hostspec' => $host,
'port' => $port,
'secure' => $sslType,
];

if ('pop3' === $serverType) {
$this->client = new \Horde_Imap_Client_Socket_Pop3($options);
} else {
$this->client = new \Horde_Imap_Client_Socket($options);
}
}

public function doCount(string $folder): int
{
$this->mailbox->switchMailbox($folder);
$results = $this->client->search($folder)['match'];

return $this->mailbox->countMails();
return is_countable($results) ? count($results) : 0;
}

public function doMove(string $folder, int $messageIndex, string $targetFolder): bool
{
$this->mailbox->switchMailbox($folder);
$mailsIds = $this->mailbox->searchMailbox();
$mails = $this->client->search($folder)['match'];

if (!array_key_exists($messageIndex, $mailsIds)) {
throw new RuntimeException('Mail was not found.');
if (!array_key_exists($messageIndex, $mails->ids)) {
throw new \RuntimeException('Mail was not found.');
}

$countBefore = $this->doCount($folder);
$this->mailbox->moveMail($mailsIds[0], $targetFolder);
$this->client->copy($folder, $targetFolder, [
'ids' => new \Horde_Imap_Client_Ids($mails->ids[$messageIndex]),
'move' => true,
]);
$this->client->close();

return $this->doCount($folder) !== $countBefore;
}
Expand Down

0 comments on commit e49763a

Please sign in to comment.