From 58bfe5e131b2ff3d9b8b58355639b8a641ab622e Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 13 Dec 2012 18:34:36 +0530 Subject: [PATCH] fixed email validation --- webnotes/utils/__init__.py | 6 ++++++ webnotes/utils/email_lib/smtp.py | 35 ++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/webnotes/utils/__init__.py b/webnotes/utils/__init__.py index 4b17c241ac..dcec500158 100644 --- a/webnotes/utils/__init__.py +++ b/webnotes/utils/__init__.py @@ -79,6 +79,12 @@ def get_email_id(user): fullname = get_fullname(user) return formataddr((fullname, user)) +def extract_email_id(email): + """fetch only the email part of the email id""" + import re + sender_email = re.findall("<([^>]*)>", email) + return sender_email and sender_email[0] or "" + def validate_email_add(email_str): """Validates the email string""" from email.utils import parseaddr diff --git a/webnotes/utils/email_lib/smtp.py b/webnotes/utils/email_lib/smtp.py index 1fa31ce1ed..4bd673b38f 100644 --- a/webnotes/utils/email_lib/smtp.py +++ b/webnotes/utils/email_lib/smtp.py @@ -185,21 +185,30 @@ class EMail: def validate(self): """validate the email ids""" + from webnotes.utils import validate_email_add, extract_email_id + def _validate(email): + """validate an email field""" + if email: + if not validate_email_add(email): + # try extracting the email part and set as sender + new_email = extract_email_id(email) + if not (new_email and validate_email_add(new_email)): + webnotes.msgprint("%s is not a valid email id" % email, + raise_exception = 1) + email = new_email + return email + if not self.sender: - self.sender = webnotes.conn.get_value('Email Settings', None, 'auto_email_id') \ - or getattr(conf, 'auto_email_id', 'ERPNext Notification ') - - from webnotes.utils import validate_email_add - # validate ids - if self.sender and (not validate_email_add(self.sender)): - webnotes.msgprint("%s is not a valid email id" % self.sender, raise_exception = 1) - - if self.reply_to and (not validate_email_add(self.reply_to)): - webnotes.msgprint("%s is not a valid email id" % self.reply_to, raise_exception = 1) - + # TODO: remove erpnext id + self.sender = webnotes.conn.get_value('Email Settings', None, + 'auto_email_id') or getattr(conf, 'auto_email_id', + 'ERPNext Notification ') + + self.sender = _validate(self.sender) + self.reply_to = _validate(self.reply_to) + for e in self.recipients + (self.cc or []): - if e.strip() and not validate_email_add(e): - webnotes.msgprint("%s is not a valid email id" % e, raise_exception = 1) + _validate(e.strip()) def make(self): """build into msg_root"""