From 7c3e7666620bfa89fdbe732a7f76e679da12e11c Mon Sep 17 00:00:00 2001
From: Maarten de Waard <maarten@greenhost.nl>
Date: Mon, 25 Oct 2021 14:24:59 +0200
Subject: [PATCH] add smtp configuration, always add "additional_functions" and
 optionally add smtp and roles settings to it

---
 values.yaml                                   | 38 ++++++++++++++++---
 .../roles/wordpress-init/tasks/main.yml       | 14 +++++++
 .../wordpress-init/tasks/openid-connect.yml   | 14 -------
 .../roles/wordpress-init/tasks/smtp.yml       |  8 ++++
 .../templates/additional_functions.php        | 17 +++++++++
 5 files changed, 72 insertions(+), 19 deletions(-)
 create mode 100644 wp-cli-docker/roles/wordpress-init/tasks/smtp.yml

diff --git a/values.yaml b/values.yaml
index 00f1633..65b9606 100644
--- a/values.yaml
+++ b/values.yaml
@@ -121,10 +121,6 @@ wordpress:
   theme_active: twentynineteen
   # Fallback theme from wordpress repo which is fetched + activated if helm reports an error 
   theme_fallback: twentytwenty 
-
-    
-
-
   wp_content:
     ## The directory to mount the files placed in wp-content. You shouldn't have to
     ## change this.
@@ -193,6 +189,27 @@ service:
   type: ClusterIP
   port: 8080
 
+smtp_settings:
+  # Enable using these SMTP settings
+  enabled: false
+  # Username for SMTP authentication
+  smtp_user: admin@example.com 
+  # Password for STMP authentication
+  smtp_pass: password
+  # Hostname of the mailserver
+  smtp_host: smtp.greenhost.nl
+  # SMTP from email address
+  smtp_from: admin@example.com
+  # SMTP from name
+  smtp_name: Admin
+  # SMTP port number - likely to be 25, 465 or 587
+  smtp_port: 587
+  # Encryption system to use - ssl or tls
+  smtp_secure: 'tls'
+  # Use SMTP authentication (true|false)
+  smtp_auth: true
+  # Can be set to 1 or 2 for debug logs
+  smtp_debug: 0
 
 ## Dictionary with variables that are inserted into the wordpress-init default
 ## task. Feel free to override these if necessary
@@ -395,7 +412,6 @@ ansibleSecrets: |
     scope: {{ .Values.openid_connect_settings.scope }}
     state_time_limit: {{ .Values.openid_connect_settings.state_time_limit }}
     role_key: {{ .Values.openid_connect_settings.role_key }}
-
   WP_SALTS:
     AUTH_KEY: {{ .Values.wpSalts.AUTH_KEY | default ( randAlphaNum 32) }}
     AUTH_SALT: {{ .Values.wpSalts.AUTH_SALT | default ( randAlphaNum 32) }}
@@ -406,6 +422,18 @@ ansibleSecrets: |
     SECURE_AUTH_KEY: {{ .Values.wpSalts.SECURE_AUTH_KEY | default ( randAlphaNum 32) }}
     SECURE_AUTH_SALT: {{ .Values.wpSalts.SECURE_AUTH_SALT | default ( randAlphaNum 32) }}
     WP_CACHE_KEY_SALT: {{ .Values.wpSalts.WP_CACHE_KEY_SALT | default ( randAlphaNum 32) }}
+  WP_SMTP_ENABLED: {{ .Values.smtp_settings.enabled }}
+  WP_SMTP_SETTINGS:
+    SMTP_USER: {{ .Values.smtp_settings.smtp_user }}
+    SMTP_PASS: {{ .Values.smtp_settings.smtp_pass }}
+    SMTP_HOST: {{ .Values.smtp_settings.smtp_host }}
+    SMTP_FROM: {{ .Values.smtp_settings.smtp_from }}
+    SMTP_NAME: {{ .Values.smtp_settings.smtp_name }}
+    SMTP_PORT: {{ .Values.smtp_settings.smtp_port }}
+    SMTP_SECURE: {{ .Values.smtp_settings.smtp_secure }}
+  WP_SMTP_RAW_SETTINGS:
+    SMTP_AUTH: {{ .Values.smtp_settings.smtp_auth }}
+    SMTP_DEBUG: {{ .Values.smtp_settings.smtp_debug }}
 
 ## Liveness and readiness probe values
 ## Ref: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes
diff --git a/wp-cli-docker/roles/wordpress-init/tasks/main.yml b/wp-cli-docker/roles/wordpress-init/tasks/main.yml
index 3332e6f..8e3102a 100644
--- a/wp-cli-docker/roles/wordpress-init/tasks/main.yml
+++ b/wp-cli-docker/roles/wordpress-init/tasks/main.yml
@@ -179,8 +179,22 @@
 - import_tasks: redis.yml
   when: WP_REDIS_ENABLED
 
+- name: Extend functions.php file
+  lineinfile:
+    path: "{{ wordpress_homedir }}/wp-includes/functions.php"
+    regexp: '^require.+additional_functions.php'
+    line:  require( ABSPATH . WPINC . '/additional_functions.php' );
+
 - import_tasks: openid-connect.yml
   when: WP_OPENID_CONNECT_ENABLED
 
+- import_tasks: smtp.yml
+  when: WP_OPENID_CONNECT_ENABLED
+
+- name: Copy additional functions file
+  template:
+    src: templates/additional_functions.php
+    dest: "{{ wordpress_homedir }}/wp-includes/additional_functions.php"
+
 - import_tasks: multilingual.yml
   when: WP_MULTILINGUAL_ENABLED
diff --git a/wp-cli-docker/roles/wordpress-init/tasks/openid-connect.yml b/wp-cli-docker/roles/wordpress-init/tasks/openid-connect.yml
index 6436d38..db3fd35 100644
--- a/wp-cli-docker/roles/wordpress-init/tasks/openid-connect.yml
+++ b/wp-cli-docker/roles/wordpress-init/tasks/openid-connect.yml
@@ -1,20 +1,6 @@
 ---
-
 - name: Install openid connect plugin
   shell: wp {{ cli_args }} plugin install daggerhart-openid-connect-generic --activate
 
 - name: Set openid connect plugin options
   command: wp {{ cli_args }} option set openid_connect_generic_settings --format=json '{{ WP_OPENID_CONNECT_SETTINGS | tojson }}'
-
-- name: Extend functions.php file
-  lineinfile:
-    path: "{{ wordpress_homedir }}/wp-includes/functions.php"
-    regexp: '^require.+additional_functions.php'
-    line:  require( ABSPATH . WPINC . '/additional_functions.php' );
-  when: WP_OPENID_CONNECT_ROLE_MAPPING_ENABLED
-
-- name: Copy additional functions file
-  template:
-    src: templates/additional_functions.php
-    dest: "{{ wordpress_homedir }}/wp-includes/additional_functions.php"
-  when: WP_OPENID_CONNECT_ROLE_MAPPING_ENABLED
diff --git a/wp-cli-docker/roles/wordpress-init/tasks/smtp.yml b/wp-cli-docker/roles/wordpress-init/tasks/smtp.yml
new file mode 100644
index 0000000..5f26699
--- /dev/null
+++ b/wp-cli-docker/roles/wordpress-init/tasks/smtp.yml
@@ -0,0 +1,8 @@
+- name: Define SMTP variables
+  shell: wp {{ cli_args }} config set "{{ item.key }}" "{{ item.value }}" --add --type=constant
+  loop: "{{ WP_SMTP_SETTINGS | dict2items }}"
+
+- name: Define SMTP variables
+  shell: wp {{ cli_args }} config set "{{ item.key }}" "{{ item.value }}" --add --raw --type=constant
+  loop: "{{ WP_SMTP_RAW_SETTINGS | dict2items }}"
+
diff --git a/wp-cli-docker/roles/wordpress-init/templates/additional_functions.php b/wp-cli-docker/roles/wordpress-init/templates/additional_functions.php
index e3842b0..e4d2f5d 100644
--- a/wp-cli-docker/roles/wordpress-init/templates/additional_functions.php
+++ b/wp-cli-docker/roles/wordpress-init/templates/additional_functions.php
@@ -1,4 +1,5 @@
 <?php
+{% if WP_OPENID_CONNECT_ROLE_MAPPING_ENABLED %}
 add_action('openid-connect-generic-update-user-using-current-claim', function( $user, $user_claim) {
     // Based on some data in the user_claim, modify the user.
     if ( array_key_exists( '{{ WP_OPENID_CONNECT_SETTINGS.role_key }}', $user_claim ) ) {
@@ -10,3 +11,19 @@ add_action('openid-connect-generic-update-user-using-current-claim', function( $
         }
     }
 }, 10, 2);
+{% endif %}
+{% if WP_SMTP_ENABLED %}
+    // SMTP Authentication
+    add_action( 'phpmailer_init', 'send_smtp_email' );
+    function send_smtp_email( $phpmailer ) {
+        $phpmailer->isSMTP();
+        $phpmailer->Host       = SMTP_HOST;
+        $phpmailer->SMTPAuth   = SMTP_AUTH;
+        $phpmailer->Port       = SMTP_PORT;
+        $phpmailer->Username   = SMTP_USER;
+        $phpmailer->Password   = SMTP_PASS;
+        $phpmailer->SMTPSecure = SMTP_SECURE;
+        $phpmailer->From       = SMTP_FROM;
+        $phpmailer->FromName   = SMTP_NAME;
+    }
+{% endif %}
-- 
GitLab