Python extract attachments

6 Python code examples are found related to " extract attachments". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: msg_parser.py    From Python-Digital-Forensics-Cookbook with MIT License 7 votes vote down vote up
def extract_attachments(msg, out_dir):
    attachment_attribs = [
        'DisplayName', 'FileName', 'PathName', 'Position', 'Size'
    ]
    i = 1  # Attachments start at 1
    while True:
        try:
            attachment = msg.Attachments(i)
        except pywintypes.com_error:
            break

        print("\nAttachment {}".format(i))
        print("=" * 15)
        for entry in attachment_attribs:
            print('{}: {}'.format(entry, getattr(attachment, entry,
                                                 "N/A")))
        outfile = os.path.join(os.path.abspath(out_dir),
                               os.path.split(args.MSG_FILE)[-1])
        if not os.path.exists(outfile):
            os.makedirs(outfile)
        outfile = os.path.join(outfile, attachment.FileName)
        attachment.SaveAsFile(outfile)
        print("Exported: {}".format(outfile))
        i += 1 
Example 2
Source File: email_mirror.py    From zulip with Apache License 2.0 6 votes vote down vote up
def extract_and_upload_attachments(message: EmailMessage, realm: Realm) -> str:
    user_profile = get_system_bot(settings.EMAIL_GATEWAY_BOT)

    attachment_links = []
    for part in message.walk():
        content_type = part.get_content_type()
        filename = part.get_filename()
        if filename:
            attachment = part.get_payload(decode=True)
            if isinstance(attachment, bytes):
                s3_url = upload_message_file(filename, len(attachment), content_type,
                                             attachment,
                                             user_profile,
                                             target_realm=realm)
                formatted_link = f"[{filename}]({s3_url})"
                attachment_links.append(formatted_link)
            else:
                logger.warning("Payload is not bytes (invalid attachment %s in message from %s).",
                               filename, message.get("From"))

    return '\n'.join(attachment_links) 
Example 3
Source File: message.py    From imapbox with MIT License 6 votes vote down vote up
def extractAttachments(self):
        message_parts = self.getParts()

        if message_parts['text']:
            self.createTextFile(message_parts['text'])

        if message_parts['html']:
            self.createHtmlFile(message_parts['html'], message_parts['embed_images'])

        if message_parts['files']:
            attdir = os.path.join(self.directory, 'attachments')
            if not os.path.exists(attdir):
                os.makedirs(attdir)
            for afile in message_parts['files']:
                with open(os.path.join(attdir, afile[1]), 'wb') as fp:
                    payload = afile[0].get_payload(decode=True)
                    if payload:
                        fp.write(payload) 
Example 4
Source File: mappers.py    From federation with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def extract_attachments(payload: Dict) -> List[Image]:
    """
    Extract images from attachments.

    There could be other attachments, but currently we only extract images.
    """
    attachments = []
    for item in payload.get('attachment', []):
        # noinspection PyProtectedMember
        if item.get("type") in ("Document", "Image") and item.get("mediaType") in Image._valid_media_types:
            if item.get('pyfed:inlineImage', False):
                # Skip this image as it's indicated to be inline in content and source already
                continue
            attachments.append(
                ActivitypubImage(
                    url=item.get('url'),
                    name=item.get('name') or "",
                    media_type=item.get("mediaType"),
                )
            )
    return attachments 
Example 5
Source File: attachment_split.py    From pst-extraction with Apache License 2.0 5 votes vote down vote up
def extractAttachments(x):
    parent_fields = {
        'id' : x['id'],
        'datetime' : x['datetime'],
        "ingest_id" : x["ingest_id"],
        "case_id" : x["case_id"],
        "alt_ref_id" : x["alt_ref_id"],
        "label" : x["label"],
        "original_artifact" : x["original_artifact"]
    }
    attachments = map(lambda o: extractKeys([
        'guid',
        'extension',
        'filename',
        'content',
        'contents64',
        'content_extracted',
        'content_encrypted',
        'content_length',
        'content_type',
        'content_hash',
        'content_tika_langid',
        'exif',
        'image_analytics',
        'metadata',
        'size'
    ], o), x['attachments'])
    attachments = [dict(a, **parent_fields) for a in attachments]
    return attachments 
Example 6
Source File: msg_extractor.py    From fame_modules with GNU General Public License v3.0 4 votes vote down vote up
def extract_attachments(self, mail, outdir):
        attachments = mail.attachments
        paths = []

        for attachment in attachments:
            if attachment.type == "msg":
                tmp = attachment.save(customPath=outdir, useFileName=True)
                for file in os.listdir(tmp):
                    paths.append("%s%s%s" % (tmp, os.path.sep, file))
            else:
                paths.append(attachment.save(customPath=outdir))

        return paths