From 8a1bfda3247843fc6d3097f268a0e9795b7ba3fb Mon Sep 17 00:00:00 2001
From: Xenomporio <>
Date: Fri, 22 Jul 2022 17:27:42 +0200
Subject: [PATCH] Added IMAP testing function to emailbackup accounts
---
www/pages/content/emailbackup_edit.tpl | 11 +++-
www/pages/emailbackup.php | 83 +++++++++++++++++++++++++-
2 files changed, 92 insertions(+), 2 deletions(-)
diff --git a/www/pages/content/emailbackup_edit.tpl b/www/pages/content/emailbackup_edit.tpl
index e91ed189..0bf4ac07 100644
--- a/www/pages/content/emailbackup_edit.tpl
+++ b/www/pages/content/emailbackup_edit.tpl
@@ -67,7 +67,10 @@
{|imap_sentfolder_aktiv|}: | |
{|imap_sentfolder|}: | |
{|imap_port|}: | |
-{|imap_type|}: | 0 = standard, 1 = SSL |
+{|imap_type|}: | 1 = standard, 3 = SSL, 5 = OAuth |
+Testmail: |
+ Bitte erst speichern und dann testen!
+ |
@@ -168,3 +171,9 @@
+
+
diff --git a/www/pages/emailbackup.php b/www/pages/emailbackup.php
index fc4d1aa6..6bd70307 100644
--- a/www/pages/emailbackup.php
+++ b/www/pages/emailbackup.php
@@ -5,6 +5,7 @@
*/
use Xentral\Components\Database\Exception\QueryFailureException;
+use Xentral\Components\Logger\Logger;
class Emailbackup {
@@ -19,6 +20,7 @@ class Emailbackup {
$this->app->ActionHandler("edit", "emailbackup_edit");
$this->app->ActionHandler("delete", "emailbackup_delete");
$this->app->ActionHandler("test_smtp",'emailbackup_test_smtp');
+ $this->app->ActionHandler("test_imap",'emailbackup_test_imap');
$this->app->DefaultActionHandler("list");
$this->app->ActionHandlerListen($app);
@@ -294,11 +296,90 @@ $width = array('10%'); // Fill out manually later
}
else {
$msg = $this->app->erp->base64_url_encode(
- 'Fehler beim Versende der Testmail: '.$this->app->erp->mail_error.'
'
+ 'Fehler beim Versenden der Testmail: '.$this->app->erp->mail_error.'
'
);
}
$this->app->Location->execute("index.php?module=emailbackup&id=$id&action=edit&msg=$msg");
}
+ function emailbackup_test_imap() {
+
+ $id = $this->app->Secure->GetGET('id');
+
+ // get email Account
+ /** @var EmailAccountGateway $accountGateway */
+ $accountGateway = $this->app->Container->get('EmailAccountGateway');
+ $account = $accountGateway->getEmailAccountById($id);
+
+ if(!empty($account)) {
+ /** @var Ticket $ticketModule */
+ $ticketModule = $this->app->erp->LoadModul('ticket');
+ /** @var MailClientFactory $factory */
+ $factory = $this->app->Container->get('MailClientFactory');
+ /** @var MailClientConfigProvider $configProvider */
+ $configProvider = $this->app->Container->get('MailClientConfigProvider');
+ /** @var TicketFormatter $formatHelper */
+ $formatHelper = $this->app->Container->get('TicketFormatter');
+ /** @var TicketImportHelperFactory $importHelperFactory */
+ $importHelperFactory = $this->app->Container->get('TicketImportHelperFactory');
+
+ /** @var Logger $logger */
+ $logger = $this->app->Container->get('Logger');
+
+ $logger->debug(
+ 'Start imap test {email}',
+ ['email' => $account->getEmailAddress(), 'account' => $account]
+ );
+ // create mail client
+ try {
+ $mailConfig = $configProvider->createImapConfigFromAccount($account);
+ $mailClient = $factory->createImapClient($mailConfig);
+ } catch (Exception $e) {
+ $logger->error('Failed to create email client', ['error' => (string)$e, 'account' => $account]);
+ $msg = $this->app->erp->base64_url_encode(
+ 'Fehler IMAP Test: '.$this->app->erp->mail_error.'
');
+ $error = true;
+ }
+
+ // connect mail client
+ try {
+ $mailClient->connect();
+ } catch (Exception $e) {
+ $logger->error('Error during imap connection', ['error' => (string)$e, 'account' => $account]);
+ $msg = $this->app->erp->base64_url_encode(
+ 'Fehler IMAP Test: '.$this->app->erp->mail_error.'
');
+ $error = true;
+ }
+
+ // connet to INBOX folder
+ try {
+ $mailClient->selectFolder('INBOX');
+ } catch (Exception $e) {
+ $logger->error('Failed to select INBOX folder', ['error' => (string)$e, 'account' => $account]);
+ $msg = $this->app->erp->base64_url_encode(
+ 'Fehler IMAP Test: '.$this->app->erp->mail_error.'
');
+ $error = true;
+ }
+
+ $mailClient->expunge();
+ $mailClient->disconnect();
+
+ if (!$error) {
+ $msg = $this->app->erp->base64_url_encode(
+ 'IMAP Verbindung erfolgreich!
');
+ $logger->debug(
+ 'IMAP test ok {email}',
+ ['email' => $account->getEmailAddress(), 'account' => $account]
+ );
+
+ }
+ } else
+ {
+ $msg = $this->app->erp->base64_url_encode(
+ 'Kein Account gefunden!
');
+
+ }
+ $this->app->Location->execute("index.php?module=emailbackup&id=$id&action=edit&msg=$msg");
+ }
}