Ejemplo n.º 1
0
def extract_body(mail, types=None):
    """
    returns a body text string for given mail.
    If types is `None`, 'text/*' is used:
    In case mail has a 'text/html' part, it is prefered over
    'text/plain' parts.

    :param mail: the mail to use
    :type mail: :class:`email.Message`
    :param types: mime content types to use for body string
    :type types: list of str
    """
    html = list(typed_subpart_iterator(mail, 'text', 'html'))

    # if no specific types are given, we favor text/html over text/plain
    drop_plaintext = False
    if html and not types:
        drop_plaintext = True

    body_parts = []
    for part in mail.walk():
        ctype = part.get_content_type()

        if types is not None:
            if ctype not in types:
                continue

        enc = part.get_content_charset() or 'ascii'
        raw_payload = part.get_payload(decode=True)
        if part.get_content_maintype() == 'text':
            raw_payload = string_decode(raw_payload, enc)
        if ctype == 'text/plain' and not drop_plaintext:
            body_parts.append(string_sanitize(raw_payload))
        else:
            #get mime handler
            handler = get_mime_handler(ctype, key='view',
                                       interactive=False)
            if handler:
                #open tempfile. Not all handlers accept stuff from stdin
                tmpfile = tempfile.NamedTemporaryFile(delete=False,
                                                      suffix='.html')
                #write payload to tmpfile
                if part.get_content_maintype() == 'text':
                    tmpfile.write(raw_payload.encode('utf8'))
                else:
                    tmpfile.write(raw_payload)
                tmpfile.close()
                #create and call external command
                cmd = handler % tmpfile.name
                cmdlist = shlex.split(cmd.encode('utf-8', errors='ignore'))
                rendered_payload, errmsg, retval = helper.call_cmd(cmdlist)
                #remove tempfile
                os.unlink(tmpfile.name)
                if rendered_payload:  # handler had output
                    body_parts.append(string_sanitize(rendered_payload))
                elif part.get_content_maintype() == 'text':
                    body_parts.append(string_sanitize(raw_payload))
                # else drop
    return '\n\n'.join(body_parts)
Ejemplo n.º 2
0
    def apply(self, ui):
        logging.info('open attachment')
        mimetype = self.attachment.get_content_type()
        handler = settings.get_mime_handler(mimetype)
        if handler:
            path = self.attachment.save(tempfile.gettempdir())
            if '%s' in handler:
                cmd = handler % path.replace(' ', '\ ')
            else:
                cmd = '%s %s' % (handler, path.replace(' ', '\ '))

            def afterwards():
                os.remove(path)
            ui.apply_command(ExternalCommand(cmd, on_success=afterwards,
                                             in_thread=True))
        else:
            ui.notify('unknown mime type')
Ejemplo n.º 3
0
def extract_body(mail):
    bodytxt = ''
    for part in mail.walk():
        ctype = part.get_content_type()
        enc = part.get_content_charset()
        raw_payload = part.get_payload(decode=True)
        if part.get_content_maintype() == 'text':
            if enc:
                raw_payload = unicode(raw_payload, enc)
            else:
                raw_payload = unicode(raw_payload, errors='replace')
        if ctype == 'text/plain':
            bodytxt += raw_payload
        else:
            #get mime handler
            handler = get_mime_handler(ctype, key='view',
                                       interactive=False)
            if handler:
                #open tempfile. Not all handlers accept stuff from stdin
                tmpfile = tempfile.NamedTemporaryFile(delete=False,
                                                      suffix='.html')
                #write payload to tmpfile
                if part.get_content_maintype() == 'text':
                    tmpfile.write(raw_payload.encode('utf8'))
                else:
                    tmpfile.write(raw_payload)
                #create and call external command
                cmd = handler % tmpfile.name
                rendered_payload = helper.cmd_output(cmd)
                #remove tempfile
                tmpfile.close()
                os.unlink(tmpfile.name)
                if rendered_payload:  # handler had output
                    bodytxt += unicode(rendered_payload.strip(),
                                       encoding='utf8', errors='replace')
                elif part.get_content_maintype() == 'text':
                    bodytxt += raw_payload
                # else drop
    return bodytxt