From ef9b877da57af77d7f085e7866a668af66f9c2b5 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen.haas@lakedrops.com>
Date: Wed, 10 Jan 2024 11:30:05 +0100
Subject: [PATCH] composer/plugin/docker4drupal#64 Add support for mailpit

---
 src/Handler.php                   | 18 +++++++++++++++-
 templates/docker-compose.yml.twig | 35 ++++++++++++++++++++++++++++++-
 templates/mailpitout.yml.twig     | 28 +++++++++++++++++++++++++
 3 files changed, 79 insertions(+), 2 deletions(-)
 create mode 100644 templates/mailpitout.yml.twig

diff --git a/src/Handler.php b/src/Handler.php
index 0be678b..3092d40 100644
--- a/src/Handler.php
+++ b/src/Handler.php
@@ -126,13 +126,26 @@ class Handler extends BaseHandler {
         'overwriteconfig' => FALSE,
       ],
       'mailhog' => [
-        'enable' => 1,
+        'enable' => 0,
         'host' => $this->env->receiveGlobal('MAILHOG_HOST', 'MailHog Host', 'smtp.freesmtpservers.com'),
         'port' => $this->env->receiveGlobal('MAILHOG_PORT', 'MailHog Port', '25'),
         'username' => $this->env->receiveGlobal('MAILHOG_USERNAME', 'MailHog Username'),
         'password' => $this->env->receiveGlobal('MAILHOG_PASSWORD', 'MailHog Password'),
         'mechanism' => $this->env->receiveGlobal('MAILHOG_MECHANISM', 'MailHog Auth Mechanism', 'NONE'),
       ],
+      'mailpit' => [
+        'enable' => 1,
+        'host' => $this->env->receiveGlobal('MAILPIT_HOST', 'MailPit Host', 'smtp.freesmtpservers.com'),
+        'port' => $this->env->receiveGlobal('MAILPIT_PORT', 'MailPit Port', '25'),
+        'username' => $this->env->receiveGlobal('MAILPIT_USERNAME', 'MailPit Username'),
+        'password' => $this->env->receiveGlobal('MAILPIT_PASSWORD', 'MailPit Password'),
+        'starttls' => $this->env->receiveGlobal('MAILPIT_STARTTLS', 'MailPit StartTLS'),
+        'allowinsecure' => $this->env->receiveGlobal('MAILPIT_ALLOW_INSECURE', 'MailPit allow insecure'),
+        'auth' => $this->env->receiveGlobal('MAILPIT_AUTH', 'MailPit Auth (none|plain|login|cram-md5)'),
+        'secret' => $this->env->receiveGlobal('MAILPIT_SECRET', 'MailPit Secret'),
+        'returnpath' => $this->env->receiveGlobal('MAILPIT_RETURNPATH', 'MailPit Bound Address'),
+        'recipientallowlist' => $this->env->receiveGlobal('MAILPIT_RECIPIENT_ALLOW_LIST', 'MailPit Regex for allowed recipients'),
+      ],
       'varnish' => [
         'enable' => 0,
       ],
@@ -613,6 +626,9 @@ class Handler extends BaseHandler {
       'mhout.json' => [
         'dest' => $projectRoot . '/tests',
       ],
+      'mailpitout.yml' => [
+        'dest' => $projectRoot . '/tests',
+      ],
     ];
     $files['cypress.config.js'] = [
       'source' => 'tests/',
diff --git a/templates/docker-compose.yml.twig b/templates/docker-compose.yml.twig
index 635e43f..dc267a7 100644
--- a/templates/docker-compose.yml.twig
+++ b/templates/docker-compose.yml.twig
@@ -51,6 +51,8 @@ services:
     environment:
 {% if mailhog.enable %}
       PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
+{% elseif mailpit.enable %}
+      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailpit:1025
 {% endif %}
       DB_HOST: mariadb
       DB_USER: drupal
@@ -291,8 +293,8 @@ services:
       traefik.http.routers.solr-{{ projectname }}.tls.certresolver: lakedrops
 {% endif %}
 {% endif %}
-
 {% if mailhog.enable and (not drupal.live|default(0) or staging) %}
+
   mailhog:
     image: '{{ docker_image_prefix }}mailhog/mailhog'
 {% if staging %}
@@ -322,6 +324,37 @@ services:
       traefik.http.routers.mailhog-{{ projectname }}.tls.certresolver: lakedrops
 {% endif %}
 {% endif %}
+{% elseif mailpit.enable and (not drupal.live|default(0) or staging) %}
+
+  mailpit:
+    image: '{{ docker_image_prefix }}axllent/mailpit'
+{% if staging %}
+    restart: unless-stopped
+{% endif %}
+    networks:
+      - traefik-public
+      - default
+    environment:
+      MP_SMTP_RELAY_CONFIG: /test/mailpitout.yml
+    privileged: true
+    volumes:
+      - /etc/timezone:/etc/timezone:ro
+      - /etc/localtime:/etc/localtime:ro
+      - {{ projectroot }}/tests/mailpitout.yml:/test/mailpitout.yml
+    labels:
+      traefik.enable: 'true'
+      traefik.docker.network: traefik-public
+{% if basicauth.enable %}
+      traefik.http.middlewares.mailpit-{{ projectname }}-auth.basicauth.users: {{ basicauth.code }}
+      traefik.http.routers.mailpit-{{ projectname }}.middlewares: mailpit-{{ projectname }}-auth@docker
+{% endif %}
+      traefik.http.services.{{ projectname }}_mailpit.loadbalancer.server.port: 8025
+      traefik.http.routers.mailpit-{{ projectname }}.rule: Host(`mailpit-{{ projectdomain }}`)
+{% if drupal.live|default(0) or traefik.usessl %}
+      traefik.http.routers.mailpit-{{ projectname }}.tls: 'true'
+      traefik.http.routers.mailpit-{{ projectname }}.tls.certresolver: lakedrops
+{% endif %}
+{% endif %}
 {% if node.enable %}
 
   nodejs:
diff --git a/templates/mailpitout.yml.twig b/templates/mailpitout.yml.twig
new file mode 100644
index 0000000..1c111b8
--- /dev/null
+++ b/templates/mailpitout.yml.twig
@@ -0,0 +1,28 @@
+host: '{{ mailpit.host }}'
+{% if mailpit.port != '' %}
+port: '{{ mailpit.port }}'
+{% endif %}
+{% if mailpit.username != '' %}
+username: '{{ mailpit.username }}'
+{% endif %}
+{% if mailpit.password != '' %}
+password: '{{ mailpit.password }}'
+{% endif %}
+{% if mailpit.starttls != '' %}
+starttls: {{ mailpit.starttls }}
+{% endif %}
+{% if mailpit.allowinsecure != '' %}
+allow-insecure: {{ mailpit.allowinsecure }}
+{% endif %}
+{% if mailpit.auth != '' %}
+auth: '{{ mailpit.auth }}'
+{% endif %}
+{% if mailpit.secret != '' %}
+secret: '{{ mailpit.secret }}'
+{% endif %}
+{% if mailpit.returnpath != '' %}
+return-path: '{{ mailpit.returnpath }}'
+{% endif %}
+{% if mailpit.recipientallowlist != '' %}
+recipient-allowlist: '{{ mailpit.recipientallowlist }}'
+{% endif %}
-- 
GitLab