Cron mails with wrong recipient

Hi @mZoo, I finally found a solution for CRON emails:

This an example of an email that CRON sends:

From: root (Cron Daemon)
To: web
Subject: Cron <web@model> cd /srv/www/model/current && wp core update --minor --quiet && wp core update-db --quiet
MIME-Version: 1.0...

Neither “From” nor “To” are valid emails and we need to fix the two.

STEP 1 - “To” email: We need to add a MAILTO in cron file. Add this task in trellis/roles/letsencrypt/tasks/main.yml:

- name: Setup MAILTO for cronjob for key generation
  cronvar:
    cron_file: letsencrypt-certificate-renewal
    name: MAILTO
    value: "{{ mail_admin }}"
    state: present

Note: I use mail_admin defined at trellis/group_vars/all/mail.yml to be notified, you can use other email address.

STEP 2 - “From” email: Cron also has a MAILFROM var but is not supported on ubuntu 18 :pensive:
The only solution I found it’s to override /usr/sbin/sendmail executable to perform a From: replacement. I have integrated it into trellis in the following way:

Add a template file trellis/roles/ssmtp/templates/sendmail.j2 with this content:

# {{ ansible_managed }}

sed "s/From: root (Cron Daemon)/From: Cron Daemon <cron@{{ mail_hostname }}>/" | /usr/sbin/ssmtp $*

Note: I use cron@{{ mail_hostname }} as email address, you can use other email address.

Add the task to override sedmail on trellis/roles/ssmtp/tasks/main.yml:

- name: sendmail override for CRON emails
  template:
    src: sendmail.j2
    dest: /usr/sbin/sendmail
    mode: 0777

Now, you need to provision your server again to apply changes.

I’d be happy to open an issue on trellis repo as @swalkinshaw pointed here but I’d like to get some feedback from other users first.