Skip to content

Commit

Permalink
Added first example
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Oct 26, 2024
1 parent 4be9d3e commit 02c75dd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ _backup*
/*.phar
/composer.lock

/examples/Example1.php
/examples/config.json
!/src/bin
/tests/assets/config.save.json
Expand Down
9 changes: 9 additions & 0 deletions examples/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MAIL_HOST=127.0.0.1
MAIL_PORT=993
MAIL_PROTOCOL=imap
MAIL_ENCRYPTION=ssl
MAIL_VALIDATECERT=true
MAIL_USER=demouser
MAIL_PASSWORD=demopassword
MAIL_FOLDERS=INBOX
MAIL_MIMETYPES=text/xml,application/pdf
46 changes: 46 additions & 0 deletions examples/ExampleHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use Dotenv\Dotenv;
use Dotenv\Exception\InvalidEncodingException;
use Dotenv\Exception\InvalidFileException;
use Dotenv\Exception\ValidationException;
use horstoeko\zugferdmail\config\ZugferdMailAccount;
use horstoeko\zugferdmail\config\ZugferdMailConfig;

class ExampleHelper
{
/**
* Create a mail account from a .env file
*
* @param ZugferdMailConfig $config
* @return ZugferdMailAccount
* @throws RuntimeException
* @throws InvalidEncodingException
* @throws InvalidFileException
* @throws ValidationException
* @throws InvalidArgumentException
*/
public static function createMailAccountFromDotEnv(ZugferdMailConfig $config): ZugferdMailAccount
{
$dotEnv = Dotenv::createImmutable(dirname(__FILE__));
$dotEnv->safeLoad();
$dotEnv->required('MAIL_PORT')->isInteger();
$dotEnv->required('MAIL_VALIDATECERT')->isBoolean();

$mailAccount = $config->addAccount(
'myaccount-1',
$_ENV['MAIL_HOST'],
filter_var($_ENV['MAIL_PORT'], FILTER_VALIDATE_INT),
$_ENV['MAIL_PROTOCOL'],
$_ENV['MAIL_ENCRYPTION'],
filter_var($_ENV['MAIL_VALIDATECERT'], FILTER_VALIDATE_BOOLEAN),
$_ENV['MAIL_USER'],
$_ENV['MAIL_PASSWORD']
);

$mailAccount->setFoldersToWatch(explode(",", $_ENV['MAIL_FOLDERS']));
$mailAccount->setMimeTypesToWatch(explode(",", $_ENV['MAIL_MIMETYPES']));

return $mailAccount;
}
}
28 changes: 28 additions & 0 deletions examples/ExampleSimple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Webklex\PHPIMAP\Folder;
use Webklex\PHPIMAP\Message;
use Webklex\PHPIMAP\Attachment;
use horstoeko\zugferd\ZugferdDocumentReader;
use horstoeko\zugferdmail\ZugferdMailReader;
use horstoeko\zugferdmail\config\ZugferdMailConfig;
use horstoeko\zugferdmail\config\ZugferdMailAccount;
use horstoeko\zugferdmail\handlers\ZugferdMailHandlerSaveToFile;

require_once dirname(__FILE__) . "/../vendor/autoload.php";
require_once dirname(__FILE__) . '/ExampleHelper.php';

$config = new ZugferdMailConfig();

$account = ExampleHelper::createMailAccountFromDotEnv($config);

$account->addHandler(new ZugferdMailHandlerSaveToFile('/tmp', 'file.xml'));

$account->addCallback(function (ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document, int $recognitionType) {
$document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $invoiceCurrency, $taxCurrency, $documentname, $documentlanguage, $effectiveSpecifiedPeriod);
echo "Document found ... " . PHP_EOL;
echo "Document No. ..... " . $documentno . PHP_EOL;
});

$reader = new ZugferdMailReader($config);
$reader->checkAllAccounts();

0 comments on commit 02c75dd

Please sign in to comment.