New feature in mail module ========================== Currently, an email can be sent to an alias from: - Everyone - Authenticated Partners - Followers This commit is intended to add another category: Employees The main purpose of this new feature is to allow employees to send an email to an 'expense' alias in order to create automatically their expenses with their mobile phones. Use this new mechanism in hr_expense ==================================== Currenlty, we're overriding message_new to make a security check and create an expense if the sender is an employee or bounce otherwise, which is not the correct way to achieve this. A better way is to use the alias mechanism now that it has been extended to employees too. Don't hardcode email_from ========================= The email_from of the bounce email is hardcoded to "help@odoo.com". I am sure they will be happy to receive all answers from any odoo instance Review the bounce email content =============================== content: Your expense has not been created because your email address is not set on an employee or on a employee's user. Configure your employee's information correctly and try again. -> this is a message for the admin, not an employee or anyone else Now the content is generical for aliase defined for employees.
56 lines
2.5 KiB
Python
56 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
import logging
|
|
|
|
from odoo import api, models
|
|
from odoo.addons.mail.models.mail_thread import decode_header
|
|
from odoo.tools import email_split
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
class MailThread(models.AbstractModel):
|
|
_inherit = "mail.thread"
|
|
|
|
@api.model
|
|
def message_route_verify(self, message, message_dict, route, update_author=True, assert_model=True, create_fallback=True, allow_private=False):
|
|
res = super(MailThread, self).message_route_verify(message, message_dict, route, update_author, assert_model, create_fallback, allow_private)
|
|
|
|
if res:
|
|
alias = route[4]
|
|
email_from = decode_header(message, 'From')
|
|
message_id = message.get('Message-Id')
|
|
|
|
# Identically equal to the definition in mail module because sub methods are local
|
|
# variables and cannot be called with super
|
|
def _create_bounce_email(body_html):
|
|
bounce_to = decode_header(message, 'Return-Path') or email_from
|
|
bounce_mail_values = {
|
|
'body_html': body_html,
|
|
'subject': 'Re: %s' % message.get('subject'),
|
|
'email_to': bounce_to,
|
|
'auto_delete': True,
|
|
}
|
|
bounce_from = self.env['ir.mail_server']._get_default_bounce_address()
|
|
if bounce_from:
|
|
bounce_mail_values['email_from'] = 'MAILER-DAEMON <%s>' % bounce_from
|
|
self.env['mail.mail'].create(bounce_mail_values).send()
|
|
|
|
def _warn(message):
|
|
_logger.info('Routing mail with Message-Id %s: route %s: %s',
|
|
message_id, route, message)
|
|
|
|
# Alias: check alias_contact settings for employees
|
|
|
|
if alias and alias.alias_contact == 'employees':
|
|
email_address = email_split(email_from)[0]
|
|
employee = self.env['hr.employee'].search([('work_email', 'ilike', email_address)], limit=1)
|
|
if not employee:
|
|
employee = self.env['hr.employee'].search([('user_id.email', 'ilike', email_address)], limit=1)
|
|
if not employee:
|
|
mail_template = self.env.ref('hr.mail_template_data_unknown_employee_email_address')
|
|
_warn('alias %s does not accept unknown employees, skipping' % alias.alias_name)
|
|
_create_bounce_email(mail_template.body_html)
|
|
return False
|
|
return res
|