Skip to content

Commit e56196f

Browse files
authored
파이썬 이메일 발송 (파일첨부)
1 parent 3342a04 commit e56196f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

email_list.xlsx

-2.34 KB
Binary file not shown.

email_send_file.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os
2+
import smtplib
3+
from email.mime.text import MIMEText
4+
import openpyxl
5+
import datetime
6+
from email.encoders import encode_base64
7+
from email.header import Header
8+
from email.mime.base import MIMEBase
9+
from email.mime.multipart import MIMEMultipart
10+
from email.utils import formatdate
11+
12+
def email_send(data):
13+
_name = data[0]
14+
_email = data[1]
15+
_subject = data[2]
16+
_content = data[3]
17+
_file1 = data[4]
18+
_file2 = data[5]
19+
20+
_subject = _subject.replace("{이름}", _name)
21+
_content = _content.replace("{이름}", _name)
22+
23+
try:
24+
s = smtplib.SMTP('smtp.naver.com', 587)
25+
s.starttls()
26+
s.login('naver_id@naver.com', 'app_password')
27+
msg = MIMEMultipart()
28+
msg['Subject'] = Header(s=_subject, charset='utf-8')
29+
body = MIMEText(_content, _charset='utf-8')
30+
msg.attach(body)
31+
32+
files = list()
33+
if _file1: files.append(_file1)
34+
if _file2: files.append(_file2)
35+
36+
for f in files:
37+
part = MIMEBase('application', 'octect-stream')
38+
part.set_payload(open(f, "rb").read())
39+
encode_base64(part)
40+
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
41+
msg.attach(part)
42+
43+
msg['From'] = "naver_id@naver.com"
44+
msg['To'] = _email
45+
msg['Date'] = formatdate(localtime=True)
46+
s.send_message(msg)
47+
s.quit()
48+
return True
49+
50+
except Exception as e:
51+
print(e)
52+
return False
53+
54+
wb = openpyxl.load_workbook("email_list.xlsx")
55+
ws = wb.active
56+
for row in ws.rows:
57+
rownum = row[0].row
58+
if rownum == 1:
59+
continue
60+
data = (row[0].value, row[1].value, row[2].value, row[3].value, row[6].value, row[7].value )
61+
62+
if row[4].value == "성공":
63+
continue
64+
65+
print("email sending --> ", row[0].value, row[1].value)
66+
result = email_send(data)
67+
68+
dt = datetime.datetime.now()
69+
if result:
70+
ws['E'+str(rownum)] = "성공"
71+
ws['F'+str(rownum)] = dt.strftime("%Y-%m-%d %H:%M:%S")
72+
else:
73+
ws['E'+str(rownum)] = "실패"
74+
ws['F'+str(rownum)] = dt.strftime("%Y-%m-%d %H:%M:%S")
75+
76+
wb.save("email_list.xlsx")

0 commit comments

Comments
 (0)