Skip to content

Commit 6d922a9

Browse files
author
Sharoon Thomas
committed
Merge pull request openlabs#11 from zikzakmedia/master
[IMP] Check emails is valid. If not valid, this email go to draft mailbox
2 parents 652f768 + 68ba89b commit 6d922a9

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

poweremail_mailbox.py

+23
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
from tools.translate import _
3232
import tools
3333

34+
import re
35+
3436
LOGGER = netsvc.Logger()
3537

3638
class PoweremailMailbox(osv.osv):
@@ -157,6 +159,27 @@ def complete_mail(self, cr, uid, ids, context=None):
157159
self.pool.get('poweremail.core_accounts').get_fullmail(cr, uid, id, context)
158160
self.historise(cr, uid, [id], "Full email downloaded", context)
159161

162+
def check_email_valid(self, email):
163+
"""Check if email is valid. Check @ and .
164+
:email str
165+
return True/False
166+
"""
167+
def get_validate_email(email):
168+
if not re.match(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$", email):
169+
return False
170+
return True
171+
172+
if not email:
173+
return False
174+
175+
emails = email.split(',')
176+
if len(emails)>0:
177+
for email in emails:
178+
if not get_validate_email(email):
179+
return False
180+
break
181+
return True
182+
160183
_columns = {
161184
'pem_from':fields.char(
162185
'From',

poweremail_send_wizard.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import netsvc
2929
import base64
3030
import time
31+
import re
3132
from tools.translate import _
3233
import tools
3334
from poweremail_template import get_value
@@ -190,8 +191,24 @@ def sav_to_drafts(self, cr, uid, ids, context=None):
190191
def send_mail(self, cr, uid, ids, context=None):
191192
if context is None:
192193
context = {}
194+
mailbox_obj = self.pool.get('poweremail.mailbox')
195+
values = {'folder':'outbox'}
196+
check_email = True
197+
193198
mailid = self.save_to_mailbox(cr, uid, ids, context)
194-
if self.pool.get('poweremail.mailbox').write(cr, uid, mailid, {'folder':'outbox'}, context):
199+
200+
if len(mailid)>0:
201+
mail = mailbox_obj.browse(cr, uid, mailid[0], context)
202+
check_email = mail.pem_to and mailbox_obj.check_email_valid(mail.pem_to) or False
203+
if mail.pem_cc:
204+
check_email = check_email and mailbox_obj.check_email_valid(mail.pem_cc)
205+
if mail.pem_bcc:
206+
check_email = check_email and mailbox_obj.check_email_valid(mail.pem_bcc)
207+
208+
if not check_email:
209+
values = {'folder':'drafts'}
210+
211+
if mailbox_obj.write(cr, uid, mailid, values, context):
195212
return {'type':'ir.actions.act_window_close' }
196213

197214
def get_generated(self, cr, uid, ids=None, context=None):

0 commit comments

Comments
 (0)