Bottom Page

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 = 'vsdf@cs.com'
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 "dsv@fv.com")')

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")
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  imaplib problem levann 3 201 Jul-15-2019, 10:05 AM
Last Post: levann
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 135 May-18-2019, 09:29 PM
Last Post: Lastwizzle
  o365 special subject mail download issue anna 3 220 May-16-2019, 07:16 PM
Last Post: micseydel
  smtplib mail without subject anna 2 200 Apr-24-2019, 05:44 AM
Last Post: anna
  How to get python to download YouTube videos in the background? Pythenx 11 741 Mar-25-2019, 04:57 AM
Last Post: samsonite
  Download multiple large json files at once halcynthis 0 253 Feb-14-2019, 08:41 AM
Last Post: halcynthis
  Import Library but Download first? OscarBoots 15 1,337 Feb-07-2019, 03:07 AM
Last Post: snippsat
  Question about for loop not creating an infinite loop. FWendeburg 1 294 Feb-03-2019, 08:45 PM
Last Post: ichabod801
  Download data from webpage after POST request AlDe 0 230 Feb-02-2019, 06:26 AM
Last Post: AlDe
  Mail sending Python - What it's wrong? airwawekz 1 299 Jan-30-2019, 03:20 PM
Last Post: gontajones

Forum Jump:


Users browsing this thread: 1 Guest(s)