Пример #1
0
def parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None)
    if not content_disposition:
        return None
    dispo_type, dispo_dict = parse_dispositions(content_disposition)
    if not (dispo_type == "attachment" or (dispo_type == 'inline' and
            'filename' in dispo_dict)):
        return None

    content_type = message_part.get("Content-Type", None)
    file_data = message_part.get_payload(decode=True)
    if file_data is None:
        payloads = message_part.get_payload()
        file_data = '\n\n'.join([p.as_string() for p in payloads]).encode('utf-8')
    attachment = BytesIO(file_data)
    attachment.content_type = message_part.get_content_type()
    attachment.size = len(file_data)
    attachment.name = None
    attachment.create_date = None
    attachment.mod_date = None
    attachment.read_date = None
    attachment.name = get_attachment_name(
        attachment, dispo_dict, content_type=content_type
    )

    if "create-date" in dispo_dict:
        attachment.create_date = dispo_dict['create-date']  # TODO: datetime
    if "modification-date" in dispo_dict:
        attachment.mod_date = dispo_dict['modification-date']  # TODO: datetime
    if "read-date" in dispo_dict:
        attachment.read_date = dispo_dict['read-date']  # TODO: datetime
    return attachment
Пример #2
0
def parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None)
    if not content_disposition:
        return None
    dispo_type, dispo_dict = parse_dispositions(content_disposition)
    if not (dispo_type == "attachment" or
            (dispo_type == 'inline' and 'filename' in dispo_dict)):
        return None

    content_type = message_part.get("Content-Type", None)
    file_data = message_part.get_payload(decode=True)
    if file_data is None:
        payloads = message_part.get_payload()
        file_data = '\n\n'.join([p.as_string()
                                 for p in payloads]).encode('utf-8')
    attachment = BytesIO(file_data)
    attachment.content_type = message_part.get_content_type()
    attachment.size = len(file_data)
    attachment.name = None
    attachment.create_date = None
    attachment.mod_date = None
    attachment.read_date = None
    attachment.name = get_attachment_name(attachment,
                                          dispo_dict,
                                          content_type=content_type)

    if "create-date" in dispo_dict:
        attachment.create_date = dispo_dict['create-date']  # TODO: datetime
    if "modification-date" in dispo_dict:
        attachment.mod_date = dispo_dict['modification-date']  # TODO: datetime
    if "read-date" in dispo_dict:
        attachment.read_date = dispo_dict['read-date']  # TODO: datetime
    return attachment
Пример #3
0
def _parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None)
    if content_disposition:
        dispositions = content_disposition.strip().split(";")
        if content_disposition and dispositions[0].lower() == "attachment":
            file_data = message_part.get_payload(decode=True)
            attachment = BytesIO(file_data)
            attachment.content_type = message_part.get_content_type()
            attachment.size = len(file_data)
            attachment.name = None
            attachment.create_date = None
            attachment.mod_date = None
            attachment.read_date = None

            for param in dispositions[1:]:
                name, value = param.strip().split("=")
                name = name.lower()

                if name == "filename":
                    attachment.name = value
                elif name == "create-date":
                    attachment.create_date = value  # FIXME: datetime
                elif name == "modification-date":
                    attachment.mod_date = value  # FIXME: datetime
                elif name == "read-date":
                    attachment.read_date = value  # FIXME: datetime

            attachment.checksum = _md5(attachment)

            return attachment

    return None
Пример #4
0
    def parse_postmark(self, obj):
        from_field = (obj['FromFull']['Name'], obj['FromFull']['Email'])
        tos = [(o['Name'], o['Email']) for o in obj['ToFull']]
        ccs = [(o['Name'], o['Email']) for o in obj['CcFull']]
        attachments = []
        for a in obj['Attachments']:
            attachment = BytesIO(base64.b64decode(a['Content']))
            attachment.content_type = a['ContentType']
            attachment.size = a['ContentLength']
            attachment.name = a['Name']
            attachment.create_date = None
            attachment.mod_date = None
            attachment.read_date = None
            attachments.append(attachment)

        return ParsedEmail(
            None, **{
                'postmark_msgobj': obj,
                'date': parse_date(obj['Date']),
                'subject': obj['Subject'],
                'body': obj['TextBody'],
                'html': obj['HtmlBody'],
                'from_': from_field,
                'to': tos,
                'cc': ccs,
                'resent_to': [],
                'resent_cc': [],
                'attachments': attachments
            })
Пример #5
0
    def parse_postmark(self, obj):
        from_field = (obj['FromFull']['Name'], obj['FromFull']['Email'])
        tos = [(o['Name'], o['Email']) for o in obj['ToFull']]
        ccs = [(o['Name'], o['Email']) for o in obj['CcFull']]
        attachments = []
        for a in obj['Attachments']:
            attachment = BytesIO(base64.b64decode(a['Content']))
            attachment.content_type = a['ContentType']
            attachment.size = a['ContentLength']
            attachment.name = a['Name']
            attachment.create_date = None
            attachment.mod_date = None
            attachment.read_date = None
            attachments.append(attachment)

        return ParsedEmail(None, **{
            'postmark_msgobj': obj,
            'date': parse_date(obj['Date']),
            'subject': obj['Subject'],
            'body': obj['TextBody'],
            'html': obj['HtmlBody'],
            'from_': from_field,
            'to': tos,
            'cc': ccs,
            'resent_to': [],
            'resent_cc': [],
            'attachments': attachments
        })