robocorp / rpaframework

Collection of open-source libraries and tools for Robotic Process Automation (RPA), designed to be used with both Robot Framework and Python
https://www.rpaframework.org/
Apache License 2.0
1.13k stars 218 forks source link

RPA.Email.ImapSmtp save_attachments doesn’t save .msg files attached to the email (ItemAttachment objects) #1056

Open 21010 opened 1 year ago

21010 commented 1 year ago

Hi! I’m using RPA.Email.ImapSmtp to read email from the mailbox and extract all attached files with save_attachments method. For some reason .msg files are not saved - all other attached files are saved, but the file with the extension .msg is not - I can see an object exists in the message property instead.

Code example:

from RPA.Email.ImapSmtp import ImapSmtp

imap: ImapSmtp = ImapSmtp(login, password=password, imap_server=imap_server, imap_port=imap_port)
imap.authorize_imap()

emails = imap.list_messages("UNSEEN")

for email in emails:
    imap.save_attachment(email, target_folder='c:/temp')

As a result, the attached .msg file will be omitted and not saved. It's because it's not an instance of the FileAttachment, but ItemAttachment where mime_content (attachment.item.mime_content) should be used not just content (attachment.content).

mikahanninen commented 1 year ago

@21010 Hi thank you for the report.

Question: Which version of rpaframework you are using ? Question: Is this report actually about RPA.Email.Exchange library and not about RPA.Email.ImapSmtp ?

My response below is related to the latter.

I tried to replicate the issue with an email containing one .msg file (saved one email from Outlook), one .pdf file and one .png file and all were saved correctly.

My code

EMAIL.authorize()
messages = EMAIL.list_messages('SUBJECT "Testing attachments"')
for m in messages:
    attachments = EMAIL.save_attachment(m, "./attachments", overwrite=True)
    print(f"Saved attachments: {attachments}")

attachments = EMAIL.save_attachments('SUBJECT "Testing attachments"', "./attachments", overwrite=True)
print(f"Saved attachments: {attachments}")

run output

Saved attachments: ['attachments\\Elennith.pdf', 'attachments\\keywords_in_outline.png', 'attachments\\meili.msg']
Saved attachments: ['attachments\\Elennith.pdf', 'attachments\\keywords_in_outline.png', 'attachments\\meili.msg']

Question: Can you provide more details on these messages ? Was the .msg attachment the only attachment in that specific email ?