# Buggy code
def on_update(self):
    """update all email accounts using this domain"""
    for email_account in frappe.get_all("Email Account", filters={"domain": self.name}):
        try:
            email_account = frappe.get_doc("Email Account", email_account.name)
            email_account.set("email_server", self.email_server)
            email_account.set("use_imap", self.use_imap)
            email_account.set("use_ssl", self.use_ssl)
            email_account.set("use_tls", self.use_tls)
            email_account.set("attachment_limit", self.attachment_limit)
            email_account.set("smtp_server", self.smtp_server)
            email_account.set("smtp_port", self.smtp_port)
            email_account.save()
        except Exception as e:
            frappe.msgprint(email_account.name)
            frappe.throw(e)
            return None


# Traceback (ssl.SSLError)
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)

# Fixed code
def on_update(self):
    """update all email accounts using this domain"""
    for email_account in frappe.get_all("Email Account", filters={"domain": self.name}):
        try:
            email_account = frappe.get_doc("Email Account", email_account.name)
            email_account.set("email_server", self.email_server)
            email_account.set("use_imap", self.use_imap)
            email_account.set("use_ssl", self.use_ssl)
            email_account.set("use_tls", self.use_tls)
            email_account.set("attachment_limit", self.attachment_limit)
            email_account.set("smtp_server", self.smtp_server)
            email_account.set("smtp_port", self.smtp_port)
            email_account.set("incoming_port", self.incoming_port)
            email_account.save()
        except Exception as e:
            frappe.msgprint(email_account.name)
            frappe.throw(e)
            return None