From 21da2f572b8df82c79c45742d73e3a54750fae19 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Tue, 14 Aug 2018 14:11:41 +0300 Subject: [PATCH] Add email-sending support --- CHANGELOG.md | 10 ++++ docs/configuring-playbook-email.md | 21 +++++++ docs/configuring-playbook.md | 2 + roles/matrix-server/defaults/main.yml | 13 +++++ roles/matrix-server/tasks/main.yml | 5 ++ roles/matrix-server/tasks/setup_mailer.yml | 56 +++++++++++++++++++ roles/matrix-server/tasks/start.yml | 4 ++ .../matrix-server/templates/env/env-mailer.j2 | 8 +++ .../templates/synapse/homeserver.yaml.j2 | 30 +++++----- .../systemd/matrix-mailer.service.j2 | 20 +++++++ .../systemd/matrix-synapse.service.j2 | 3 + 11 files changed, 157 insertions(+), 15 deletions(-) create mode 100644 docs/configuring-playbook-email.md create mode 100644 roles/matrix-server/tasks/setup_mailer.yml create mode 100644 roles/matrix-server/templates/env/env-mailer.j2 create mode 100644 roles/matrix-server/templates/systemd/matrix-mailer.service.j2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b28c2f62..1956ac8e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# 2018-08-14 + +## Email-sending support + +The playbook now configures an email-sending service (postfix) by default. +Additional details are available in [Adjusting email-sending settings](docs/configuring-playbook-email.md). + +With this, Matrix Synapse is able to send email notifications for missed messages, etc. + + # 2018-08-08 diff --git a/docs/configuring-playbook-email.md b/docs/configuring-playbook-email.md new file mode 100644 index 000000000..b308b6b4b --- /dev/null +++ b/docs/configuring-playbook-email.md @@ -0,0 +1,21 @@ +# Adjusting email-sending settings (optional) + +By default, this playbook sets up a [postfix](http://www.postfix.org/) email server through which all Matrix services send emails. + +The email server would attempt to deliver emails directly to their final destination. +This may or may not work, depending on your domain configuration (SPF settings, etc.) + +By default, emails are sent from `matrix@` (as specified by the `matrix_mailer_sender_address` playbook variable). + +Furthmore, if you'd like to relay email through another SMTP server, feel free to redefine a few more playbook variables. +Example: + +```yaml +matrix_mailer_sender_address: "another.sender@example.com" +matrix_mailer_relay_use: true +matrix_mailer_relay_host_name: "mail.example.com" +matrix_mailer_relay_host_port: 587 +matrix_mailer_relay_auth: true +matrix_mailer_relay_auth_username: "another.sender@example.com" +matrix_mailer_relay_auth_password: "some-password" +``` \ No newline at end of file diff --git a/docs/configuring-playbook.md b/docs/configuring-playbook.md index 9b7d36b4b..682afa549 100644 --- a/docs/configuring-playbook.md +++ b/docs/configuring-playbook.md @@ -23,6 +23,8 @@ When you're done with all the configuration you'd like to do, continue with [Ins ## Other configuration options +- [Adjusting email-sending settings](configuring-playbook-email.md) (optional) + - [Storing Matrix media files on Amazon S3](configuring-playbook-s3.md) (optional) - [Using an external PostgreSQL server](configuring-playbook-external-postgres.md) (optional) diff --git a/roles/matrix-server/defaults/main.yml b/roles/matrix-server/defaults/main.yml index 85a042350..566743327 100644 --- a/roles/matrix-server/defaults/main.yml +++ b/roles/matrix-server/defaults/main.yml @@ -69,6 +69,7 @@ matrix_docker_image_riot: "avhost/docker-matrix-riot:v0.16.0" matrix_docker_image_s3fs: "xueshanf/s3fs:latest" matrix_docker_image_goofys: "cloudproto/goofys:latest" matrix_docker_image_coturn: "instrumentisto/coturn:4.5.0.7" +matrix_docker_image_mailer: "panubo/postfix:latest" # The Docker network that all services would be put into matrix_docker_network: "matrix" @@ -89,6 +90,18 @@ matrix_s3_media_store_aws_access_key: "your-aws-access-key" matrix_s3_media_store_aws_secret_key: "your-aws-secret-key" matrix_s3_media_store_region: "eu-central-1" +# By default, this playbook sets up a postfix mailer server (running in a container). +# This is so that Matrix Synapse can send email reminders for unread messages. +matrix_mailer_enabled: true + +matrix_mailer_sender_address: "matrix@{{ hostname_identity }}" +matrix_mailer_relay_use: false +matrix_mailer_relay_host_name: "mail.example.com" +matrix_mailer_relay_host_port: 587 +matrix_mailer_relay_auth: false +matrix_mailer_relay_auth_username: "" +matrix_mailer_relay_auth_password: "" + # By default, this playbook installs the Riot.IM web UI on the `hostname_riot` domain. # If you wish to connect to your Matrix server by other means, # you may wish to disable this. diff --git a/roles/matrix-server/tasks/main.yml b/roles/matrix-server/tasks/main.yml index 9a87b23dc..2808d0030 100644 --- a/roles/matrix-server/tasks/main.yml +++ b/roles/matrix-server/tasks/main.yml @@ -37,6 +37,11 @@ - setup-all - setup-coturn +- include: tasks/setup_mailer.yml + tags: + - setup-all + - setup-mailer + - include: tasks/setup_synapse.yml tags: - setup-all diff --git a/roles/matrix-server/tasks/setup_mailer.yml b/roles/matrix-server/tasks/setup_mailer.yml new file mode 100644 index 000000000..7f8df8d56 --- /dev/null +++ b/roles/matrix-server/tasks/setup_mailer.yml @@ -0,0 +1,56 @@ +--- + +# +# Tasks related to setting up the mailer +# + +- name: Ensure mailer environment variables file created + template: + src: "{{ role_path }}/templates/env/{{ item }}.j2" + dest: "{{ matrix_environment_variables_data_path }}/{{ item }}" + mode: 0640 + with_items: + - "env-mailer" + +- name: Ensure mailer image is pulled + docker_image: + name: "{{ matrix_docker_image_mailer }}" + when: matrix_mailer_enabled + +- name: Ensure matrix-mailer.service installed + template: + src: "{{ role_path }}/templates/systemd/matrix-mailer.service.j2" + dest: "/etc/systemd/system/matrix-mailer.service" + mode: 0644 + when: matrix_mailer_enabled + +# +# Tasks related to getting rid of the mailer (if it was previously enabled) +# + +- name: Check existence of matrix-mailer service + stat: path="/etc/systemd/system/matrix-mailer.service" + register: matrix_mailer_service_stat + +- name: Ensure matrix-mailer is stopped + service: name=matrix-mailer state=stopped daemon_reload=yes + register: stopping_result + when: "not matrix_mailer_enabled and matrix_mailer_service_stat.stat.exists" + +- name: Ensure matrix-mailer.service doesn't exist + file: + path: "/etc/systemd/system/matrix-mailer.service" + state: absent + when: "not matrix_mailer_enabled and matrix_mailer_service_stat.stat.exists" + +- name: Ensure Matrix mailer environment variables path doesn't exist + file: + path: "{{ matrix_environment_variables_data_path }}/env-mailer" + state: absent + when: "not matrix_mailer_enabled" + +- name: Ensure mailer Docker image doesn't exist + docker_image: + name: "{{ matrix_docker_image_mailer }}" + state: absent + when: "not matrix_mailer_enabled" \ No newline at end of file diff --git a/roles/matrix-server/tasks/start.yml b/roles/matrix-server/tasks/start.yml index 2ee6d2ba6..80469456b 100644 --- a/roles/matrix-server/tasks/start.yml +++ b/roles/matrix-server/tasks/start.yml @@ -11,6 +11,10 @@ - name: Ensure matrix-coturn autoruns and is restarted service: name=matrix-coturn enabled=yes state=restarted daemon_reload=yes +- name: Ensure matrix-mailer autoruns and is restarted + service: name=matrix-mailer enabled=yes state=restarted daemon_reload=yes + when: matrix_mailer_enabled + - name: Ensure matrix-synapse autoruns and is restarted service: name=matrix-synapse enabled=yes state=restarted daemon_reload=yes diff --git a/roles/matrix-server/templates/env/env-mailer.j2 b/roles/matrix-server/templates/env/env-mailer.j2 new file mode 100644 index 000000000..67f867e5d --- /dev/null +++ b/roles/matrix-server/templates/env/env-mailer.j2 @@ -0,0 +1,8 @@ +MAILNAME=matrix-mailer +{% if matrix_mailer_relay_use %} +RELAYHOST={{ matrix_mailer_relay_host_name }}:{{ matrix_mailer_relay_host_port }} +{% endif %} +{% if matrix_mailer_relay_auth %} +RELAYHOST_AUTH=yes +RELAYHOST_PASSWORDMAP={{ matrix_mailer_relay_host_name }}:{{ matrix_mailer_relay_auth_username }}:{{ matrix_mailer_relay_auth_password }} +{% endif %} \ No newline at end of file diff --git a/roles/matrix-server/templates/synapse/homeserver.yaml.j2 b/roles/matrix-server/templates/synapse/homeserver.yaml.j2 index 3f908aaec..fa0e396bd 100644 --- a/roles/matrix-server/templates/synapse/homeserver.yaml.j2 +++ b/roles/matrix-server/templates/synapse/homeserver.yaml.j2 @@ -86,7 +86,7 @@ web_client: False # web_client_location: "/path/to/web/root" # The public-facing base URL for the client API (not including _matrix/...) -# public_baseurl: https://example.com:8448/ +public_baseurl: https://{{ hostname_matrix }}/ # Set the soft limit on the number of file descriptors synapse can use # Zero is used to indicate synapse should set the soft limit to the @@ -563,20 +563,20 @@ password_config: # If your SMTP server requires authentication, the optional smtp_user & # smtp_pass variables should be used # -#email: -# enable_notifs: false -# smtp_host: "localhost" -# smtp_port: 25 -# smtp_user: "exampleusername" -# smtp_pass: "examplepassword" -# require_transport_security: False -# notif_from: "Your Friendly %(app)s Home Server " -# app_name: Matrix -# template_dir: res/templates -# notif_template_html: notif_mail.html -# notif_template_text: notif_mail.txt -# notif_for_new_users: True -# riot_base_url: "http://localhost/riot" +{% if matrix_mailer_enabled %} +email: + enable_notifs: true + smtp_host: "matrix-mailer" + smtp_port: 587 + require_transport_security: false + notif_from: "Matrix <{{ matrix_mailer_sender_address }}>" + app_name: Matrix + template_dir: /synapse/res/templates + notif_template_html: notif_mail.html + notif_template_text: notif_mail.txt + notif_for_new_users: True + riot_base_url: "https://{{ hostname_riot }}" +{% endif %} # password_providers: diff --git a/roles/matrix-server/templates/systemd/matrix-mailer.service.j2 b/roles/matrix-server/templates/systemd/matrix-mailer.service.j2 new file mode 100644 index 000000000..310d5502f --- /dev/null +++ b/roles/matrix-server/templates/systemd/matrix-mailer.service.j2 @@ -0,0 +1,20 @@ +[Unit] +Description=Matrix mailer +After=docker.service +Requires=docker.service + +[Service] +Type=simple +ExecStartPre=-/usr/bin/docker kill matrix-mailer +ExecStartPre=-/usr/bin/docker rm matrix-mailer +ExecStart=/usr/bin/docker run --rm --name matrix-mailer \ + --network={{ matrix_docker_network }} \ + --env-file={{ matrix_environment_variables_data_path }}/env-mailer \ + {{ matrix_docker_image_mailer }} +ExecStop=-/usr/bin/docker kill matrix-mailer +ExecStop=-/usr/bin/docker rm matrix-mailer +Restart=always +RestartSec=30 + +[Install] +WantedBy=multi-user.target diff --git a/roles/matrix-server/templates/systemd/matrix-synapse.service.j2 b/roles/matrix-server/templates/systemd/matrix-synapse.service.j2 index 4f76ef3f8..9918a21af 100644 --- a/roles/matrix-server/templates/systemd/matrix-synapse.service.j2 +++ b/roles/matrix-server/templates/systemd/matrix-synapse.service.j2 @@ -10,6 +10,9 @@ After=matrix-postgres.service After=matrix-goofys.service Requires=matrix-goofys.service {% endif %} +{% if matrix_mailer_enabled %} +Wants=matrix-mailer.service +{% endif %} Wants=matrix-coturn.service [Service]