How do I extract and save email attachments using Python's imaplib and email library?

After fetching an email with `M.fetch(num, '(RFC822)')`, convert the raw bytes to an `EmailMessage` object using `email.message_from_bytes(data[0][1])`. Then iterate over `msg.walk()` and check for `part.get('Content-Disposition')` to identify attachments. Retrieve the filename with `part.get_filename()` and save the content using `part.get_payload(decode=True)`. This technique is demonstrated in the [Penetration Basics - Reading Emails Using the IMAP Protocol](/news/penetration-basics-reading-emails-using-the-imap-protocol) article and can be extended to search and export emails from Exchange servers as described in [Penetration Basics - Searching and Exporting Emails from Exchange Servers](/news/penetration-basics-searching-and-exporting-emails-from-exchange-servers).