Added IMAP testing function to emailbackup accounts

This commit is contained in:
Xenomporio 2022-07-22 17:27:42 +02:00
parent 881267a00e
commit 8a1bfda324
2 changed files with 92 additions and 2 deletions

View File

@ -67,7 +67,10 @@
<tr><td>{|imap_sentfolder_aktiv|}:</td><td><input type="text" name="imap_sentfolder_aktiv" value="[IMAP_SENTFOLDER_AKTIV]" size="40"></td></tr>
<tr><td>{|imap_sentfolder|}:</td><td><input type="text" name="imap_sentfolder" value="[IMAP_SENTFOLDER]" size="40"></td></tr>
<tr><td>{|imap_port|}:</td><td><input type="text" name="imap_port" value="[IMAP_PORT]" size="40"></td></tr>
<tr><td>{|imap_type|}:</td><td><input type="text" name="imap_type" value="[IMAP_TYPE]" size="40"><i>0 = standard, 1 = SSL</i></td></tr>
<tr><td>{|imap_type|}:</td><td><input type="text" name="imap_type" value="[IMAP_TYPE]" size="40"><i>1 = standard, 3 = SSL, 5 = OAuth</i></td></tr>
<tr><td width="50">Testmail:</td><td>
<input type="submit" form="imap_test" value="IMAP testen" id="testimap-button">&nbsp;<i>Bitte erst speichern und dann testen!</i>
</td></tr>
</table>
</fieldset>
</div>
@ -168,3 +171,9 @@
<input type="text" name="id" value="[ID]" style="display:none">
</form>
<form id="imap_test" action = "index.php">
<input type="text" name="module" value="emailbackup" style="display:none">
<input type="text" name="action" value="test_imap" style="display:none">
<input type="text" name="id" value="[ID]" style="display:none">
</form>

View File

@ -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(
'<div class="error">Fehler beim Versende der Testmail: '.$this->app->erp->mail_error.'</div>'
'<div class="error">Fehler beim Versenden der Testmail: '.$this->app->erp->mail_error.'</div>'
);
}
$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(
'<div class="error">Fehler IMAP Test: '.$this->app->erp->mail_error.'</div>');
$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(
'<div class="error">Fehler IMAP Test: '.$this->app->erp->mail_error.'</div>');
$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(
'<div class="error">Fehler IMAP Test: '.$this->app->erp->mail_error.'</div>');
$error = true;
}
$mailClient->expunge();
$mailClient->disconnect();
if (!$error) {
$msg = $this->app->erp->base64_url_encode(
'<div class="info">IMAP Verbindung erfolgreich!</div>');
$logger->debug(
'IMAP test ok {email}',
['email' => $account->getEmailAddress(), 'account' => $account]
);
}
} else
{
$msg = $this->app->erp->base64_url_encode(
'<div class="error">Kein Account gefunden!</div>');
}
$this->app->Location->execute("index.php?module=emailbackup&id=$id&action=edit&msg=$msg");
}
}