Example #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)
Example #2
0
 def send_mail(self, mail):
     mail["Date"] = email.utils.formatdate(time.time(), True)
     cmdlist = shlex.split(self.cmd.encode("utf-8", errors="ignore"))
     out, err, retval = helper.call_cmd(cmdlist, stdin=mail.as_string())
     if err:
         errmsg = "%s. sendmail_cmd set to: %s" % (err, self.cmd)
         raise SendingMailFailed(errmsg)
     self.store_sent_mail(mail)
Example #3
0
 def send_mail(self, mail):
     mail['Date'] = email.utils.formatdate(time.time(), True)
     cmdlist = shlex.split(self.cmd.encode('utf-8', errors='ignore'))
     out, err, retval = helper.call_cmd(cmdlist, stdin=mail.as_string())
     if err:
         return err + '. sendmail_cmd set to: %s' % self.cmd
     self.store_sent_mail(mail)
     return None
Example #4
0
 def lookup(self, prefix):
     cmdlist = split_commandstring(self.command)
     resultstring, errmsg, retval = call_cmd(cmdlist + [prefix])
     if not resultstring:
         return []
     lines = resultstring.splitlines()
     res = []
     for l in lines:
         m = re.match(self.match, l)
         if m:
             info = m.groupdict()
             email = info['email'].strip()
             name = info['name']
             res.append((name, email))
     return res
Example #5
0
 def lookup(self, prefix):
     cmdlist = split_commandstring(self.command)
     resultstring, errmsg, retval = call_cmd(cmdlist + [prefix])
     if not resultstring:
         return []
     lines = resultstring.splitlines()
     res = []
     for l in lines:
         m = re.match(self.match, l, self.reflags)
         if m:
             info = m.groupdict()
             email = info['email'].strip()
             name = info['name']
             res.append((name, email))
     return res
Example #6
0
 def lookup(self, prefix):
     cmdlist = shlex.split(self.command.encode("utf-8", errors="ignore"))
     resultstring, errmsg, retval = helper.call_cmd(cmdlist + [prefix])
     if not resultstring:
         return []
     lines = resultstring.replace("\t", " " * 4).splitlines()
     res = []
     for l in lines:
         m = re.match(self.match, l)
         if m:
             info = m.groupdict()
             email = info["email"].strip()
             name = info["name"].strip()
             res.append((name, email))
     return res
Example #7
0
    def lookup(self, prefix):
        cmdlist = split_commandstring(self.command)
        resultstring, errmsg, retval = call_cmd(cmdlist + [prefix])
        if retval != 0:
            msg = 'abook command "%s" returned with ' % self.command
            msg += 'return code %d' % retval
            if errmsg:
                msg += ':\n%s' % errmsg
            raise AddressbookError(msg)

        if not resultstring:
            return []
        lines = resultstring.splitlines()
        res = []
        for l in lines:
            m = re.match(self.match, l, self.reflags)
            if m:
                info = m.groupdict()
                if 'email' and 'name' in info:
                    email = info['email'].strip()
                    name = info['name']
                    res.append((name, email))
        return res