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 344 Jul-15-2019, 10:05 AM
Last Post: levann
  o365 special subject mail download issue anna 3 337 May-16-2019, 07:16 PM
Last Post: micseydel
  Add png to the body of the email, not the attachment Ivan87 8 715 Dec-14-2018, 11:05 AM
Last Post: wavic
  Read from a file, get user input in a while loop lynden 8 990 Nov-05-2018, 12:31 PM
Last Post: lynden
  PYTHON - send email with attachment Error PYTHONDUDE 1 878 Feb-27-2018, 07:52 PM
Last Post: PYTHONDUDE
  Problem with sending text file as an attachment using this Gmail OAuth script downloaderfan 6 2,197 Feb-07-2018, 09:54 PM
Last Post: downloaderfan
  using attachment for trouble in a project sylas 10 1,926 Sep-04-2017, 06:31 PM
Last Post: sylas
  Simple e-mail sender, that automatically changes subject for every e-mail joker 2 1,103 Aug-24-2017, 09:45 AM
Last Post: joker
  Email Checker with IMAPlib ronaldrios 1 1,539 Jun-23-2017, 04:03 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)