案例:发送E:\Python_script\目录下 logo.png图片文件到指定的邮箱
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
smtpserver='smtp.163.com'
user='yuexiaolu2015@163.com'
password='070337shu'
sender='yuexiaolu2015@163.com'
receives=['yuexiaolu2015@126.com','yuexiaolu2015@sina.com']
subject='Web Selenium 附件发送测试'
content='<html><h1 style="color:red">我要自学网,自学成才!</h1></html>'
send_file=open(r"E:\Python_script\logo.png",'rb').read()
att=MIMEText(send_file,'base64','utf-8')
att["Content-Type"]='application/octet-stream'
att["Content-Disposition"]='attachment;filename="logo.png"'
msgRoot=MIMEMultipart()
msgRoot.attach(MIMEText(content, 'html', 'utf-8'))
msgRoot['subject']=subject
msgRoot['From']=sender
msgRoot['To'] = ','.join(receives)
msgRoot.attach(att)
smtp = smtplib.SMTP_SSL(smtpserver, 465)
smtp.helo(smtpserver)
smtp.ehlo(smtpserver)
smtp.login(user,password)
print("Start send email...")
smtp.sendmail(sender,receives,msgRoot.as_string())
smtp.quit()
print("Send End!")