I made a Python program that sends an attachment to a mailbox which is the main mailbox of my project. The mail is sent from the mailbox itself to the mailbox itself, so it is the same address for the receiver and for the sender. (see on pic)
here is my code which works from attachment that is not coming from the same mailbox. And I just can't get the attachment. Making another mail box is annoying.
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()
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 :
That's why it was not detected as attachement by Imbox. Now it look like this and it work perfectly :
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) here
If it's not clear enough or/and someone need futher explanation ask me.