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