From 0e59763a0e1a26631ecf5c73480dd8d760577ce6 Mon Sep 17 00:00:00 2001
From: Arthur BOUDREAULT <arthur@lydra.fr>
Date: Tue, 26 Oct 2021 13:52:28 +0000
Subject: [PATCH] feat(yunohost): add SMTP relay support

---
 README-FR.md         | 15 ++++++++++++++-
 README.md            | 14 ++++++++++++++
 defaults/main.yml    | 13 +++++++++++++
 tasks/main.yml       |  5 +++++
 tasks/smtp_relay.yml | 30 ++++++++++++++++++++++++++++++
 5 files changed, 76 insertions(+), 1 deletion(-)
 create mode 100644 tasks/smtp_relay.yml

diff --git a/README-FR.md b/README-FR.md
index 29163da..f3dcbb5 100644
--- a/README-FR.md
+++ b/README-FR.md
@@ -31,7 +31,7 @@ ynh_install_script_url: https://install.yunohost.org
 ynh_admin_password: MYINSECUREPWD_PLZ_OVERRIDE_THIS
 ```
 
--`ynh_install_script_url` est le script d'installation des packages Yunohost, par défaut c'est le script officiel. Yunohost ne s'installe que sur Debian 10.
+- `ynh_install_script_url` est le script d'installation des packages Yunohost, par défaut c'est le script officiel. Yunohost ne s'installe que sur Debian 10.
 - `ynh_admin_password` est le mot de passe permettant d'accéder à l’interface d’administration du serveur.
 
 ### Gestion des domaines
@@ -49,6 +49,19 @@ ynh_ignore_dyndns_server: False
 - `ynh_extra_domains` sont des sous-domaines optionnels. Ils permettent d'installer une application par sous-domaine (plus d'infos [ici](https://yunohost.org/fr/dns_subdomains)).
 - `ynh_ignore_dyndns_server` permet d'enregistrer les domaines avec un service de DNS dynamique (plus d'infos [ici](https://yunohost.org/fr/dns_dynamicip)).
 
+### Configuration d'un relais SMTP
+
+```yml
+# paramètres personnalisés du relais SMTP
+ynh_smtp_relay:
+    host: smtp.domain.tld
+    port: 25
+    user: user1
+    password: Pa$$w0rd
+```
+Yunohost possède son propre serveur SMTP natif mais il est aussi possible de configurer Yunohost pour qu'il utilise un relais SMTP à la place. 
+Pour faire cela, créez la variable `ynh_smtp_relay` et mettez vos propres valeurs. Vous pouvez en apprendre plus sur les relais SMTP [ici](https://yunohost.org/fr/administrate/specific_use_cases/email_relay).
+
 ### Gestion des utilisateurs
 
 ```yml
diff --git a/README.md b/README.md
index 26b9441..4a82b8c 100644
--- a/README.md
+++ b/README.md
@@ -49,6 +49,20 @@ ynh_ignore_dyndns_server: False
 - `ynh_extra_domains` are optional and allow you to install one app per subdomain (more info [here](https://yunohost.org/en/administrate/specific_use_cases/domains/dns_subdomains)).
 - `ynh_ignore_dyndns_server` allow to register domains with a Dynamic DNS service (more info [here](https://yunohost.org/en/dns_dynamicip)).
 
+### SMTP relay configuration
+
+```yml
+# SMTP custom settings
+ynh_smtp_relay:
+    host: smtp.domain.tld
+    port: 25
+    user: user1
+    password: Pa$$w0rd
+```
+
+There is a built-in SMTP server on Yunohost but you can also set up Yunohost to use a SMTP relay instead.
+In order to do so, create the `ynh_smtp_relay` variable and provide your own values. You can learn more about SMTP relay [here](https://yunohost.org/en/administrate/specific_use_cases/email_relay).
+
 ### User management
 
 ```yml
diff --git a/defaults/main.yml b/defaults/main.yml
index 2971031..14b42ca 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -36,6 +36,19 @@ ynh_users: null
   #   lastname: Doe
   #   mail_domain: domain.tld
 
+# Do not touch this variable
+# Just to have dict default value
+ynh_smtp_relay:
+  value: null
+
+# SMTP custom settings (Only override if you need a SMTP relay)
+# Example:
+# ynh_smtp_relay:
+    # host: smtp.domain.tld
+    # port: "25"
+    # user: user1
+    # password: Pa$$w0rd
+
 # The list of Yunohost apps.
 ynh_apps: null
   # - label: Tiny Tiny RSS
diff --git a/tasks/main.yml b/tasks/main.yml
index e0f8b73..0480221 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -60,6 +60,11 @@
   ansible.builtin.command: yunohost domain cert-install
   changed_when: False
 
+- name: Configure SMTP relay
+  ansible.builtin.include_tasks: smtp_relay.yml
+  loop: "{{ ynh_smtp_relay | dict2items }}"
+  when: item.value
+
 - name: Add Yunohost users
   ansible.builtin.include_tasks: users.yml
   when: ynh_users
diff --git a/tasks/smtp_relay.yml b/tasks/smtp_relay.yml
new file mode 100644
index 0000000..8745870
--- /dev/null
+++ b/tasks/smtp_relay.yml
@@ -0,0 +1,30 @@
+---
+#-----------------------------------------------------------------------------#
+# ansible-yunohost allows to deploy Yunohost using Ansible                    #
+# Copyright 2021-2021 Lydra https://www.lydra.fr/                             #
+#                                                                             #
+# this program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# this program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program. If not, see <http://www.gnu.org/licenses/>.        #
+#                                                                             #
+#-----------------------------------------------------------------------------#
+
+- name: Get current SMTP settings
+  ansible.builtin.command:
+    "yunohost settings get smtp.relay.{{ item.key }}"
+  register: _ynh_smtp_current_values
+  changed_when: False
+
+- name: Set new SMTP settings
+  ansible.builtin.command:
+    "yunohost settings set smtp.relay.{{ item.key }} -v {{ item.value }}"
+  when: _ynh_smtp_current_values.stdout != item.value
-- 
GitLab