-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail_sending.py
More file actions
executable file
·44 lines (39 loc) · 1.57 KB
/
mail_sending.py
File metadata and controls
executable file
·44 lines (39 loc) · 1.57 KB
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
36
37
38
39
40
41
42
43
44
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import smtplib
from email.mime.text import MIMEText
def send_mail(username, passwd, recv, title, content, mail_host='smtp.126.com', port=465, file=None):
'''
发送邮件函数,默认使用163smtp
:param username: 邮箱账号 xx@163.com
:param passwd: 邮箱密码
:param recv: 邮箱接收人地址,多个账号以逗号隔开
:param title: 邮件标题
:param content: 邮件内容
:param mail_host: 邮箱服务器
:param port: 端口号
:return:
'''
if file:
msg = MIMEMultipart()
# 构建正文
part_text = MIMEText(content)
msg.attach(part_text) # 把正文加到邮件体里面去
# 构建邮件附件
part_attach1 = MIMEApplication(open(file, 'rb').read()) # 打开附件
part_attach1.add_header('Content-Disposition', 'attachment', filename=file) # 为附件命名
msg.attach(part_attach1) # 添加附件
else:
msg = MIMEText(content) # 邮件内容
msg['Subject'] = title # 邮件主题
msg['From'] = username # 发送者账号
msg['To'] = recv # 接收者账号列表
smtp = smtplib.SMTP_SSL(mail_host, port=port)
smtp.login(username, passwd) # 登录
smtp.sendmail(username, recv, msg.as_string())
smtp.quit()
if __name__ == '__main__':
for i in range(1, 103):
file_name = "aa" + format(i, '0>3d')
print(file_name + " is sending...")
send_mail("", "", "", "", "", mail_host="", file=file_name)