martinrusev / imbox

Python IMAP for Human beings
MIT License
1.18k stars 190 forks source link

I can't get the attachment from a mail sent in the mail box to the mail box #210

Closed InsomniakD closed 2 years ago

InsomniakD commented 3 years ago

I don't know if its clear in the title so i'll explain there. I made a python program that send an attachment to a mailbox which is the main mailbox of my project. The mail is sent from the mailbox to the mailbox so it is the same address for the receiver and for the sender. (see on pic) Untitled-1

here is my code wich work from attachment that not comming from the same mailbox.

import os
from imbox import Imbox
import traceback

host = "imap.gmail.com"
download_folder = "G:\DownloadFMail"
username = "example@gmail.com"
password = "123456"

mail = Imbox(host, username, password, ssl=True, ssl_context=None, starttls=False)
mailAttach = mail.messages(unread=True,sent_from = username)

for (uid, message) in mailAttach:
    mail.mark_seen(uid) # optional, mark message as read

    for idx, attachment in enumerate(message.attachments):
        try:
            att_fn = attachment.get('filename')
            download_path = f"{download_folder}/{att_fn}"
            print(download_path)
            with open(download_path, "wb") as fp:
                fp.write(attachment.get('content').read())
        except:
            print(traceback.print_exc())

mail.logout()
ThirumaranManivannan commented 3 years ago

I'm also having same issue. For me mail has PDF attachment but message.attachments empty. Anyone help

InsomniakD commented 2 years ago

Fixed this issue but the cause was coming from the way I send the attachement. Here i was using stmplib to send it but there was not any header for the attachement resulting to this in mail detail :

image

that's why it was not detected as attachement by Imbox now it look like this and it work perfectly :

image

and this is the code sample :

with open(zipfile, "rb") as attachment: payload = MIMEBase('application', 'octate-stream') payload.set_payload((attachment).read()) encoders.encode_base64(payload) #encode the attachment payload.add_header('Content-Disposition', 'attachment',filename="image.zip") message.attach(payload)

ask me if need help