fixed email validation

This commit is contained in:
Anand Doshi 2012-12-13 18:34:36 +05:30
parent 1ba94dc81b
commit 58bfe5e131
2 changed files with 28 additions and 13 deletions

View file

@ -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

View file

@ -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 <notification@erpnext.com>')
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 <notification@erpnext.com>')
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"""