Caution
This library is currently still considered experimental and should therefore be used with caution. I would be happy for an issue to be posted if bugs are found.
The code in this project is provided under the MIT license.
With this library it is possible to monitor mail accounts (IMAP and POP3) and to check and process incoming electronic invoice documents (ZUGFeRD PDF and XML).
This package makes use of
There is one recommended way to install horstoeko/zugferdmail
via Composer:
composer require horstoeko/zugferdmail
For detailed eplanation you may have a look in the examples of this package and the documentation attached to every release.
First, it is necessary to configure the library so that it is aware of the mail accounts to be checked. For IMAP accounts, the folders to be monitored must also be defined. In addition, any actions (handlers) can be defined for each mail account.
First you need to create a configuration instance:
$config = new ZugferdMailConfig();
The mail accounts to be monitored must then be defined. You can also specify the folders and mimetypes to be checked:
$account1 = $config->addAccount('demo', '192.168.1.1', 993, 'imap', 'ssl', false, 'demouser', 'demopassword');
$account1->addFolderToWatch('INBOX');
$account1->addFolderToWatch('somefolder/somesubfolder');
$account1->addMmimeTypeToWatch('application/pdf');
$account1->addMmimeTypeToWatch('text/xml');
Last but not least, the actions (handlers) to be performed are specified for each mail account, which are executed when a ZUGFeRD or XML document is found. Some of these handlers are already available, but you can also define your own actions:
$account1->addHandler(new ZugferdMailHandlerMoveMessage('Invoice/Incoming'));
$account1->addHandler(new ZugferdMailHandlerSaveToFile('/tmp', 'invoice.att'));
You can also define callbacks to call when a document was found. If such a callback function returns the value false, then all remaining callback functions are not called:
$account1->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;
});
Please note that the callbacks are neither loaded from a configuration file nor saved in one.
To perform the actual handling of all mail accounts, instantiate the class ZugferdMailReader
, to which the configuration is passed. The monitoring is started by calling the method checkAllAccounts
$reader = new ZugferdMailReader($config);
$reader->checkAllAccounts();
Class | Description |
---|---|
ZugferdMailHandlerNull | Does not perform any operations |
ZugferdMailHandlerCli | Displays brief information about the e-invoice document found on the console |
ZugferdMailHandlerCopyMessage | Copies the message to another (different) directory |
ZugferdMailHandlerMoveMessage | Moves the message to another (different) directory |
ZugferdMailHandlerDeleteMessage | Deletes the message |
ZugferdMailHandlerMarkSeenMessage | Marks the message as seen |
ZugferdMailHandlerMarkUnseenMessage | Marks the message as unseen |
ZugferdMailHandlerSaveToFile | Saves the E-Document to a specified directory, optionally with a different filename |
It is quite easy to implement your own action (handler). To do this, define your own class which extends a class from ZugferdMailHandlerAbstract
. This abstract class defines a single method handleDocument
, which is passed information about the folder, message, attachment and the e-invoice document:
public function handleDocument(
ZugferdMailAccount $account,
Folder $folder,
Message $message,
Attachment $attachment,
ZugferdDocumentReader $document,
int $recognitionType
);
An example:
use horstoeko\zugferd\ZugferdDocumentReader;
use horstoeko\zugferdmail\config\ZugferdMailAccount;
use Webklex\PHPIMAP\Attachment;
use Webklex\PHPIMAP\Folder;
use Webklex\PHPIMAP\Message;
class MyOwnHandler extends ZugferdMailHandlerAbstract
{
public function handleDocument(
ZugferdMailAccount $account,
Folder $folder,
Message $message,
Attachment $attachment,
ZugferdDocumentReader $document,
int $recognitionType)
{
// Do some stuff
}
}
The parameter $recognitionType is one of the constants from ZugferdMailReaderRecognitionType
Name | Vakue | Description |
---|---|---|
ZFMAIL_RECOGNITION_TYPE_PDF_CII | 0 | The document was recognized from a ZUGFeRD/Factur-X PDF attachment |
ZFMAIL_RECOGNITION_TYPE_XML_CII | 1 | The document was recognized from a ZUGFeRD/Factur-X XML attachment |
ZFMAIL_RECOGNITION_TYPE_XML_UBL | 2 | The document was recognized from a ZUGFeRD/Factur-X XML attachment (in UBL-Syntax) |
A possible implementation could look like this, for example:
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 dirname(__FILE__) . "/../vendor/autoload.php";
$config = new ZugferdMailConfig();
$account1 = $config->addAccount('de', '127.0.0.1', 993, 'imap', 'ssl', false, 'demouser', 'demopassword');
$account1->addFolderToWatch('Accounts/invoice@mydomain.com');
$account1->addMimeTypeToWatch('application/pdf');
$account1->addMimeTypeToWatch('text/xml');
$account1->addHandler(new ZugferdMailHandlerSaveToFile('/tmp', 'file.xml'));
$account1->addCallback(function(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document, int $recognitionType) {
$document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $invoiceCurrency, $taxCurrency, $documentname, $documentlanguage, $effectiveSpecifiedPeriod);
// Some business logic here...
});
$account2 = $config->addAccount('de', '172.10.10.10', 993, 'imap', 'ssl', false, 'demouser', 'demopassword');
$account2->addFolderToWatch('Accounts/invoice@otheromain.com');
$account2->addMimeTypeToWatch('application/pdf');
$account2->addMimeTypeToWatch('text/xml');
$account1->addCallback(function(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document, int $recognitionType) {
$document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $invoiceCurrency, $taxCurrency, $documentname, $documentlanguage, $effectiveSpecifiedPeriod);
// Some other business logic here...
});
$reader = new ZugferdMailReader($config);
$reader->checkAllAccounts();
This library also includes a console application with which it is possible to carry out corresponding operations via shell. To get an overview of the existing commands from the “zfmail” namespace, use:
./vendor/bin/ZugferdMailConsole list
The command zfmail:processmailboxfolders
is available for monitoring a mailbox via shell:
./vendor/bin/ZugferdMailConsole zfmail:processmailboxfolders [options]
This has the following parameters:
Parameter | Description | Default | Multiple |
---|---|---|---|
host | The IP or hostname | ❌ | |
port | The port on the host | 993 | ❌ |
protocol | The protocol to use | imap | ❌ |
encryption | The encryption method to use | ssl | ❌ |
validateCert | If given the SSL certificates will be validated | false | ❌ |
username | The username to use for login | ❌ | |
password | The password to use for login | ❌ | |
authentication | The authentication method to use | ❌ | |
timeout | The connection timeout | 30 | ❌ |
folder | A folder to look into | ✔️ | |
mimetype | An attachment mimetype to check | ✔️ | |
handler | A handler to use | ✔️ | |
enableublsupport | If given the application will check for UBL syntax too | false | ❌ |
enablesymfonyvalidation | If given the application will validate the XML using symfony validator against the yaml definitions | false | ❌ |
enablexsdvalidation | If given the application will validate the XML against the XSD definitions | false | ❌ |
enablekositvalidation | If given the application will validate the XML using the KosIT validator (JAVA required) | false | ❌ |
enableunseenonly | If given only unseen message are processed | false | ❌ |
Example usage
./vendor/bin/ZugferdMailConsole zfmail:processmailboxfolders \
--host 127.0.0.1 \
--port 993 \
--username demouser \
--password demopassword \
--folder=INBOX \
--mimetype=application/pdf \
--mimetype=text/xml \
--handler="horstoeko\zugferdmail\handlers\ZugferdMailHandlerCli" \
--handler="horstoeko\zugferdmail\handlers\ZugferdMailHandlerSaveToFile,/tmp" \
--enableublsupport