‘’’
import smtplib
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes
from email import encoders
mailuser=“11111111@qq.com”
mailpassword=“gpkzfhmoqwgqcjbf”
sender=“11111111@qq.com”
receiver=[“11111111@qq.com”]
smtp= smtplib.SMTP_SSL(“smtp.qq.com”,465)
smtp.login(mailuser, mailpassword)
message=MIMEMultipart()
message[“From”] = Header(f"kafeidou<{sender}>“)
message[“To”] = Header(‘,’.join(receiver))
message[“Subject”] = Header(“图片”)
mailContent=MIMEText(f"图片”,“plain”,“utf-8”)
message.attach(mailContent)
“”"
att1=MIMEText(open(‘sample.xlsx’,‘rb’).read(),‘base64’,‘utf-8’)
att1[“Content-Type”]=‘application/octet-stream’
att1[“Content-Disposition”] = ‘attachment; filename=“sample.xlsx”’
message.attach(att1)
att2=MIMEText(open(r"E:\base.txt",“rb”).read(),‘base64’,‘utf-8’)
att2[“Content-Type”]=‘application/octet-stream’
att2[“Content-Disposition”] = ‘attachment; filename=“base.txt”’
message.attach(att2)
“”"
#附件太大发送不了
filepath=“F:\奶牛.zip”
data = open(filepath, ‘rb’)
ctype, encoding = mimetypes.guess_type(filepath)
if ctype is None or encoding is not None:
ctype = ‘application/octet-stream’
maintype, subtype = ctype.split(‘/’, 1)
att3 = MIMEBase(maintype, subtype)
att3.set_payload(data.read())
data.close()
encoders.encode_base64(att3) # 把附件编码
att3.add_header(‘Content-Disposition’, ‘attachment’, filename=“奶牛.zip”) # 修改邮件头
message.attach(att3)
try:
smtp.sendmail(sender, receiver, message.as_string())
print(“发送成功”)
except smtplib.SMTPException:
print(“Error:无法发送邮件”)
print(smtplib.SMTPException)
dic={}
for i in receiver:
dic[“To”]=Header(f"<{i}>")
print(dic[“To”])
‘’’