From e12ce51dcc70e3d388bfa69c775c61253b15f62a Mon Sep 17 00:00:00 2001 From: Ante de Baas <5467398+antedebaas@users.noreply.github.com> Date: Sat, 20 Jan 2024 12:27:22 +0100 Subject: [PATCH] Add option to delete processed mails (#23) --- .env | 4 +++- .env.test | 3 ++- Dockerfile | 1 + config/services.yaml | 1 + dockerfiles/containerstartup.sh | 1 + src/Command/CheckmailboxCommand.php | 9 ++++++++- 6 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.env b/.env index 0977c18..9e343c2 100644 --- a/.env +++ b/.env @@ -34,4 +34,6 @@ MAILER_DSN=null://null MAILBOX_CONNECTION= MAILBOX_USERNAME= -MAILBOX_PASSWORD= \ No newline at end of file +MAILBOX_PASSWORD= + +DELETE_PROCESSED_MAILS= \ No newline at end of file diff --git a/.env.test b/.env.test index 28fafde..fa6d5af 100644 --- a/.env.test +++ b/.env.test @@ -29,4 +29,5 @@ MAILER_DSN=null://null MAILBOX_CONNECTION="localhost:993/imap/ssl" MAILBOX_USERNAME="none@example.org" -MAILBOX_PASSWORD="password" \ No newline at end of file +MAILBOX_PASSWORD="password" +DELETE_PROCESSED_MAILS=false \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 221f58a..493d364 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,7 @@ ENV MAILER_SMTPPORT=25 ENV MAILER_IMAPPORT=993 ENV MAILER_USER= ENV MAILER_PASSWORD= +ENV DELETE_PROCESSED_MAILS= RUN apk --update add ca-certificates RUN apk --no-cache add \ diff --git a/config/services.yaml b/config/services.yaml index a08183f..ddf84c2 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -5,6 +5,7 @@ # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration parameters: app.mailbox_username: '%env(MAILBOX_USERNAME)%' + app.delete_processed_mails: '%env(DELETE_PROCESSED_MAILS)%' services: # default configuration for services in *this* file diff --git a/dockerfiles/containerstartup.sh b/dockerfiles/containerstartup.sh index 6eca509..f7f9355 100644 --- a/dockerfiles/containerstartup.sh +++ b/dockerfiles/containerstartup.sh @@ -10,6 +10,7 @@ if [ ! -f "/var/www/html/.env.local" ]; then echo "MAILBOX_CONNECTION=\"$MAILER_HOST:$MAILER_IMAPPORT/imap/ssl\"" >> /var/www/html/.env.local echo "MAILBOX_USERNAME=\"$MAILER_USER\"" >> /var/www/html/.env.local echo "MAILBOX_PASSWORD=\"$MAILER_PASSWORD\"" >> /var/www/html/.env.local + echo "DELETE_PROCESSED_MAILS=\"$DELETE_PROCESSED_MAILS\"" >> /var/www/html/.env.local fi echo "Run migrations" diff --git a/src/Command/CheckmailboxCommand.php b/src/Command/CheckmailboxCommand.php index aabe740..c474e91 100644 --- a/src/Command/CheckmailboxCommand.php +++ b/src/Command/CheckmailboxCommand.php @@ -12,6 +12,7 @@ use Doctrine\ORM\EntityManagerInterface; use SecIT\ImapBundle\Service\Imap; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use App\Entity\Domains; use App\Entity\MXRecords; @@ -32,11 +33,13 @@ class CheckmailboxCommand extends Command { private $em; private $imap; + private $params; - public function __construct(EntityManagerInterface $em, Imap $imap) + public function __construct(EntityManagerInterface $em, Imap $imap, ParameterBagInterface $params) { $this->em = $em; $this->imap = $imap; + $this->params = $params; parent::__construct(); } @@ -276,6 +279,10 @@ private function open_mailbox(Imap $imap):array $smtptls_reports = array_merge($smtptls_reports,$new_reports['smtptls_reports']); unlink($attachment->filePath); } + + if ($this->params->get('app.delete_processed_mails') == "true") { + $mailbox->deleteMail($mailId); + } } return array('num_emails' => $num_emails, 'reports' => array('dmarc_reports' => $dmarc_reports, 'smtptls_reports' => $smtptls_reports)); }