How to send a warning when email didn't send

Hi,
I’m trying to send a Notification to the sender of an email to warn him that the email hasn’t been sent. When I try to save the Notification, I get:

Why is this document forbidden?

I also tried to create an event on the hooks.py on both ‘Email Queue’ and ‘Communication’, but none of them worked (on_submit, before_save).
The ‘Communication’ doctype doesn’t seem that helpful for this case since it doesn’t seem to trigger the ‘before_save’ when the Email Queue status changed.
My last option is to use a scheduler, but I would prefer using a hook.
Does anyone knows a way to do this using hooks?

I remember coming across this in version 10. I believe what we found was that the Email Queue document doesn’t have a valid owner (or maybe it was owned by Admin). IT was a long time ago and I do not know for sure, but it is worth looking into.

I remember wanting to do this also and my paid developer said it could not be done easily using the email queue so I abandoned it.

BKM

If it helps, I’ve done it using a scheduler. It send an email to the sender of the original email warning him that the email haven’t worked.
hook.py:

scheduler_events = {
    "hourly": [
        "app.tasks.send_warning_email"
    ],
}

tasks.py:

from frappe.utils import now_datetime
from frappe.email.doctype.email_template.email_template import get_email_template

def send_warning_email():
    curr_time = now_datetime().replace(hour=now_datetime().hour - 1)  # take every Email Queue since the last hour
    failed_email_queue = frappe.db.get_all('Email Queue', {'status':'Error', 'modified': ('>=', curr_time)}, ['name', 'error', 'sender'])
    for eq in failed_email_queue:
        doc = frappe.get_doc('Email Queue', eq.name)
        eq_recipient = frappe.db.get_all('Email Queue Recipient', {'parenttype': 'Email Queue', 'parent': eq.name},
                                               'recipient')

        frappe.sendmail(
            recipients=doc.sender,
            **get_email_template('Error while sending email', {'doc': doc, 'recipients': eq_recipient})
        )
1 Like

@mel_erp

Thanks for the workaround. To bad it had to be this complex (due to the doc not working). I will try this in my new v12 testing servers.

BKM