Fanny Zhingre
Fanny Zhingre

Reputation: 183

I want to mark the gmail messages Seen by imaplib

I want to parse some gmail emails by python. I want when the message is read to put it seen. I put this code but it is not marked as seen?

#read or seen email
 mail.store(i,'+FLAGS', '\\Seen')

Do you know how I can keep the email looking whether it is VIEWED?

mport imaplib,dateutil.parser
import email

###################### mail read code  ###################

mail=imaplib.IMAP4_SSL('imap.gmail.com',993)    #SMTPは993,POPは995
mail.login('[email protected]','12123')
mail.select('example.jp',readonly=True)   #mailbox select read only

#UNSEEN read mail
type,data=mail.search(None,'UNSEEN') 

for i in data[0].split():   #data loop
 ok,x=mail.fetch(i,'RFC822')    #mail information get
 ms=email.message_from_string(x[0][1].decode('iso-2022-jp'))    #pass get

 #from get
 ad=email.header.decode_header(ms.get('From'))
 ms_code=ad[0][1]
 if(ms_code!=None):
  address=ad[0][0].decode(ms_code)
  address+=ad[1][0].decode(ms_code)
 else:
  address=ad[0][0]
 
 #Title get
 sb=email.header.decode_header(ms.get('Subject'))
 ms_code=sb[0][1]
 if(ms_code!=None):
  sbject=sb[0][0].decode(ms_code)
 else:
  ms_code=sb[1][1]
  sbject=sb[1][0].decode(ms_code)
 
 #body get
 maintext=ms.get_payload()

 #read email
 mail.store(i,'+FLAGS', '\\Seen')

 print(sbject)
 print(address)
 print(maintext)

Upvotes: 1

Views: 287

Answers (2)

Max
Max

Reputation: 10985

If you open your mailbox read-only, you can't make changes to it, including storing flags:

mail.select('example.jp',readonly=True)   #mailbox 

Remove the read-only flag.

Upvotes: 1

sophros
sophros

Reputation: 16660

I guess you may want to have a read of Gmail API reference on labels but very likely you are in need to remove labels UNREAD from the e-mail you want to be marked as read. For that you would have to use REST API or googleapiclient Python library.

Upvotes: 1

Related Questions