Python Forum

Full Version: How can read and download the attachment from the mail using while loop in IMAPlib Py
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can read the mails and get the new mails by using while loop, but i want download the attachment from the mail (and check for the new mails after downloading, if the new mail contains the attachment then i have to download that too). I'm really stuck on this.

Quote: imap_host = 'cas.com'
imap_user = '[email protected]'
imap_pass = 'sdfgsdfg5'
detach_dir = 'C:\\Users\\ddf\\Desktop\\we'

## open a connection
mail = imaplib.IMAP4_SSL(imap_host)

## login
typ, res = mail.login(imap_user, imap_pass)

if typ != 'OK':
print('Not able to sign in!')

while True:
mail.select('INBOX')

# Filter : Email Filter
print('SEARCH WITH EMAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
status, results = mail.search(None, '(FROM "[email protected]")')

if status == 'OK':
if results[0]:
mid = results[0].split()[0]
print('mail id', mid)
print(mail.fetch(mid, '(UID BODY[HEADER.FIELDS (FROM)])'))
time.sleep(6)

##Filter : Subject Filtering
print('SEARCH WITH SUBJECT>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
status, results = mail.search(None, '(SUBJECT "df")')

if status == 'OK':
if results[0]:
mid = results[0].split()[0]
print('mail id', mid)
print(mail.fetch(mid, '(UID BODY[HEADER.FIELDS (SUBJECT)])'))

a = mail.fetch(mid, '(RFC822)')

##download the attachment code have to be start here.



else:
print('No results yielded for the specified Subject') ## Subject checking ERROR!
time.sleep(6) ##Refresh to check mail
else:
print('unable to search', results)
time.sleep(6) ##Refresh to check mail



else:
print('No results yielded for the mentioned Email id') ## Email checking ERROR!
time.sleep(6) ##Refresh to check mail

else:
print('unable to search', results)
time.sleep(6) ##Refresh to check mail
[/quote]

The above code works fine and i can read the new mails from the server also.The below code for downloading the file . i combined both and i can download the file at once, and then it goes to while loop for checking new mails and i got error **Traceback (most recent call last): File "C:/Users/df/PycharmProjects/DemoReadmail/demo.py", line 23, in mail.select("INBOX" ) AttributeError: 'Message' object has no attribute 'select' ** i'm trying to solve this error for a long time, but i can't.. How can i solve this? Anyone can combine this and solve this error?
Quote: ##Downloading Attachment

for num in results[0].split():
typ, data = mail.fetch(mid, '(RFC822)')

mail = email.message_from_bytes(data[0][1]) ##Attachment Download starts here...
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
# print part.as_string()
continue
if part.get('Content-Disposition') is None:
# print part.as_string()
continue
fileName = part.get_filename()

if bool(fileName):
filePath = os.path.join(detach_dir, 'attachments', fileName)
if not os.path.isfile(filePath):
print
fileName
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
print("The file " + fileName + " is saved Successfully")