-
Notifications
You must be signed in to change notification settings - Fork 40
/
sendEmail.py
35 lines (32 loc) · 1.35 KB
/
sendEmail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import smtplib
import time
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from randomString import get_random_str
from util import *
if not os.path.exists("./comics"):
os.mkdir('./comics')
if not os.path.exists("./zips"):
os.mkdir('./zips')
zip_file("./comics", "./zips")
zips = os.listdir('./zips')
for i in range(len(os.listdir('./zips'))):
smtpObj = smtplib.SMTP(os.environ["EMAIL_SERVER_HOST"], os.environ["EMAIL_SERVER_PORT"])
if os.environ["EMAIL_STARTTLS"] == 'true':
smtpObj.starttls()
email_account = os.environ["EMAIL_ACCOUNT"]
smtpObj.login(email_account, os.environ["EMAIL_AUTH_CODE"])
zipFile = zips[i]
msg = MIMEMultipart()
msg['From'] = Header(email_account)
msg['Subject'] = Header('pica comics | ' + generate_random_str(), 'utf-8')
att = MIMEText(open('./zips/' + zipFile, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="' + zipFile + '"'
msg.attach(att)
msg.attach(MIMEText(get_random_str(), 'html', 'utf-8'))
smtpObj.sendmail(email_account, email_account, msg.as_string())
# 短时间频繁发邮件容易被邮件服务器检测到, 邮件越多则下封邮件的间隔时间越长
time.sleep(i * 10)
smtpObj.quit()