def test_email_domain_normalize(self): self.assertEqual(email_domain_normalize("Test.Com"), "test.com", "Should have normalized the domain") self.assertEqual(email_domain_normalize("*****@*****.**"), False, "The domain is not valid, should return False") self.assertEqual(email_domain_normalize(False), False, "The domain is not valid, should return False")
def _find_mail_server(self, email_from, mail_servers=None): """Find the appropriate mail server for the given email address. Returns: Record<ir.mail_server>, email_from - Mail server to use to send the email (None if we use the odoo-bin arguments) - Email FROM to use to send the email (in some case, it might be impossible to use the given email address directly if no mail server is configured for) """ email_from_normalized = email_normalize(email_from) email_from_domain = email_domain_extract(email_from_normalized) notifications_email = email_normalize(self._get_default_from_address()) notifications_domain = email_domain_extract(notifications_email) if mail_servers is None: mail_servers = self.sudo().search([], order='sequence') # 1. Try to find a mail server for the right mail from mail_server = mail_servers.filtered(lambda m: email_normalize(m.from_filter) == email_from_normalized) if mail_server: return mail_server[0], email_from mail_server = mail_servers.filtered(lambda m: email_domain_normalize(m.from_filter) == email_from_domain) if mail_server: return mail_server[0], email_from # 2. Try to find a mail server for <*****@*****.**> if notifications_email: mail_server = mail_servers.filtered(lambda m: email_normalize(m.from_filter) == notifications_email) if mail_server: return mail_server[0], notifications_email mail_server = mail_servers.filtered(lambda m: email_domain_normalize(m.from_filter) == notifications_domain) if mail_server: return mail_server[0], notifications_email # 3. Take the first mail server without "from_filter" because # nothing else has been found... Will spoof the FROM because # we have no other choices mail_server = mail_servers.filtered(lambda m: not m.from_filter) if mail_server: return mail_server[0], email_from # 4. Return the first mail server even if it was configured for another domain if mail_servers: return mail_servers[0], email_from # 5: SMTP config in odoo-bin arguments from_filter = self.env['ir.config_parameter'].sudo().get_param( 'mail.default.from_filter', tools.config.get('from_filter')) if self._match_from_filter(email_from, from_filter): return None, email_from if notifications_email and self._match_from_filter(notifications_email, from_filter): return None, notifications_email return None, email_from
def _match_from_filter(self, email_from, from_filter): """Return True is the given email address match the "from_filter" field. The from filter can be Falsy (always match), a domain name or an full email address. """ if not from_filter: return True normalized_mail_from = email_normalize(email_from) if '@' in from_filter: return email_normalize(from_filter) == normalized_mail_from return email_domain_extract(normalized_mail_from) == email_domain_normalize(from_filter)