#相关库
from email.mime.base import MIMEBase
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
import smtplib,os
from_addr = '******'
password = '******'
to_addr = '*******'
# 发信服务器
smtp_server = '。。。。'
msg = MIMEMultipart()
# 邮件头信息
msg['From'] = Header(from_addr)
msg['To'] = Header(to_addr)
msg['Subject'] = Header('邮件测试')
# 取出文件夹中所有文件
allfilelist = os.listdir('.//images//feedBack')
#可以在content中添加一些文字说明
content = '
'
for index in range(len(allfilelist)):
#
content += '
'
msg.attach(MIMEText(content, 'html', 'utf-8'))
index = 0
for file in allfilelist:
#判断是否为文件
if os.path.isfile(f'.//images//feedBack//{file}'):
with open(f'.//images//feedBack//{file}', 'rb') as f:
mime = MIMEBase('image', 'png', filename=file)
mime.set_payload(f.read())
mime.add_header('Content-ID', '<' + str(index) + '>')
encoders.encode_base64(mime)
index += 1
msg.attach(mime)
f.close()
try:
server = smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server, 465)
server.login(from_addr, password)
# 发送邮件
server.sendmail(from_addr, to_addr, msg.as_string())
# 关闭服务器
server.quit()
except:
print("发送失败")