---
# tasks file for mail
#
- name: Install postfix
  ansible.builtin.apt:
    pkg:
    - postfix
    - libsasl2-modules
    state: latest

- name: (postconf) relay configuration
  ansible.builtin.command:
    cmd: postconf "{{ item }}"
  loop:
    - relayhost = "{{ smtp_hostname }}:{{ smtp_port }}"
    - smtp_sasl_auth_enable = yes
    - smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    - smtp_sasl_security_options = noanonymous
    - header_size_limit = 4096000
    - smtp_use_tls = yes
    - smtp_tls_security_level=may
  notify:
    - restart postfix

- name: Create file for sasl password
  ansible.builtin.file:
    path: /etc/postfix/sasl_passwd
    state: touch
    owner: root
    group: root

- name: Add credentials in sasl-passwd file
  ansible.builtin.lineinfile:
    path: /etc/postfix/sasl_passwd
    line: '{{ smtp_hostname }}:{{ smtp_port }}  {{ api_key}}:{{ secret_key }}'
  notify:
   - restart postfix

- name: (postmap) relay configuration
  ansible.builtin.command:
    cmd: postmap /etc/postfix/sasl_passwd
  notify:
    - restart postfix

- name: Change permissions for sasl_passwd
  ansible.builtin.file:
    path:  /etc/postfix/sasl_passwd
    mode: '0600'

- name: Change permissions for sasl_passwd.db
  ansible.builtin.file:
    path:  /etc/postfix/sasl_passwd.db
    mode: '0600'

- name: Flush handlers
  meta: flush_handlers
