Python Forum
How can read and download the attachment from the mail using while loop in IMAPlib Py
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can read and download the attachment from the mail using while loop in IMAPlib Py
#1
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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to download TLS Report attachment blason16 6 455 Feb-26-2024, 07:36 AM
Last Post: Pedroski55
  Extract PDF Attachment from Gmail jstaffon 0 535 Sep-10-2023, 01:55 PM
Last Post: jstaffon
  Assistance debuging imaplib errors Linh_lee 1 1,011 Aug-21-2022, 05:57 PM
Last Post: Linh_lee
  I get attachment paperclip on email without any attachments monika_v 5 1,913 Mar-19-2022, 10:20 PM
Last Post: cosmarchy
  Trying to determine attachment file type before saving off.. cubangt 1 2,092 Feb-23-2022, 07:45 PM
Last Post: cubangt
  Help Needed | Read Outlook email Recursively & download attachment Vinci141 1 4,017 Jan-07-2022, 07:38 PM
Last Post: cubangt
  download with internet download manager coral_raha 0 2,880 Jul-18-2021, 03:11 PM
Last Post: coral_raha
  Clicking Every Page and Attachment on Website shoulddt 1 1,698 Jul-09-2021, 01:08 PM
Last Post: snippsat
  Mail issue Mihil 3 2,588 Dec-03-2020, 05:25 AM
Last Post: Mihil
  ATT00001 instead of PDF attachment to an email dominiklucas 0 3,725 Jul-25-2020, 04:05 PM
Last Post: dominiklucas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020