def message_route_check_bounce(self, cr, uid, message, context=None): """ Override to verify that the email_to is the bounce alias. If it is the case, log the bounce, set the parent and related document as bounced and return False to end the routing process. """ bounce_alias = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.bounce.alias", context=context) message_id = message.get('Message-Id') email_from = decode_header(message, 'From') email_to = decode_header(message, 'To') # 0. Verify whether this is a bounced email (wrong destination,...) -> use it to collect data, such as dead leads if bounce_alias in email_to: # Bounce regex # Typical form of bounce is bounce_alias-128-crm.lead-34@domain # group(1) = the mail ID; group(2) = the model (if any); group(3) = the record ID bounce_re = re.compile("%s-(\d+)-?([\w.]+)?-?(\d+)?" % re.escape(bounce_alias), re.UNICODE) bounce_match = bounce_re.search(email_to) if bounce_match: bounced_model, bounced_thread_id = None, False bounced_mail_id = bounce_match.group(1) stat_ids = self.pool['mail.mail.statistics'].set_bounced(cr, uid, mail_mail_ids=[bounced_mail_id], context=context) for stat in self.pool['mail.mail.statistics'].browse(cr, uid, stat_ids, context=context): bounced_model = stat.model bounced_thread_id = stat.res_id _logger.info('Routing mail from %s to %s with Message-Id %s: bounced mail from mail %s, model: %s, thread_id: %s', email_from, email_to, message_id, bounced_mail_id, bounced_model, bounced_thread_id) if bounced_model and bounced_model in self.pool and hasattr(self.pool[bounced_model], 'message_receive_bounce') and bounced_thread_id: self.pool[bounced_model].message_receive_bounce(cr, uid, [bounced_thread_id], mail_id=bounced_mail_id, context=context) return False return True
def message_route_check_bounce(self, cr, uid, message, context=None): """ Override to verify that the email_to is the bounce alias. If it is the case, log the bounce, set the parent and related document as bounced and return False to end the routing process. """ bounce_alias = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.bounce.alias", context=context) message_id = message.get('Message-Id') email_from = decode_header(message, 'From') email_to = decode_header(message, 'To') # 0. Verify whether this is a bounced email (wrong destination,...) -> use it to collect data, such as dead leads if bounce_alias in email_to: bounce_match = tools.bounce_re.search(email_to) if bounce_match: bounced_model, bounced_thread_id = None, False bounced_mail_id = bounce_match.group(1) stat_ids = self.pool['mail.mail.statistics'].set_bounced(cr, uid, mail_mail_ids=[bounced_mail_id], context=context) for stat in self.pool['mail.mail.statistics'].browse(cr, uid, stat_ids, context=context): bounced_model = stat.model bounced_thread_id = stat.res_id _logger.info('Routing mail from %s to %s with Message-Id %s: bounced mail from mail %s, model: %s, thread_id: %s', email_from, email_to, message_id, bounced_mail_id, bounced_model, bounced_thread_id) if bounced_model and bounced_model in self.pool and hasattr(self.pool[bounced_model], 'message_receive_bounce') and bounced_thread_id: self.pool[bounced_model].message_receive_bounce(cr, uid, [bounced_thread_id], mail_id=bounced_mail_id, context=context) return False return True
def message_route_check_bounce(self, cr, uid, message, context=None): """ Override to verify that the email_to is the bounce alias. If it is the case, log the bounce, set the parent and related document as bounced and return False to end the routing process. """ bounce_alias = self.pool['ir.config_parameter'].get_param( cr, uid, "mail.bounce.alias", context=context) message_id = message.get('Message-Id') email_from = decode_header(message, 'From') email_to = decode_header(message, 'To') # 0. Verify whether this is a bounced email (wrong destination,...) -> use it to collect data, such as dead leads if bounce_alias in email_to: bounce_match = tools.bounce_re.search(email_to) if bounce_match: bounced_mail_id = bounce_match.group(1) stat_ids = self.pool['mail.mail.statistics'].set_bounced( cr, uid, mail_mail_ids=[bounced_mail_id], context=context) for stat in self.pool['mail.mail.statistics'].browse( cr, uid, stat_ids, context=context): bounced_model = stat.model bounced_thread_id = stat.res_id _logger.info( 'Routing mail from %s to %s with Message-Id %s: bounced mail from mail %s, model: %s, thread_id: %s', email_from, email_to, message_id, bounced_mail_id, bounced_model, bounced_thread_id) if bounced_model and bounced_model in self.pool and hasattr( self.pool[bounced_model], 'message_receive_bounce'): self.pool[bounced_model].message_receive_bounce( cr, uid, [bounced_thread_id], mail_id=bounced_mail_id, context=context) return False return True
def message_route_check_bounce(self, cr, uid, message, context=None): """ Check bounce with a '+' case of any match then replace it with '-' before and call before """ bounce_alias = self.pool['ir.config_parameter'].get_param( cr, uid, 'mail.bounce.alias', context=context) email_to = decode_header(message, 'To') if bounce_alias in email_to: # Is a bounce ? Reuse the same technic as in super method in # mass_mailing/models/mail_thread.py bounce_re = re.compile( r'%s\+(\d+)-?([\w.]+)?-?(\d+)?' % re.escape(bounce_alias), re.UNICODE) if bounce_re.search(email_to): # Replace the first occurrence of a plus sign in the # recipient address by a dash, e.g.: # [email protected] # => [email protected] del message['To'] message['To'] = email_to.replace('%s+' % bounce_alias, '%s-' % bounce_alias, 1) return super(MailThread, self).message_route_check_bounce(cr, uid, message, context=context)
def message_route_check_bounce(self, cr, uid, message, context=None): """ Override to verify that the email_to is the bounce alias. If it is the case, log the bounce, set the parent and related document as bounced and return False to end the routing process. """ bounce_alias = self.pool["ir.config_parameter"].get_param(cr, uid, "mail.bounce.alias", context=context) message_id = message.get("Message-Id") email_from = decode_header(message, "From") email_to = decode_header(message, "To") # 0. Verify whether this is a bounced email (wrong destination,...) -> use it to collect data, such as dead leads if bounce_alias in email_to: # Bounce regex # Typical form of bounce is bounce_alias-128-crm.lead-34@domain # group(1) = the mail ID; group(2) = the model (if any); group(3) = the record ID bounce_re = re.compile("%s-(\d+)-?([\w.]+)?-?(\d+)?" % re.escape(bounce_alias), re.UNICODE) bounce_match = bounce_re.search(email_to) if bounce_match: bounced_model, bounced_thread_id = None, False bounced_mail_id = bounce_match.group(1) stat_ids = self.pool["mail.mail.statistics"].set_bounced( cr, uid, mail_mail_ids=[bounced_mail_id], context=context ) for stat in self.pool["mail.mail.statistics"].browse(cr, uid, stat_ids, context=context): bounced_model = stat.model bounced_thread_id = stat.res_id _logger.info( "Routing mail from %s to %s with Message-Id %s: bounced mail from mail %s, model: %s, thread_id: %s", email_from, email_to, message_id, bounced_mail_id, bounced_model, bounced_thread_id, ) if ( bounced_model and bounced_model in self.pool and hasattr(self.pool[bounced_model], "message_receive_bounce") and bounced_thread_id ): self.pool[bounced_model].message_receive_bounce( cr, uid, [bounced_thread_id], mail_id=bounced_mail_id, context=context ) return False return True
def message_queue(self, model, message, custom_values=None, save_original=False, strip_attachments=False, thread_id=None): if isinstance(message, xmlrpclib.Binary): message = str(message.data) if isinstance(message, unicode): message = message.encode('utf-8') msg_txt = email.message_from_string(message) msg = self.env['mail.thread'].message_parse( msg_txt, save_original=save_original) in_reply_to = mt.decode_header(msg_txt, 'In-Reply-To') if in_reply_to: # Search id message id has a message inside odoo self.env['mail.thread'].message_process(model, message) return True mails_from = re.findall(r"<[^>]+>", msg_txt.get('From')) part_obj = self.env['res.partner'] for m in mails_from: p_ids = part_obj.search([('email', '=', self.clean_ref(m))]) if p_ids: self.env['mail.thread'].message_process(model, message) return True # Search if message already integrate message_id = self.clean_ref(msg_txt.get('Message-ID')) queue_ids = self.search([('message', '=', message_id)]) if queue_ids: return True mess_type = 'normal' if msg_txt.get('List-Post') or msg_txt.get('List-ID'): mess_type = 'mailing' elif msg_txt.get('X-Spam-Flag', '') == 'YES': mess_type = 'spam' args = { 'message': message_id, 'model': model, 'original': message, 'email_from': msg.get('from'), 'email_to': msg.get('to'), 'email_cc': msg.get('cc'), 'email_subject': msg.get('subject'), 'email_count_attachments': len(msg.get('attachments', 0)), 'email_date': msg.get('date'), 'status': mess_type, } return self.create(args)
def message_route_check_bounce(self, cr, uid, message, context=None): """ Check bounce with a '+' case of any match then replace it with '-' before and call before """ bounce_alias = self.pool['ir.config_parameter'].get_param( cr, uid, 'mail.bounce.alias', context=context) email_to = decode_header(message, 'To') if bounce_alias in email_to: bounce_re = re.compile( '%s%s' % (re.escape(bounce_alias), re.UNICODE), BOUNCE_EXPR) if bounce_re: message.set_param('To', message['To'].replace('+', '-', 1)) return super(MailThread, self).message_route_check_bounce(cr, uid, message, context=context)