Home > Back-end >  How to download attachments in .eml file?
How to download attachments in .eml file?

Time:12-07

I have an .eml file which contains .eml files as attachements and each of these files have an pdf attachment, how do I download the pdf files from every email file?

Attaching the email file components

image of email as attachments

Image containing the pdf attachements

tried using python email parser module

msg = email.message_from_file(open('AWEX WMR.eml'), policy=policy.default)

for att in msg.iter_attachments():
    print(att)

This is returning the following content

Output exceeds the size limit. Open the full output data in a text editor Content-Type: application/octet-stream; name="WMR.pdf" Content-Description: WMR.pdf Content-Disposition: attachment; filename="WMR.pdf"; size=271839; creation-date="Wed, 09 Nov 2022 07:52:34 GMT"; modification-date="Wed, 23 Nov 2022 08:56:38 GMT" Content-ID: [email protected] Content-Transfer-Encoding: base64

Still Unable to download the file

CodePudding user response:

On PyPI there is an existing package called eml-extractor which should be the thing you're looking for. You can find it here: eml-extractor ,or just use pip install eml-extractor.

Usage: eml-extractor [OPTIONS]

Extracts attachments from .eml files

optional arguments:
  -h, --help            show this help message and exit
  -s PATH, --source PATH
                        the directory containing the .eml files to extract
                        attachments (default: current working directory)
  -r, --recursive       allow recursive search for .eml files under SOURCE
                        directory
  -f FILE [FILE ...], --files FILE [FILE ...]
                        specify an .eml file or a list of .eml files to extract
                        attachments
  -d PATH, --destination PATH
                        the directory to extract attachments to (default:
                        current working directory)

Just the command $ eml-extractor already searches for all .eml files in your working directory and saves the attachments in the same directory. Further specifications are really easy to achieve.

Hope this helps!

CodePudding user response:

On PyPI there is an existing package called eml-extractor which should be the thing you're looking for. You can find it here: eml-extractor ,or just use pip install eml-extractor.

  • Related