Check email settings while saving
diff --git a/erpnext/setup/doctype/email_settings/email_settings.py b/erpnext/setup/doctype/email_settings/email_settings.py
index ef0a763..4c45a77 100644
--- a/erpnext/setup/doctype/email_settings/email_settings.py
+++ b/erpnext/setup/doctype/email_settings/email_settings.py
@@ -21,6 +21,82 @@
 		"""
 		if self.doc.fields.get(key):
 			webnotes.conn.set_value('Control Panel', None, key, self.doc.fields[key])
+
+	def validate(self):
+		"""
+			Checks connectivity to email servers before saving
+		"""
+		self.validate_outgoing()
+		self.validate_incoming()
+
+	
+	def validate_outgoing(self):
+		"""
+			Checks incoming email settings
+		"""
+		if self.doc.outgoing_mail_server:
+			from webnotes.utils import cint
+			import _socket
+			from webnotes.utils.email_lib.send import EMail
+			out_email = EMail()
+			out_email.server = self.doc.outgoing_mail_server.encode('utf-8')
+			out_email.port = cint(self.doc.mail_port)
+			out_email.use_ssl = self.doc.use_ssl
+			try:
+				out_email.login = self.doc.mail_login.encode('utf-8')
+				out_email.password =  self.doc.mail_password.encode('utf-8')
+			except AttributeError, e:
+				webnotes.msgprint('Login Id or Mail Password missing. Please enter and try again.')
+				webnotes.msgprint(e)
+			
+			try:
+				sess = out_email.smtp_connect()
+				try:
+					sess.quit()
+				except:
+					pass
+			except _socket.error, e:
+				# Invalid mail server -- due to refusing connection
+				webnotes.msgprint('Invalid Outgoing Mail Server. Please rectify and try again.')
+				webnotes.msgprint(e)
+			except smtplib.SMTPAuthenticationError, e:
+				webnotes.msgprint('Invalid Login Id or Mail Password. Please rectify and try again.')
+			except smtplib.SMTPException, e:
+				webnotes.msgprint('There is something wrong with your Outgoing Mail Settings. \
+				Please contact us at support@erpnext.com')
+				webnotes.msgprint(e)
+		
+
+	def validate_incoming(self):
+		"""
+			Checks support ticket email settings
+		"""
+		if self.doc.support_host:
+			from webnotes.utils.email_lib.receive import POP3Mailbox
+			from webnotes.model.doc import Document
+			import _socket, poplib
+			inc_email = Document('Incoming Email Settings')
+			inc_email.host = self.doc.support_host.encode('utf-8')
+			inc_email.use_ssl = self.doc.support_use_ssl
+			try:
+				inc_email.username = self.doc.support_username.encode('utf-8')
+				inc_email.password = self.doc.support_password.encode('utf-8')
+			except AttributeError, e:
+				webnotes.msgprint('User Name or Support Password missing. Please enter and try again.')
+				webnotes.msgprint(e)
+
+			pop_mb = POP3Mailbox(inc_email)
+			
+			try:
+				pop_mb.connect()
+			except _socket.error, e:
+				# Invalid mail server -- due to refusing connection
+				webnotes.msgprint('Invalid POP3 Mail Server. Please rectify and try again.')
+				webnotes.msgprint(e)
+			except poplib.error_proto, e:
+				webnotes.msgprint('Invalid User Name or Support Password. Please rectify and try again.')
+				webnotes.msgprint(e)
+
 		
 	def on_update(self):
 		"""
@@ -39,4 +115,4 @@
 			set_event('support.doctype.support_ticket.get_support_mails', 60*5, 1)
 		else:
 			from webnotes.utils.scheduler import cancel_event
-			cancel_event('support.doctype.support_ticket.get_support_mails')
\ No newline at end of file
+			cancel_event('support.doctype.support_ticket.get_support_mails')
diff --git a/erpnext/setup/doctype/email_settings/email_settings.txt b/erpnext/setup/doctype/email_settings/email_settings.txt
index ef6efe6..b6e52fc 100644
--- a/erpnext/setup/doctype/email_settings/email_settings.txt
+++ b/erpnext/setup/doctype/email_settings/email_settings.txt
@@ -5,18 +5,19 @@
 	{
 		'creation': '2010-08-08 17:08:59',
 		'docstatus': 0,
-		'modified': '2011-07-25 15:03:51',
+		'modified': '2012-01-25 18:44:45',
 		'modified_by': 'Administrator',
 		'owner': 'harshada@webnotestech.com'
 	},
 
 	# These values are common for all DocType
 	{
-		'_last_update': '1311586371',
+		'_last_update': '1325570647',
 		'allow_copy': 1,
 		'allow_email': 1,
 		'allow_print': 1,
 		'colour': 'White:FFF',
+		'default_print_format': 'Standard',
 		'doctype': 'DocType',
 		'in_create': 1,
 		'issingle': 1,
@@ -24,7 +25,7 @@
 		'name': '__common__',
 		'section_style': 'Simple',
 		'server_code_error': ' ',
-		'version': 34
+		'version': 35
 	},
 
 	# These values are common for all DocField
@@ -59,21 +60,18 @@
 
 	# DocPerm
 	{
-		'doctype': 'DocPerm',
-		'idx': 1
+		'doctype': 'DocPerm'
 	},
 
 	# DocPerm
 	{
-		'doctype': 'DocPerm',
-		'idx': 2
+		'doctype': 'DocPerm'
 	},
 
 	# DocField
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 1,
 		'label': 'Outgoing Mails'
 	},
 
@@ -81,7 +79,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'HTML',
-		'idx': 2,
 		'label': '1',
 		'options': '<div class="help_box">Set your outgoing mail settings here. All system generated notifications, emails will go from this mail server</div>'
 	},
@@ -91,7 +88,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'outgoing_mail_server',
 		'fieldtype': 'Data',
-		'idx': 3,
 		'label': 'Outgoing Mail Server'
 	},
 
@@ -99,8 +95,7 @@
 	{
 		'doctype': 'DocField',
 		'fieldname': 'mail_port',
-		'fieldtype': 'Data',
-		'idx': 4,
+		'fieldtype': 'Int',
 		'label': 'Mail Port'
 	},
 
@@ -109,7 +104,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'use_ssl',
 		'fieldtype': 'Check',
-		'idx': 5,
 		'label': 'Use SSL'
 	},
 
@@ -118,7 +112,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'mail_login',
 		'fieldtype': 'Data',
-		'idx': 6,
 		'label': 'Login Id'
 	},
 
@@ -127,7 +120,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'mail_password',
 		'fieldtype': 'Password',
-		'idx': 7,
 		'label': 'Mail Password'
 	},
 
@@ -136,7 +128,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'auto_email_id',
 		'fieldtype': 'Data',
-		'idx': 8,
 		'label': 'Auto Email Id'
 	},
 
@@ -145,7 +136,6 @@
 		'description': 'Set the POP3 email settings to pull emails directly from a mailbox and create Support Tickets',
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 9,
 		'label': 'Support Ticket Mail Settings'
 	},
 
@@ -153,7 +143,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'HTML',
-		'idx': 10,
 		'label': '2',
 		'options': '<div class="help_box">To automatically create Support Tickets from your incoming mail, set your pop3 settings here.</div>'
 	},
@@ -162,7 +151,6 @@
 	{
 		'doctype': 'DocField',
 		'fieldtype': 'Section Break',
-		'idx': 11,
 		'options': 'Simple'
 	},
 
@@ -173,7 +161,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'sync_support_mails',
 		'fieldtype': 'Check',
-		'idx': 12,
 		'label': 'Sync Support Mails'
 	},
 
@@ -184,7 +171,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'support_email',
 		'fieldtype': 'Data',
-		'idx': 13,
 		'label': 'Support Email'
 	},
 
@@ -195,7 +181,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'support_host',
 		'fieldtype': 'Data',
-		'idx': 14,
 		'label': 'POP3 Mail Server'
 	},
 
@@ -204,7 +189,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'support_use_ssl',
 		'fieldtype': 'Check',
-		'idx': 15,
 		'label': 'Use SSL'
 	},
 
@@ -213,7 +197,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'support_username',
 		'fieldtype': 'Data',
-		'idx': 16,
 		'label': 'User Name'
 	},
 
@@ -222,15 +205,13 @@
 		'doctype': 'DocField',
 		'fieldname': 'support_password',
 		'fieldtype': 'Password',
-		'idx': 17,
 		'label': 'Support Password'
 	},
 
 	# DocField
 	{
 		'doctype': 'DocField',
-		'fieldtype': 'Column Break',
-		'idx': 18
+		'fieldtype': 'Column Break'
 	},
 
 	# DocField
@@ -240,7 +221,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'support_signature',
 		'fieldtype': 'Text',
-		'idx': 19,
 		'label': 'Signature'
 	},
 
@@ -251,7 +231,6 @@
 		'doctype': 'DocField',
 		'fieldname': 'support_autoreply',
 		'fieldtype': 'Text',
-		'idx': 20,
 		'label': 'Autoreply'
 	}
 ]
\ No newline at end of file