From 4d1a89e006ef3cfa2b6b1b518f744857a76f06ed Mon Sep 17 00:00:00 2001 From: "Turkesh Patel (Open ERP)" Date: Thu, 21 Jun 2012 11:44:05 +0530 Subject: [PATCH 1/3] [ADD] added message_catchall method. bzr revid: tpa@tinyerp.com-20120621061405-uofftetkrptjo6mu --- addons/mail/mail_thread.py | 58 +++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index 40824ba5427..08eaad8d139 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -447,11 +447,61 @@ class mail_thread(osv.osv): return msgs - def message_catchall(self, cr, uid, message, context=None): - """TODO proper docstring, inspired by messsage_process()""" - # TODO - pass + def _get_user(self, cr, uid, alias, context): + """ + return user_id based on alias + else return id of admin + """ + user_obj = self.pool.get('res.user') + if alias.alias_user_id: + user_id = alias_id.alias_user_id.id + #if user_id not defined in the alias then search related user using name of Email sender + else if: + frm = msg.get('from') + user_ids = user_obj.search(cr, uid, [('name','=',frm)], context) + if user_ids: + user_id = user_obj.browse(cr, uid, user_ids[0], context).id + return user_id or 1 + + def message_catchall(self, cr, uid, message, context=None): + """ + Process incoming mail and call messsage_process using details of the mail.alias model + else raise Exception so that mailgate script will reject the mail and + send notification mail sender that this mailbox does not exist so your mail have been rejected. + """ + + alias_obj = self.pool.get('mail.alias') + user_obj = self.pool.get('res.user') + mail_message = self.pool.get('mail.compose.message') + + if isinstance(message, xmlrpclib.Binary): + message = str(message.data) + + # Parse Message + # Warning: message_from_string doesn't always work correctly on unicode, + # we must use utf-8 strings here :-( + if isinstance(message, unicode): + message = message.encode('utf-8') + msg_txt = email.message_from_string(message) + msg = mail_message.parse_message(msg_txt, save_original=save_original) + + alias_name = msg.get('to') + alias_ids = mail_alias.search(cr, uid, [('alias_name','=',alias_name)],context) + alias_id = mail_alias.browse(cr, uid, alias_ids[0], context) + #if alias found then call message_process method. + if alias_id: + user_id = self._get_user(self, cr, uid, alias_id, context) + self.message_process(self, cr, user_id, alias_id.alias_model_id.id, message, alias_id.alias_defaults or False , False, False, alias_id.alias_force_thread_id or False, context) + #if alis not found give Exception + else: + _logger.warning("This mailbox does not exist so mail gate will reject this mail.") + frm = user_obj.browse(cr, uid, uid, context).user_email + sub = "Mail Rejection" + msg.get('subject') + message = "Respective mailbox does not exist so your mail have been rejected" + msg + mail_message.send_mail(cr, uid, {'email_from': frm,'email_to': msg.get('from'),'subject': sub, 'body_text': message}, context) + + return True #------------------------------------------------------ # Email specific From f64fa15ec072c06001fdc199f965e35bfca8dd2c Mon Sep 17 00:00:00 2001 From: "Turkesh Patel (Open ERP)" Date: Thu, 21 Jun 2012 11:53:33 +0530 Subject: [PATCH 2/3] [IMP] improved code bzr revid: tpa@tinyerp.com-20120621062333-qv7hblle9l6huch1 --- addons/mail/mail_thread.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index 08eaad8d139..644b52df2db 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -457,7 +457,7 @@ class mail_thread(osv.osv): if alias.alias_user_id: user_id = alias_id.alias_user_id.id #if user_id not defined in the alias then search related user using name of Email sender - else if: + else: frm = msg.get('from') user_ids = user_obj.search(cr, uid, [('name','=',frm)], context) if user_ids: From ca0509b340c89b2e507ee9dbb5b417b6dd3f6f68 Mon Sep 17 00:00:00 2001 From: "Turkesh Patel (Open ERP)" Date: Thu, 21 Jun 2012 14:37:14 +0530 Subject: [PATCH 3/3] [IMP] improved code. bzr revid: tpa@tinyerp.com-20120621090714-giqg7gynttquiztn --- addons/mail/mail_thread.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index 644b52df2db..76cb942b327 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -449,20 +449,21 @@ class mail_thread(osv.osv): def _get_user(self, cr, uid, alias, context): """ - return user_id based on alias - else return id of admin + param alias: browse record of alias. + return: int user_id. """ user_obj = self.pool.get('res.user') + user_id = 1 if alias.alias_user_id: user_id = alias_id.alias_user_id.id #if user_id not defined in the alias then search related user using name of Email sender else: - frm = msg.get('from') - user_ids = user_obj.search(cr, uid, [('name','=',frm)], context) + from_email = msg.get('from') + user_ids = user_obj.search(cr, uid, [('name','=',from_email)], context) if user_ids: user_id = user_obj.browse(cr, uid, user_ids[0], context).id - return user_id or 1 + return user_id def message_catchall(self, cr, uid, message, context=None): """ @@ -492,14 +493,14 @@ class mail_thread(osv.osv): #if alias found then call message_process method. if alias_id: user_id = self._get_user(self, cr, uid, alias_id, context) - self.message_process(self, cr, user_id, alias_id.alias_model_id.id, message, alias_id.alias_defaults or False , False, False, alias_id.alias_force_thread_id or False, context) + self.message_process(self, cr, user_id, alias_id.alias_model_id.id, message, custom_values = alias_id.alias_defaults or {}, thread_id = alias_id.alias_force_thread_id or {}, context) #if alis not found give Exception else: - _logger.warning("This mailbox does not exist so mail gate will reject this mail.") - frm = user_obj.browse(cr, uid, uid, context).user_email + #_logger.warning("This mailbox does not exist so mail gate will reject this mail.") + from_email = user_obj.browse(cr, uid, uid, context).user_email sub = "Mail Rejection" + msg.get('subject') message = "Respective mailbox does not exist so your mail have been rejected" + msg - mail_message.send_mail(cr, uid, {'email_from': frm,'email_to': msg.get('from'),'subject': sub, 'body_text': message}, context) + mail_message.send_mail(cr, uid, {'email_from': from_email,'email_to': msg.get('from'),'subject': sub, 'body_text': message}, context) return True