Skip to content

Commit e33c835

Browse files
committed
이메일 처리 모듈 추가
1 parent a34307f commit e33c835

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎bigpython/mail.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import email
55
from email.header import decode_header
66
import subprocess
7+
import getpass
78

89
from 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

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
description='Make things easier for data analysis and automation',
66
author='Lee Seongjoo',
77
author_email='seongjoo@codebasic.co',
8-
py_modules=['mail'],
8+
py_modules=['bigpython.mail'],
99
packages=['bigpython', 'bigpython.webscrap', 'bigpython.office'],
1010
package_dir={'bigpython':'bigpython'},
1111
install_requires=[

0 commit comments

Comments
 (0)