-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendEmail.py
59 lines (45 loc) · 1.29 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from email.mime.multipart import MIMEMultipart
import smtplib
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr,formataddr
from email.mime.application import MIMEApplication
#发件人邮箱
asender = ""
#收件人邮箱
areceiver = ""
#抄送人邮箱
# acc = ""
#邮件主题
asubject = "这是一份测试邮件"
#发件人地址
from_addr = ""
#邮箱密码(授权码)
password = ""
#邮件设置
msg = MIMEMultipart()
msg["Subject"] = asubject
msg["to"] = areceiver
# msg["Cc"] = acc
msg["from"] = asender
#邮件正文
body = "你好,这是一封测试邮件"
#添加邮件正文
msg.attach(MIMEText(body,'plain','utf-8'))
#添加附件
#注意,这里的文件路径是分割线
filename = "测试文档.xlsx"
xlsxpart = MIMEApplication(open(r'/Users/Desktop/testData/saleReport.txt','rb').read())
xlsxpart.add_header('Content-Disposition', 'attachment',filename="测试文档.xlsx")
msg.attach(xlsxpart)
#设置邮箱服务器地址及端口
smtp_server= "smtp.qq.com"
server = smtplib.SMTP(smtp_server,25)
#打印日志
server.set_debuglevel(1)
#登录邮箱
server.login(from_addr,password)
server.sendmail(from_addr,areceiver.split(",")+acc.split(","),msg.as_string())
#断开服务器连接
server.quit()