22
33def mark_as_read (service , query ):
44 messages_to_mark = search_messages (service , query )
5+ if len (messages_to_mark ) == 0 : # No emails found
6+ return print ("No emails found" )
7+ else :
8+ print ("=" * 50 )
9+ for message_id in messages_to_mark :
10+ msg = service .users ().messages ().get (userId = 'me' , id = message_id ['id' ], format = 'full' ).execute ()
11+ payload = msg ['payload' ]
12+ headers = payload .get ("headers" )
13+ if headers :
14+ # this section prints email basic info & creates a folder for the email
15+ for header in headers :
16+ name = header .get ("name" )
17+ value = header .get ("value" )
18+ if name == 'From' :
19+ # we print the From address
20+ print ("From:" , value )
21+ if name == "To" :
22+ # we print the To address
23+ print ("To:" , value )
24+ if name == "Subject" :
25+ # we print the Subject
26+ print ("Subject:" , value )
27+ if name == "Date" :
28+ # we print the date when the message was sent
29+ print ("Date:" , value )
30+ print ("=" * 50 )
31+ print ("MARKED AS READ" )
532 return service .users ().messages ().batchModify (
633 userId = 'me' ,
734 body = {
@@ -12,6 +39,33 @@ def mark_as_read(service, query):
1239
1340def mark_as_unread (service , query ):
1441 messages_to_mark = search_messages (service , query )
42+ if len (messages_to_mark ) == 0 : # No emails found
43+ return print ("No emails found" )
44+ else :
45+ print ("=" * 50 )
46+ for message_id in messages_to_mark :
47+ msg = service .users ().messages ().get (userId = 'me' , id = message_id ['id' ], format = 'full' ).execute ()
48+ payload = msg ['payload' ]
49+ headers = payload .get ("headers" )
50+ if headers :
51+ # this section prints email basic info & creates a folder for the email
52+ for header in headers :
53+ name = header .get ("name" )
54+ value = header .get ("value" )
55+ if name == 'From' :
56+ # we print the From address
57+ print ("From:" , value )
58+ if name == "To" :
59+ # we print the To address
60+ print ("To:" , value )
61+ if name == "Subject" :
62+ # we print the Subject
63+ print ("Subject:" , value )
64+ if name == "Date" :
65+ # we print the date when the message was sent
66+ print ("Date:" , value )
67+ print ("=" * 50 )
68+ print ("MARKED AS UNREAD" )
1569 return service .users ().messages ().batchModify (
1670 userId = 'me' ,
1771 body = {
@@ -27,9 +81,10 @@ def mark_as_unread(service, query):
2781 parser .add_argument ("-r" , "--read" , action = "store_true" , help = 'Whether to mark the message as read' )
2882 parser .add_argument ("-u" , "--unread" , action = "store_true" , help = 'Whether to mark the message as unread' )
2983
30- args = parser .parse_args ()
3184 service = gmail_authenticate ()
85+
86+ args = parser .parse_args ()
3287 if args .read :
33- mark_as_read (service , args .query )
88+ mark_as_read (service , '"' + args .query + '" and label:unread' )
3489 elif args .unread :
3590 mark_as_unread (service , args .query )
0 commit comments