Exemplo n.º 1
0
    def patch_message(self, message):
        """Override message recipients.

        Result is dependent on what is specified in the configuration:
        1. Message is not changed (no configuration or 'disabled');
        2. Message recipients are replaced;
        3. There is no valid recipient in the override configuration.

        If there is no valid recipient, an Exception will be thrown. This
        will be an AssertionException, in line with what is done by standard
        Odoo. Most likely this message will be catched and an appropiate
        reason for non delivery registered.
        """
        override_email_id = self.env.ref(
            'override_mail_recipients.override_email_to',
            raise_if_not_found=False)
        if not override_email_id or \
                not override_email_id.value or \
                override_email_id.value == 'disable':
            return
        actual_recipients = extract_rfc2822_addresses(override_email_id.value)
        assert actual_recipients, 'No valid override_email_to'
        for field in ['to', 'cc', 'bcc']:
            if not message[field]:
                continue
            original = COMMASPACE.join(message.get_all(field, []))
            not_so_original = original.replace('\\', '').replace(
                '"', '\\"').replace('<', '[').replace('>', ']').replace(
                    '@', '(at)')
            del message[field]
            message[field] = COMMASPACE.join(
                '"%s" <%s>' % (not_so_original, email)
                for email in actual_recipients)
Exemplo n.º 2
0
    def send_email(self, message, mail_server_id=None, smtp_server=None, smtp_port=None,
                   smtp_user=None, smtp_password=None, smtp_encryption=None, smtp_debug=False,):

        smtp_from = message['Return-Path'] or message['From']
        assert smtp_from, "The Return-Path or From header is required for any outbound email"

        # The email's "Envelope From" (Return-Path), and all recipient addresses must only contain ASCII characters.
        from_rfc2822 = extract_rfc2822_addresses(smtp_from)
        assert len(from_rfc2822) == 1, "Malformed 'Return-Path' or 'From' address - it may only contain plain ASCII characters"
        smtp_from = from_rfc2822[0]

        #################################################
        # patch: 首先查找 smtp_from 对应的 smtp 服务器
        # 要求 定义 Outgoing Mail Servers 时候, 确保 Description(name) 字段的值 为对应的 邮件发送账户(完整的eMail地址)
        # 本模块以此 为 邮件的发送者 查找 smtp 服务器
        # 需要为系统中 每个可能发送邮件的账户 按以上要求设置一个 服务器
        if self.env.user.mail_server:
            mail_server_ids = self.search([('id','=',self.env.user.mail_server.id)], order='sequence', limit=1)
            if mail_server_ids:
                mail_server_id = mail_server_ids[0].id

        res = super(ir_mail_server,self).send_email(message,
                                                    mail_server_id,
                                                    smtp_server,
                                                    smtp_port,
                                                    smtp_user,
                                                    smtp_password,
                                                    smtp_encryption,
                                                    smtp_debug
                                                    )
        return res
Exemplo n.º 3
0
 def send(self,
          cr,
          uid,
          ids,
          auto_commit=False,
          raise_exception=False,
          context=None):
     for email in self.pool.get('mail.mail').browse(cr,
                                                    uid,
                                                    ids,
                                                    context=context):
         from_rfc2822 = extract_rfc2822_addresses(email.email_from)[-1]
         server_id = self.pool.get('ir.mail_server').search(
             cr, uid, [('smtp_user', '=', from_rfc2822)], context=context)
         server_id = server_id and server_id[0] or False
         if server_id:
             self.write(cr,
                        uid,
                        ids, {
                            'mail_server_id': server_id,
                            'reply_to': email.email_from,
                        },
                        context=context)
     return super(mail_mail, self).send(cr,
                                        uid,
                                        ids,
                                        auto_commit=auto_commit,
                                        raise_exception=raise_exception,
                                        context=context)
Exemplo n.º 4
0
def send_email(self,
               cr,
               uid,
               message,
               mail_server_id=None,
               smtp_server=None,
               smtp_port=None,
               smtp_user=None,
               smtp_password=None,
               smtp_encryption=None,
               smtp_debug=False,
               context=None):
    from_rfc2822 = extract_rfc2822_addresses(message['From'])[-1]
    server_id = self.pool.get('ir.mail_server').search(
        cr, uid, [('smtp_user', '=', from_rfc2822)], context=context)
    if server_id and server_id[0]:
        if 'Return-Path' in message:
            message.replace_header('Return-Path', from_rfc2822)
    return super(ir_mail_server, self).send_email(cr,
                                                  uid,
                                                  message,
                                                  mail_server_id,
                                                  smtp_server,
                                                  smtp_port,
                                                  smtp_user,
                                                  smtp_password,
                                                  smtp_encryption,
                                                  smtp_debug,
                                                  context=context)
Exemplo n.º 5
0
 def send_email(
         self, message, mail_server_id=None, smtp_server=None,
         smtp_port=None, smtp_user=None, smtp_password=None,
         smtp_encryption=None, smtp_debug=False):
     override_email = self.env['ir.model.data'].xmlid_to_object(
         'override_mail_from.override_email_from',
         raise_if_not_found=True).value
     if override_email:
         for field in ['from', 'reply-to']:
             if not message[field]:
                 continue
             original = COMMASPACE.join(message.get_all(field, []))
             del message[field]
             message[field] = COMMASPACE.join(
                 '"%s" <%s>' % (
                     original.replace('\\', '').replace('"', '\\"')
                     .replace('<', '[').replace('>', ']')
                     .replace('@', '(at)'),
                     email
                 )
                 for email in extract_rfc2822_addresses(override_email))
     return super(IrMailServer, self).send_email(
         message, mail_server_id=mail_server_id,
         smtp_server=smtp_server, smtp_port=smtp_port, smtp_user=smtp_user,
         smtp_password=smtp_password, smtp_encryption=smtp_encryption,
         smtp_debug=smtp_debug)
Exemplo n.º 6
0
 def send_email(
         self, message, mail_server_id=None, smtp_server=None,
         smtp_port=None, smtp_user=None, smtp_password=None,
         smtp_encryption=None, smtp_debug=False):
     override_email = self.env.ref(
         'override_mail_recipients.override_email_to').value
     if override_email:
         for field in ['to', 'cc', 'bcc']:
             if not message[field]:
                 continue
             original = COMMASPACE.join(message.get_all(field, []))
             del message[field]
             message[field] = COMMASPACE.join(
                 '"%s" <%s>' % (
                     original.replace('\\', '').replace('"', '\\"')
                     .replace('<', '[').replace('>', ']')
                     .replace('@', '(at)'),
                     email
                 )
                 for email in extract_rfc2822_addresses(override_email))
     return super(IrMailServer, self).send_email(
         message, mail_server_id=mail_server_id,
         smtp_server=smtp_server, smtp_port=smtp_port, smtp_user=smtp_user,
         smtp_password=smtp_password, smtp_encryption=smtp_encryption,
         smtp_debug=smtp_debug)
Exemplo n.º 7
0
 def send_email(self,
                message,
                mail_server_id=None,
                smtp_server=None,
                smtp_port=None,
                smtp_user=None,
                smtp_password=None,
                smtp_encryption=None,
                smtp_debug=False):
     # Start copy from native method
     smtp_from = message['Return-Path'] or\
         self._get_default_bounce_address() or message['From']
     from_rfc2822 = extract_rfc2822_addresses(smtp_from)
     smtp_from = from_rfc2822[-1]
     # End copy from native method
     logger.info(
         "Sending email from '%s' to '%s' Cc '%s' Bcc '%s' "
         "with subject '%s'", smtp_from, message.get('To'),
         message.get('Cc'), message.get('Bcc'), message.get('Subject'))
     return super(IrMailServer,
                  self).send_email(message,
                                   mail_server_id=mail_server_id,
                                   smtp_server=smtp_server,
                                   smtp_port=smtp_port,
                                   smtp_user=smtp_user,
                                   smtp_password=smtp_password,
                                   smtp_encryption=smtp_encryption,
                                   smtp_debug=smtp_debug)
Exemplo n.º 8
0
 def check_email_validity(self):
     if self.env.context.get('skip_check_email_validity'):
         return
     for channel in self:
         if not extract_rfc2822_addresses(channel.email_exchange_system):
             raise exceptions.ValidationError(
                 _("Email %s is not valid")
                 % channel.email_exchange_system)
Exemplo n.º 9
0
 def send(self, auto_commit=False, raise_exception=False):
     for email in self.env['mail.mail'].browse(self.ids):
         from_rfc2822 = extract_rfc2822_addresses(email.email_from)[-1]
         server_id = self.env['ir.mail_server'].search([('smtp_user', '=',
                                                         from_rfc2822)])
         server_id = server_id and server_id[0] or False
         if server_id:
             self.write({
                 'mail_server_id': server_id[0].id,
                 'reply_to': email.email_from
             })
     return super(MailMail, self).send(auto_commit=auto_commit,
                                       raise_exception=raise_exception)
Exemplo n.º 10
0
 def send_email(self,
                message,
                mail_server_id=None,
                smtp_server=None,
                smtp_port=None,
                smtp_user=None,
                smtp_password=None,
                smtp_encryption=None,
                smtp_debug=False):
     from_rfc2822 = extract_rfc2822_addresses(message['From'])[-1]
     server_id = self.env['ir.mail_server'].search([('smtp_user', '=',
                                                     from_rfc2822)])
     if server_id and server_id[0]:
         message['Return-Path'] = from_rfc2822
     return super(IrMailServer,
                  self).send_email(message, mail_server_id, smtp_server,
                                   smtp_port, smtp_user, smtp_password,
                                   smtp_encryption, smtp_debug)
Exemplo n.º 11
0
 def check_email_validity(self):
     for channel in self:
         if not extract_rfc2822_addresses(channel.email_exchange_system):
             raise exceptions.ValidationError(
                 _("Email %s is not valid") % channel.email_exchange_system)