44import email
55from email .header import decode_header
66import subprocess
7+ import getpass
78
89from imapclient import IMAPClient
910
@@ -18,6 +19,12 @@ def _get_config(self):
1819 config .read (self .configpath )
1920 return config
2021
22+ def get_credential (self , section = 'DEFAULT' ):
23+ config = self .config
24+ username = config [section ]['username' ]
25+ password = config [section ]['password' ]
26+ return username , password
27+
2128 def get_access_token (self , section , refresh = False ):
2229 config = self .config
2330 if refresh :
@@ -87,6 +94,17 @@ def oauth2_login(self, user, access_token):
8794 self .select_folder ()
8895 return result
8996
97+ def login (self , default_mailbox = 'INBOX' , configfile = None ):
98+ if configfile is None :
99+ username = input ('이메일 계정: ' )
100+ password = getpass .getpass (prompt = '비밀번호: ' )
101+
102+ config = EmailConfig (configfile )
103+ username , password = config .get_credential ()
104+
105+ self ._imapclient .login (username , password )
106+ return self .select_folder (folder = default_mailbox )
107+
90108 def logout (self ):
91109 return self ._imapclient .logout ()
92110
@@ -125,3 +143,52 @@ def _decode_header(self, header):
125143 content = content .decode (encoding if encoding else 'utf-8' )
126144 content_list .append (content )
127145 return content_list
146+
147+ def get_text (self , message_or_message_id ):
148+ if type (message_or_message_id ) == int :
149+ mid , message = self .get_messages (message_or_message_id )[0 ]
150+
151+ for part in message .walk ():
152+ content_type = part .get_content_type ()
153+ if content_type .startswith ('text' ):
154+ text = part .get_payload (decode = True ).decode ('utf-8' )
155+ yield content_type , text
156+
157+ def show_attachments (self , message_or_message_id ):
158+ if type (message_or_message_id ) == int :
159+ mid , message = self .get_messages (message_or_message_id )[0 ]
160+
161+ attachment_list = []
162+ if not message .is_multipart ():
163+ return attachment_list
164+
165+ for part in message .walk ():
166+ content_type = part .get_content_type ()
167+ if content_type .startswith ('text' ): continue
168+ filename = part .get_filename ()
169+ if filename :
170+ attachment_list .append (filename )
171+
172+ return attachment_list
173+
174+ def download_attachments (self , message_or_message_id , target_dir = None ):
175+ if type (message_or_message_id ) == int :
176+ mid , message = self .get_messages (message_or_message_id )[0 ]
177+
178+ file_list = []
179+ for part in message .walk ():
180+ content_type = part .get_content_type ()
181+ if content_type .startswith ('text' ): continue
182+ filename = part .get_filename ()
183+ if not filename : continue
184+
185+ if target_dir :
186+ if not os .path .exists (target_dir ):
187+ raise FileNotFoundError ('{} 경로가 존재하지 않습니다.' .format (target_dir ))
188+ filename = os .path .join (target_dir , filename )
189+
190+ with open (filename , 'wb' ) as f :
191+ f .write (part .get_payload (decode = True ))
192+ file_list .append (os .path .abspath (filename ))
193+
194+ return file_list
0 commit comments