import logging from odoo import models, fields, api, tools _logger = logging.getLogger(__name__) class ResPartner(models.Model): _inherit = "res.partner" email2 = fields.Char("Email 2") email2_formatted = fields.Char( "Formatted Email 2", compute="_compute_email2_formatted", help='Format email address "Name "', ) @api.depends("name", "email2") def _compute_email2_formatted(self): """Compute formatted email for partner, using formataddr. Be defensive in computation, notably * double format: if email already holds a formatted email like 'Name' we should not use it as it to compute email formatted like "Name <'Name' >"; * multi emails: sometimes this field is used to hold several addresses like email1@domain.com, email2@domain.com. We currently let this value untouched, but remove any formatting from multi emails; * invalid email: if something is wrong, keep it in email2_formatted as this eases management and understanding of failures at mail.mail, mail.notification and mailing.trace level; * void email: email2_formatted is False, as we cannot do anything with it; """ self.email2_formatted = False for partner in self: emails_normalized = tools.email_normalize_all(partner.email) if emails_normalized: # note: multi-email input leads to invalid email like "Name" # but this is current behavior in Odoo 14+ and some servers allow it partner.email2_formatted = tools.formataddr( (partner.name or "False", ",".join(emails_normalized)) ) elif partner.email: partner.email2_formatted = tools.formataddr( (partner.name or "False", partner.email) )