Skip to content

Commit 5d73427

Browse files
committed
이메일 전송 기능 추가
1 parent 077ab4b commit 5d73427

2 files changed

Lines changed: 76 additions & 26 deletions

File tree

‎bigpython/mail.py‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from email.header import decode_header
66
import subprocess
77
import getpass
8+
import smtplib
89

910
from imapclient import IMAPClient
1011

@@ -68,16 +69,22 @@ def update_config(self, verbose=False):
6869
class EmailClient:
6970
def __init__(self,
7071
host,
71-
port=None,
7272
use_uid=True,
73-
ssl=False,
73+
imap_port=None,
74+
imap_ssl=True,
75+
smtp_port=None,
76+
smtp_ssl=True,
7477
stream=False,
7578
ssl_context=None,
7679
timeout=None,
77-
method=None,
78-
debug=False):
79-
self.debug = debug
80-
self._imapclient = IMAPClient(host, ssl=ssl)
80+
method=None):
81+
82+
self.host = host
83+
84+
self._imapclient = self._connect_imap_server(imap_ssl, imap_port)
85+
86+
self._smpt_server = self._connect_smtp_server(smtp_ssl, port=smtp_port)
87+
8188
if method:
8289
if 'oauth2' in method:
8390
oauth2_config = method['oauth2']
@@ -88,6 +95,23 @@ def __init__(self,
8895
self.mailconfig.access_token)
8996
print(login_result)
9097

98+
def _connect_imap_server(self, ssl=True, port=None):
99+
host = '.'.join(['imap', self.host])
100+
return IMAPClient(host, ssl=ssl)
101+
102+
def _connect_smtp_server(self, ssl=True, port=0):
103+
if ssl:
104+
SMTP = smtplib.SMTP_SSL
105+
else:
106+
SMTP = smtplib.SMTP
107+
108+
smtp_server = SMTP(
109+
host='.'.join(['smtp', self.host]),
110+
port=port)
111+
112+
return smtp_server
113+
114+
91115
def oauth2_login(self, user, access_token):
92116
result = self._imapclient.oauth2_login(user, access_token)
93117
if 'Success' in result[0].decode('utf-8'):
@@ -101,6 +125,8 @@ def login(self, default_mailbox='INBOX', configfile=None):
101125

102126
config = EmailConfig(configfile)
103127
username, password = config.get_credential()
128+
self.username = username
129+
self.from_addr = '{}@{}'.format(username, self.host)
104130

105131
self._imapclient.login(username, password)
106132
return self.select_folder(folder = default_mailbox)
@@ -192,3 +218,22 @@ def download_attachments(self, message_or_message_id, target_dir=None):
192218
file_list.append(os.path.abspath(filename))
193219

194220
return file_list
221+
222+
def send_message(self, to_addrs, msg, from_addr=None,
223+
mail_options=[]):
224+
225+
smtp_server = self._smpt_server
226+
227+
password = getpass.getpass(prompt='비밀번호: ')
228+
smtp_server.login(self.username, password)
229+
230+
if not from_addr:
231+
from_addr = self.from_addr
232+
233+
msg = email.message_from_string(msg)
234+
ressult = smtp_server.send_message(msg=msg,
235+
from_addr=from_addr,
236+
to_addrs=to_addrs,
237+
mail_options=mail_options)
238+
239+
smtp_server.quit()

‎tests/test_mail.py‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,55 @@ def get_config(configfile='login.cfg'):
1212
return configfile
1313

1414
@pytest.fixture(scope='module')
15-
def imapclient(request):
16-
host = getattr(request.module, 'host', 'imap.naver.com')
17-
imapclient = EmailClient(host, ssl=True)
18-
imapclient.login(configfile=get_config())
19-
return imapclient
15+
def mailclient(request):
16+
host = getattr(request.module, 'host', 'naver.com')
17+
mailclient = EmailClient(host)
18+
mailclient.login(configfile=get_config())
19+
return mailclient
2020

2121
def test_create_emailclient():
22-
assert EmailClient('imap.naver.com', ssl=True) != None
22+
assert EmailClient('naver.com') != None
2323

2424
def test_login():
25-
host = 'imap.naver.com'
26-
imapclient = EmailClient(host, ssl=True)
27-
assert imapclient.login(configfile=get_config())
25+
host = 'naver.com'
26+
mailclient = EmailClient(host)
27+
assert mailclient.login(configfile=get_config())
2828

29-
def test_search(imapclient):
30-
message_ids = imapclient.search(['SUBJECT', '빅파이'])
31-
for header in imapclient.get_subjects(message_ids):
29+
def test_search(mailclient):
30+
message_ids = mailclient.search(['SUBJECT', '빅파이'])
31+
for header in mailclient.get_subjects(message_ids):
3232
assert '빅파이' in header['Subject']
3333

34-
def test_get_text(imapclient):
35-
message_ids = imapclient.search(['SUBJECT', '빅파이'])
34+
def test_get_text(mailclient):
35+
message_ids = mailclient.search(['SUBJECT', '빅파이'])
3636
for mid in message_ids:
37-
for content_type, text in imapclient.get_text(mid):
37+
for content_type, text in mailclient.get_text(mid):
3838
assert content_type.startswith('text')
3939

40-
def test_download_attachments(imapclient):
41-
message_ids = imapclient.search(['SUBJECT', '빅파이'])
40+
def test_download_attachments(mailclient):
41+
message_ids = mailclient.search(['SUBJECT', '빅파이'])
4242

4343
download_files = []
4444

4545
for mid in message_ids:
46-
attachment_list = imapclient.download_attachments(mid)
46+
attachment_list = mailclient.download_attachments(mid)
4747
for filepath in attachment_list:
4848
assert os.path.exists(filepath)
4949
download_files.append(filepath)
5050

5151
for mid in message_ids:
5252
userhome = os.path.expanduser('~')
5353
target_dir = os.path.join(userhome, 'Downloads')
54-
attachment_list = imapclient.download_attachments(mid, target_dir=target_dir)
54+
attachment_list = mailclient.download_attachments(mid, target_dir=target_dir)
5555
for filepath in attachment_list:
5656
assert os.path.exists(filepath)
5757
download_files.append(filepath)
5858

59-
6059
for filepath in download_files:
6160
os.unlink(filepath)
61+
62+
63+
def test_sendmail(mailclient):
64+
msg = 'Subject: I am seongjoo\nThank you.'
65+
result = mailclient.send_message('seongjoo@codebasic.co', msg)
66+
assert not result

0 commit comments

Comments
 (0)