Repository: frappe/frappe

Issue:
Some emails do not send, in Error Log: "Communication 178befe608 not found"
Some emails do not send with error frappe.exceptions.DoesNotExistError: Communication 178befe608 not found`

Email sending unstabile: about 60% emails was sent without error, but 40% emails gets error “Communication 178befe608 not found”

I think the problem is in the sequence of scheduled tasks, sometimes Communication has time to be created and then this message can be sent by "Email Queue".
Sometimes the Communication doesn’t have time to be prepared and then another process “Email Queue” tries to send a Communication which hasn’t been created yet and then an error occurs.

**Error Log 1**
``` 
frappe.core.doctype.communication.email.sendmail
``` 
**Error Log 2**
``` 
20:52:26 Traceback (most recent call last):
  File "/home/erpnext/frappe-bench/env/lib/python3.8/site-packages/rq/worker.py", line 1013, in perform_job
    rv = job.perform()
  File "/home/erpnext/frappe-bench/env/lib/python3.8/site-packages/rq/job.py", line 709, in perform
    self._result = self._execute()
  File "/home/erpnext/frappe-bench/env/lib/python3.8/site-packages/rq/job.py", line 732, in _execute
    result = self.func(*self.args, **self.kwargs)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/utils/background_jobs.py", line 99, in execute_job
    method(**kwargs)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/core/doctype/communication/email.py", line 458, in sendmail
    communication = frappe.get_doc("Communication", communication_name)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/__init__.py", line 855, in get_doc
    doc = frappe.model.document.get_doc(*args, **kwargs)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py", line 75, in get_doc
    return controller(*args, **kwargs)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py", line 113, in __init__
    self.load_from_db()
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py", line 156, in load_from_db
    frappe.throw(_("{0} {1} not found").format(_(self.doctype), self.name), frappe.DoesNotExistError)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/__init__.py", line 437, in throw
    msgprint(msg, raise_exception=exc, title=title, indicator='red', is_minimizable=is_minimizable, wide=wide, as_list=as_list)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/__init__.py", line 416, in msgprint
    _raise_exception()
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/__init__.py", line 370, in _raise_exception
    raise raise_exception(msg)
frappe.exceptions.DoesNotExistError: Communication 178befe608 not found
```
**bench --site erp.sitename.com enable-scheduler:**

```
Enabled for erp.sitename.com
```

**bench doctor output:**
```
-----Checking scheduler status-----
Workers online: 3
-----erp.sitename.com Jobs-----
```
**How to reproduce error:**

1. Send ten e-mails in sequence one by one to some email address. (for email identification in the email subject specify the number like 1,2,3,4,5,6,7,8,9,10..
2. as a result:
6 e-mails will appear in "Email Queue" and will be delivered.
4 e-mails will not be delivered and will not appear in the "Email Queue". And an error message will appear in the "Error Log"



## Additional information

Ubuntu 20.04 / Frappe Easy install / ERPNext 13.10



Discussion:

Resolving patch:
diff --git a/frappe/core/doctype/communication/email.py b/frappe/core/doctype/communication/email.py
--- a/frappe/core/doctype/communication/email.py
+++ b/frappe/core/doctype/communication/email.py
@@ -85,6 +85,8 @@ def make(doctype=None, name=None, content=None, subject=None, sent_or_received =
 		add_attachments(comm.name, attachments)
 
 	if cint(send_email):
+		# Raise error if outgoing email account is missing
+		_ = frappe.email.smtp.get_outgoing_email_account(append_to=comm.doctype, sender=comm.sender)
 		frappe.flags.print_letterhead = cint(print_letterhead)
 		comm.send(print_html, print_format, attachments, send_me_a_copy=send_me_a_copy)
 
@@ -138,7 +140,7 @@ def notify(doc, print_html=None, print_format=None, attachments=None,
 			recipients=recipients, cc=cc, bcc=None)
 	else:
 		enqueue(sendmail, queue="default", timeout=300, event="sendmail",
-			communication_name=doc.name,
+			enqueue_after_commit=True, communication_name=doc.name,
 			print_html=print_html, print_format=print_format, attachments=attachments,
 			recipients=recipients, cc=cc, bcc=bcc, lang=frappe.local.lang,
 			session=frappe.local.session, print_letterhead=frappe.flags.print_letterhead)


Test patch: