import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.header import Header
from email import encoders

mail_host = {
    "host": 'smtp.qq.com',
    "port": 465
}
mail_user = {
    "user": 'admin@fanscloud.net',
    "pass": 'gyzzvmjdlofnbedj'
}
sender = "admin@fanscloud.net"

mailToList = [
    "1634991757@qq.com",
    "fanscloud@foxmail.com"
]
mailCcList = [
    "zhaoyafan@foxmail.com"
]
mailBcList = [

]
html = '''
<p>Python 邮件发送HTML格式文件测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
'''
message = MIMEMultipart()
messageFrom = Header()
messageFrom.append('云凡网络')
messageFrom.append('<admin@fanscloud.net>')
print(messageFrom)
message["From"] = messageFrom
message["To"] = ';'.join(mailToList)
message["Cc"] = ';'.join(mailCcList)
message["Bcc"] = ';'.join(mailBcList)
message['Subject'] = Header('主题333')
message.attach(MIMEText(html, 'html', 'utf-8'))


# 构造附件2，传送当前目录下的 runoob.txt 文件
# att2 = MIMEText(open('./1.xlsx', 'rb').read(), 'base64', 'utf-8')
# att2["Content-Type"] = 'application/octet-stream'
# file = Header('attachment; filename=')
#
# att2["Content-Disposition"] = file
# message.attach(att2)
#
#
# print(file)

mime = MIMEBase('application','octet-stream')
mime.add_header('Content-Disposition', 'attachment', filename='文件文件.xlsx')
mime.set_payload(open('./1.xlsx', 'rb').read())
# 用Base64编码:
encoders.encode_base64(mime)
# 添加到MIMEMultipart:
message.attach(mime)

print("加密后的发送内容\n", message.as_string())  # 打印输出加密后的发送内容
#
#
smtpObj = smtplib.SMTP_SSL(mail_host['host'], mail_host['port'])
# smtpObj.connect(mail_host['host'], mail_host['port'])  # 链接 SMTP 服务器
smtpObj.login(mail_user['user'], mail_user['pass'])  # 登录邮箱验证
smtpObj.sendmail(sender, mailToList, message.as_string())  # 发送邮件； "message" 通过 "as_string()" 进行发送内容字符串的加密
