We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
IMAP port settings are not updated from Email Domain to Email Account
## Description of the issue

When changing the IMAP port in an existing Email Domain, the Email Accounts using this Domain are not updated accordingly. This can lead to Frappe trying an IMAPS connection (which usually is 993) to the plain IMAP port 143, resulting in misleading error messages like `ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:852)`.

We could track down the root cause to the method `on_update` from the DocType "Email Domain": it simply misses the field `incoming_port` when copying data to all e-mail accounts that use this domain. This leads to the problem if the `incoming_port` is already set in the email account and gets updated/changed afterwards in the email domain.
## Context information (for bug reports)

```
frappe-bench$ bench --version
5.0.0

frappe-bench$ bench version
erpnext 12.11.2
frappe 12.9.1
```
## Steps to reproduce the issue

1. To reproduce this small bug you need to create a "Email Domain" in Frappe and save it with imap-port 143 and no SSL.
2. Create an e-mail account and link it with the domain from step 1 but without `Enable Incoming` and save.
3. Try to `Enable Incoming` and save
4. After "saving" the e-mail account go to the domain and change the imap-port from 143 to 993 and check SSL.
5. The `incoming_port` in Email-account is still 143.

### Observed result
In the database you can see that the `incoming_port` in the e-mail account is still 143 (real domain and mail addresses hidden):

```
select
    ea.email_id,
    ea.domain,
    ea.incoming_port,
    ed.incoming_port,
    ea.email_server,
    ed.email_server
from 
    `tabEmail Account` ea,
    `tabEmail Domain` ed
where ea.domain = ed.name
    and ed.name = "example.com";
```

#### Before updating the IMAP port in the domain
```
+------------------+-------------+---------------+---------------+--------------+--------------+
| email_id         | domain      | incoming_port | incoming_port | email_server | email_server |
+------------------+-------------+---------------+---------------+--------------+--------------+
| test@example.com | example.com | 143           | 143           | example.com  | example.com  |
+------------------+-------------+---------------+---------------+--------------+--------------+
1 row in set (0.000 sec)
```
#### After updating the IMAP port in the domain
```
+------------------+-------------+---------------+---------------+--------------+--------------+
| email_id         | domain      | incoming_port | incoming_port | email_server | email_server |
+------------------+-------------+---------------+---------------+--------------+--------------+
| test@example.com | example.com | 143           | 993           | example.com  | example.com  |
+------------------+-------------+---------------+---------------+--------------+--------------+
1 row in set (0.001 sec)
```
Now it will always trigger an SSL-handshake-error if the scheduler tries to get access.

### Expected result
When the mail domain gets updated all necessary fields related to e-mail account should be updated including the `incoming_port`.
### Stacktrace / full error message

```
Traceback (most recent call last):
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/app.py", line 64, in application
    response = frappe.api.handle()
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/api.py", line 59, in handle
    return frappe.handler.handle()
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/handler.py", line 24, in handle
    data = execute_cmd(cmd)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/handler.py", line 63, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/__init__.py", line 1055, in call
    return fn(*args, **newargs)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/desk/form/save.py", line 21, in savedocs
    doc.save()
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py", line 273, in save
    return self._save(*args, **kwargs)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py", line 309, in _save
    self.run_before_save_methods()
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py", line 896, in run_before_save_methods
    self.run_method("validate")
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py", line 797, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py", line 1073, in composer
    return composed(self, method, *args, **kwargs)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py", line 1056, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py", line 791, in <lambda>
    fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/email/doctype/email_account/email_account.py", line 68, in validate
    self.get_incoming_server()
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/email/doctype/email_account/email_account.py", line 168, in get_incoming_server
    email_server.connect()
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/email/receive.py", line 43, in connect
    return self.connect_imap()
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/email/receive.py", line 51, in connect_imap
    self.imap = Timed_IMAP4_SSL(self.settings.host, self.settings.incoming_port, timeout=frappe.conf.get("pop_timeout"))
  File "/home/erpnext/frappe-bench/apps/frappe/frappe/email/receive.py", line 564, in __init__
    self._super.__init__(self, *args, **kwargs)
  File "/usr/lib/python3.6/imaplib.py", line 1288, in __init__
    IMAP4.__init__(self, host, port)
  File "/usr/lib/python3.6/imaplib.py", line 198, in __init__
    self.open(host, port)
  File "/usr/lib/python3.6/imaplib.py", line 1301, in open
    IMAP4.open(self, host, port)
  File "/usr/lib/python3.6/imaplib.py", line 299, in open
    self.sock = self._create_socket()
  File "/usr/lib/python3.6/imaplib.py", line 1293, in _create_socket
    server_hostname=self.host)
  File "/usr/lib/python3.6/ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "/usr/lib/python3.6/ssl.py", line 817, in __init__
    self.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 1077, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:852)
```

## OS
- Linux Ubuntu 18.04


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `frappe/email/doctype/email_domain/email_domain.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and contributors
3 # For license information, please see license.txt
4 
5 from __future__ import unicode_literals
6 import frappe
7 from frappe import _
8 from frappe.model.document import Document
9 from frappe.utils import validate_email_address ,cint, cstr
10 import imaplib,poplib,smtplib
11 from frappe.email.utils import get_port
12 
13 class EmailDomain(Document):
14 	def autoname(self):
15 		if self.domain_name:
16 			self.name = self.domain_name
17 
18 	def validate(self):
19 		"""Validate email id and check POP3/IMAP and SMTP connections is enabled."""
20 		if self.email_id:
21 			validate_email_address(self.email_id, True)
22 
23 		if frappe.local.flags.in_patch or frappe.local.flags.in_test:
24 			return
25 
26 		if not frappe.local.flags.in_install and not frappe.local.flags.in_patch:
27 			try:
28 				if self.use_imap:
29 					if self.use_ssl:
30 						test = imaplib.IMAP4_SSL(self.email_server, port=get_port(self))
31 					else:
32 						test = imaplib.IMAP4(self.email_server, port=get_port(self))
33 
34 				else:
35 					if self.use_ssl:
36 						test = poplib.POP3_SSL(self.email_server, port=get_port(self))
37 					else:
38 						test = poplib.POP3(self.email_server, port=get_port(self))
39 
40 			except Exception:
41 				frappe.throw(_("Incoming email account not correct"))
42 
43 			finally:
44 				try:
45 					if self.use_imap:
46 						test.logout()
47 					else:
48 						test.quit()
49 				except Exception:
50 					pass
51 
52 			try:
53 				if self.get('use_ssl_for_outgoing'):
54 					if not self.get('smtp_port'):
55 						self.smtp_port = 465
56 
57 					sess = smtplib.SMTP_SSL((self.smtp_server or "").encode('utf-8'),
58 							cint(self.smtp_port) or None)
59 				else:
60 					if self.use_tls and not self.smtp_port:
61 						self.smtp_port = 587
62 					sess = smtplib.SMTP(cstr(self.smtp_server or ""), cint(self.smtp_port) or None)
63 				sess.quit()
64 			except Exception:
65 				frappe.throw(_("Outgoing email account not correct"))
66 
67 	def on_update(self):
68 		"""update all email accounts using this domain"""
69 		for email_account in frappe.get_all("Email Account", filters={"domain": self.name}):
70 			try:
71 				email_account = frappe.get_doc("Email Account", email_account.name)
72 				for attr in ["email_server", "use_imap", "use_ssl", "use_tls", "attachment_limit", "smtp_server", "smtp_port", "use_ssl_for_outgoing", "append_emails_to_sent_folder"]:
73 					email_account.set(attr, self.get(attr, default=0))
74 				email_account.save()
75 
76 			except Exception as e:
77 				frappe.msgprint(_("Error has occurred in {0}").format(email_account.name), raise_exception=e.__class__)
78
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/frappe/email/doctype/email_domain/email_domain.py b/frappe/email/doctype/email_domain/email_domain.py
--- a/frappe/email/doctype/email_domain/email_domain.py
+++ b/frappe/email/doctype/email_domain/email_domain.py
@@ -69,7 +69,7 @@
 		for email_account in frappe.get_all("Email Account", filters={"domain": self.name}):
 			try:
 				email_account = frappe.get_doc("Email Account", email_account.name)
-				for attr in ["email_server", "use_imap", "use_ssl", "use_tls", "attachment_limit", "smtp_server", "smtp_port", "use_ssl_for_outgoing", "append_emails_to_sent_folder"]:
+				for attr in ["email_server", "use_imap", "use_ssl", "use_tls", "attachment_limit", "smtp_server", "smtp_port", "use_ssl_for_outgoing", "append_emails_to_sent_folder", "incoming_port"]:
 					email_account.set(attr, self.get(attr, default=0))
 				email_account.save()

{"golden_diff": "diff --git a/frappe/email/doctype/email_domain/email_domain.py b/frappe/email/doctype/email_domain/email_domain.py\n--- a/frappe/email/doctype/email_domain/email_domain.py\n+++ b/frappe/email/doctype/email_domain/email_domain.py\n@@ -69,7 +69,7 @@\n \t\tfor email_account in frappe.get_all(\"Email Account\", filters={\"domain\": self.name}):\n \t\t\ttry:\n \t\t\t\temail_account = frappe.get_doc(\"Email Account\", email_account.name)\n-\t\t\t\tfor attr in [\"email_server\", \"use_imap\", \"use_ssl\", \"use_tls\", \"attachment_limit\", \"smtp_server\", \"smtp_port\", \"use_ssl_for_outgoing\", \"append_emails_to_sent_folder\"]:\n+\t\t\t\tfor attr in [\"email_server\", \"use_imap\", \"use_ssl\", \"use_tls\", \"attachment_limit\", \"smtp_server\", \"smtp_port\", \"use_ssl_for_outgoing\", \"append_emails_to_sent_folder\", \"incoming_port\"]:\n \t\t\t\t\temail_account.set(attr, self.get(attr, default=0))\n \t\t\t\temail_account.save()\n", "issue": "IMAP port settings are not updated from Email Domain to Email Account\n## Description of the issue\r\n\r\nWhen changing the IMAP port in an existing Email Domain, the Email Accounts using this Domain are not updated accordingly. This can lead to Frappe trying an IMAPS connection (which usually is 993) to the plain IMAP port 143, resulting in misleading error messages like `ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:852)`.\r\n\r\nWe could track down the root cause to the method `on_update` from the DocType \"Email Domain\": it simply misses the field `incoming_port` when copying data to all e-mail accounts that use this domain. This leads to the problem if the `incoming_port` is already set in the email account and gets updated/changed afterwards in the email domain.\r\n## Context information (for bug reports)\r\n\r\n```\r\nfrappe-bench$ bench --version\r\n5.0.0\r\n\r\nfrappe-bench$ bench version\r\nerpnext 12.11.2\r\nfrappe 12.9.1\r\n```\r\n## Steps to reproduce the issue\r\n\r\n1. To reproduce this small bug you need to create a \"Email Domain\" in Frappe and save it with imap-port 143 and no SSL.\r\n2. Create an e-mail account and link it with the domain from step 1 but without `Enable Incoming` and save.\r\n3. Try to `Enable Incoming` and save\r\n4. After \"saving\" the e-mail account go to the domain and change the imap-port from 143 to 993 and check SSL.\r\n5. The `incoming_port` in Email-account is still 143.\r\n\r\n### Observed result\r\nIn the database you can see that the `incoming_port` in the e-mail account is still 143 (real domain and mail addresses hidden):\r\n\r\n```\r\nselect\r\n    ea.email_id,\r\n    ea.domain,\r\n    ea.incoming_port,\r\n    ed.incoming_port,\r\n    ea.email_server,\r\n    ed.email_server\r\nfrom \r\n    `tabEmail Account` ea,\r\n    `tabEmail Domain` ed\r\nwhere ea.domain = ed.name\r\n    and ed.name = \"example.com\";\r\n```\r\n\r\n#### Before updating the IMAP port in the domain\r\n```\r\n+------------------+-------------+---------------+---------------+--------------+--------------+\r\n| email_id         | domain      | incoming_port | incoming_port | email_server | email_server |\r\n+------------------+-------------+---------------+---------------+--------------+--------------+\r\n| test@example.com | example.com | 143           | 143           | example.com  | example.com  |\r\n+------------------+-------------+---------------+---------------+--------------+--------------+\r\n1 row in set (0.000 sec)\r\n```\r\n#### After updating the IMAP port in the domain\r\n```\r\n+------------------+-------------+---------------+---------------+--------------+--------------+\r\n| email_id         | domain      | incoming_port | incoming_port | email_server | email_server |\r\n+------------------+-------------+---------------+---------------+--------------+--------------+\r\n| test@example.com | example.com | 143           | 993           | example.com  | example.com  |\r\n+------------------+-------------+---------------+---------------+--------------+--------------+\r\n1 row in set (0.001 sec)\r\n```\r\nNow it will always trigger an SSL-handshake-error if the scheduler tries to get access.\r\n\r\n### Expected result\r\nWhen the mail domain gets updated all necessary fields related to e-mail account should be updated including the `incoming_port`.\r\n### Stacktrace / full error message\r\n\r\n```\r\nTraceback (most recent call last):\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/app.py\", line 64, in application\r\n    response = frappe.api.handle()\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/api.py\", line 59, in handle\r\n    return frappe.handler.handle()\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/handler.py\", line 24, in handle\r\n    data = execute_cmd(cmd)\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/handler.py\", line 63, in execute_cmd\r\n    return frappe.call(method, **frappe.form_dict)\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/__init__.py\", line 1055, in call\r\n    return fn(*args, **newargs)\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/desk/form/save.py\", line 21, in savedocs\r\n    doc.save()\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py\", line 273, in save\r\n    return self._save(*args, **kwargs)\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py\", line 309, in _save\r\n    self.run_before_save_methods()\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py\", line 896, in run_before_save_methods\r\n    self.run_method(\"validate\")\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py\", line 797, in run_method\r\n    out = Document.hook(fn)(self, *args, **kwargs)\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py\", line 1073, in composer\r\n    return composed(self, method, *args, **kwargs)\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py\", line 1056, in runner\r\n    add_to_return_value(self, fn(self, *args, **kwargs))\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/model/document.py\", line 791, in <lambda>\r\n    fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/email/doctype/email_account/email_account.py\", line 68, in validate\r\n    self.get_incoming_server()\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/email/doctype/email_account/email_account.py\", line 168, in get_incoming_server\r\n    email_server.connect()\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/email/receive.py\", line 43, in connect\r\n    return self.connect_imap()\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/email/receive.py\", line 51, in connect_imap\r\n    self.imap = Timed_IMAP4_SSL(self.settings.host, self.settings.incoming_port, timeout=frappe.conf.get(\"pop_timeout\"))\r\n  File \"/home/erpnext/frappe-bench/apps/frappe/frappe/email/receive.py\", line 564, in __init__\r\n    self._super.__init__(self, *args, **kwargs)\r\n  File \"/usr/lib/python3.6/imaplib.py\", line 1288, in __init__\r\n    IMAP4.__init__(self, host, port)\r\n  File \"/usr/lib/python3.6/imaplib.py\", line 198, in __init__\r\n    self.open(host, port)\r\n  File \"/usr/lib/python3.6/imaplib.py\", line 1301, in open\r\n    IMAP4.open(self, host, port)\r\n  File \"/usr/lib/python3.6/imaplib.py\", line 299, in open\r\n    self.sock = self._create_socket()\r\n  File \"/usr/lib/python3.6/imaplib.py\", line 1293, in _create_socket\r\n    server_hostname=self.host)\r\n  File \"/usr/lib/python3.6/ssl.py\", line 407, in wrap_socket\r\n    _context=self, _session=session)\r\n  File \"/usr/lib/python3.6/ssl.py\", line 817, in __init__\r\n    self.do_handshake()\r\n  File \"/usr/lib/python3.6/ssl.py\", line 1077, in do_handshake\r\n    self._sslobj.do_handshake()\r\n  File \"/usr/lib/python3.6/ssl.py\", line 689, in do_handshake\r\n    self._sslobj.do_handshake()\r\nssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:852)\r\n```\r\n\r\n## OS\r\n- Linux Ubuntu 18.04\r\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and contributors\n# For license information, please see license.txt\n\nfrom __future__ import unicode_literals\nimport frappe\nfrom frappe import _\nfrom frappe.model.document import Document\nfrom frappe.utils import validate_email_address ,cint, cstr\nimport imaplib,poplib,smtplib\nfrom frappe.email.utils import get_port\n\nclass EmailDomain(Document):\n\tdef autoname(self):\n\t\tif self.domain_name:\n\t\t\tself.name = self.domain_name\n\n\tdef validate(self):\n\t\t\"\"\"Validate email id and check POP3/IMAP and SMTP connections is enabled.\"\"\"\n\t\tif self.email_id:\n\t\t\tvalidate_email_address(self.email_id, True)\n\n\t\tif frappe.local.flags.in_patch or frappe.local.flags.in_test:\n\t\t\treturn\n\n\t\tif not frappe.local.flags.in_install and not frappe.local.flags.in_patch:\n\t\t\ttry:\n\t\t\t\tif self.use_imap:\n\t\t\t\t\tif self.use_ssl:\n\t\t\t\t\t\ttest = imaplib.IMAP4_SSL(self.email_server, port=get_port(self))\n\t\t\t\t\telse:\n\t\t\t\t\t\ttest = imaplib.IMAP4(self.email_server, port=get_port(self))\n\n\t\t\t\telse:\n\t\t\t\t\tif self.use_ssl:\n\t\t\t\t\t\ttest = poplib.POP3_SSL(self.email_server, port=get_port(self))\n\t\t\t\t\telse:\n\t\t\t\t\t\ttest = poplib.POP3(self.email_server, port=get_port(self))\n\n\t\t\texcept Exception:\n\t\t\t\tfrappe.throw(_(\"Incoming email account not correct\"))\n\n\t\t\tfinally:\n\t\t\t\ttry:\n\t\t\t\t\tif self.use_imap:\n\t\t\t\t\t\ttest.logout()\n\t\t\t\t\telse:\n\t\t\t\t\t\ttest.quit()\n\t\t\t\texcept Exception:\n\t\t\t\t\tpass\n\n\t\t\ttry:\n\t\t\t\tif self.get('use_ssl_for_outgoing'):\n\t\t\t\t\tif not self.get('smtp_port'):\n\t\t\t\t\t\tself.smtp_port = 465\n\n\t\t\t\t\tsess = smtplib.SMTP_SSL((self.smtp_server or \"\").encode('utf-8'),\n\t\t\t\t\t\t\tcint(self.smtp_port) or None)\n\t\t\t\telse:\n\t\t\t\t\tif self.use_tls and not self.smtp_port:\n\t\t\t\t\t\tself.smtp_port = 587\n\t\t\t\t\tsess = smtplib.SMTP(cstr(self.smtp_server or \"\"), cint(self.smtp_port) or None)\n\t\t\t\tsess.quit()\n\t\t\texcept Exception:\n\t\t\t\tfrappe.throw(_(\"Outgoing email account not correct\"))\n\n\tdef on_update(self):\n\t\t\"\"\"update all email accounts using this domain\"\"\"\n\t\tfor email_account in frappe.get_all(\"Email Account\", filters={\"domain\": self.name}):\n\t\t\ttry:\n\t\t\t\temail_account = frappe.get_doc(\"Email Account\", email_account.name)\n\t\t\t\tfor attr in [\"email_server\", \"use_imap\", \"use_ssl\", \"use_tls\", \"attachment_limit\", \"smtp_server\", \"smtp_port\", \"use_ssl_for_outgoing\", \"append_emails_to_sent_folder\"]:\n\t\t\t\t\temail_account.set(attr, self.get(attr, default=0))\n\t\t\t\temail_account.save()\n\n\t\t\texcept Exception as e:\n\t\t\t\tfrappe.msgprint(_(\"Error has occurred in {0}\").format(email_account.name), raise_exception=e.__class__)\n", "path": "frappe/email/doctype/email_domain/email_domain.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and contributors\n# For license information, please see license.txt\n\nfrom __future__ import unicode_literals\nimport frappe\nfrom frappe import _\nfrom frappe.model.document import Document\nfrom frappe.utils import validate_email_address ,cint, cstr\nimport imaplib,poplib,smtplib\nfrom frappe.email.utils import get_port\n\nclass EmailDomain(Document):\n\tdef autoname(self):\n\t\tif self.domain_name:\n\t\t\tself.name = self.domain_name\n\n\tdef validate(self):\n\t\t\"\"\"Validate email id and check POP3/IMAP and SMTP connections is enabled.\"\"\"\n\t\tif self.email_id:\n\t\t\tvalidate_email_address(self.email_id, True)\n\n\t\tif frappe.local.flags.in_patch or frappe.local.flags.in_test:\n\t\t\treturn\n\n\t\tif not frappe.local.flags.in_install and not frappe.local.flags.in_patch:\n\t\t\ttry:\n\t\t\t\tif self.use_imap:\n\t\t\t\t\tif self.use_ssl:\n\t\t\t\t\t\ttest = imaplib.IMAP4_SSL(self.email_server, port=get_port(self))\n\t\t\t\t\telse:\n\t\t\t\t\t\ttest = imaplib.IMAP4(self.email_server, port=get_port(self))\n\n\t\t\t\telse:\n\t\t\t\t\tif self.use_ssl:\n\t\t\t\t\t\ttest = poplib.POP3_SSL(self.email_server, port=get_port(self))\n\t\t\t\t\telse:\n\t\t\t\t\t\ttest = poplib.POP3(self.email_server, port=get_port(self))\n\n\t\t\texcept Exception:\n\t\t\t\tfrappe.throw(_(\"Incoming email account not correct\"))\n\n\t\t\tfinally:\n\t\t\t\ttry:\n\t\t\t\t\tif self.use_imap:\n\t\t\t\t\t\ttest.logout()\n\t\t\t\t\telse:\n\t\t\t\t\t\ttest.quit()\n\t\t\t\texcept Exception:\n\t\t\t\t\tpass\n\n\t\t\ttry:\n\t\t\t\tif self.get('use_ssl_for_outgoing'):\n\t\t\t\t\tif not self.get('smtp_port'):\n\t\t\t\t\t\tself.smtp_port = 465\n\n\t\t\t\t\tsess = smtplib.SMTP_SSL((self.smtp_server or \"\").encode('utf-8'),\n\t\t\t\t\t\t\tcint(self.smtp_port) or None)\n\t\t\t\telse:\n\t\t\t\t\tif self.use_tls and not self.smtp_port:\n\t\t\t\t\t\tself.smtp_port = 587\n\t\t\t\t\tsess = smtplib.SMTP(cstr(self.smtp_server or \"\"), cint(self.smtp_port) or None)\n\t\t\t\tsess.quit()\n\t\t\texcept Exception:\n\t\t\t\tfrappe.throw(_(\"Outgoing email account not correct\"))\n\n\tdef on_update(self):\n\t\t\"\"\"update all email accounts using this domain\"\"\"\n\t\tfor email_account in frappe.get_all(\"Email Account\", filters={\"domain\": self.name}):\n\t\t\ttry:\n\t\t\t\temail_account = frappe.get_doc(\"Email Account\", email_account.name)\n\t\t\t\tfor attr in [\"email_server\", \"use_imap\", \"use_ssl\", \"use_tls\", \"attachment_limit\", \"smtp_server\", \"smtp_port\", \"use_ssl_for_outgoing\", \"append_emails_to_sent_folder\", \"incoming_port\"]:\n\t\t\t\t\temail_account.set(attr, self.get(attr, default=0))\n\t\t\t\temail_account.save()\n\n\t\t\texcept Exception as e:\n\t\t\t\tfrappe.msgprint(_(\"Error has occurred in {0}\").format(email_account.name), raise_exception=e.__class__)\n", "path": "frappe/email/doctype/email_domain/email_domain.py"}]}