2

I would like to go through my emails and save big attachments in a file folder. Once the attachment is saved, I would like to replace the attachment with a note where to find the original attachment.

My understanding is that I have to make a modified copy of the original email (with the notification instead of the original attachment) and delete the original email.

Now my questions is, how I can create a new email based on the original email using imap_tools, without the attachments that I want to remove, but with a plain text message instead.

This is where I am now:

from copy import copy
from imap_tools import MailBox, A, U

msgId = "30214"
mailbox = MailBox("imap.gmail.com").login(user, password)
mailbox.folder.set('[Gmail]/All Mail')
for msg in mailbox.fetch(A(uid=U(msgId))):
    print(f"{i:>5}: {msg.date}, {msg.subject}, Attachments: {len(msg.attachments)}, Size: {msg.size}")
    newMsg = copy(msg)
    for i, att in enumerate(newMsg.attachments):
        print(i)
        if att.size > 100_000:
            print(f"{i}: {att.filename}: {att.size}")
            # remove att from newMessage
            # add a new attachment (e.g. text/plain with text denoting where I saved the original)
    # add newMsg to mailbox
    # remove msg from mailbox
    

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
divingTobi
  • 2,044
  • 10
  • 25

1 Answers1

1

imap_tools is for work with emails by IMAP

your task is out of imap_tools context

however, there is a little example at imap_tools:

https://github.com/ikvk/imap_tools/blob/master/examples/email_edit.py

Vladimir
  • 6,162
  • 2
  • 32
  • 36