import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication if __name__ == '__main__': fromaddr = '179xxx@qq.com' password = 'xxxxxx' toaddrs = ['179xxx@qq.com', '925xxx@qq.com'] #构建文字内容 content = 'hello, this is email content.' textApart = MIMEText(content,'plain','utf-8') #读取本地图片 imageFile = 'D:计算机二级dog.jpg' imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1]) imageApart.add_header('Content-Disposition', 'attachment', filename=imageFile) #读取本地文档 pdfFile = 'D:计算机二级chicago.csv' pdfApart = MIMEApplication(open(pdfFile, 'rb').read()) pdfApart.add_header('Content-Disposition', 'attachment', filename=pdfFile) m = MIMEMultipart() m.attach(textApart) m.attach(imageApart) m.attach(pdfApart) m['Subject'] = 'title' #发送邮件 try: server = smtplib.SMTP('smtp.qq.com',25) server.login(fromaddr,password) server.sendmail(fromaddr, toaddrs, m.as_string()) print('success') server.quit() except smtplib.SMTPException as e: print('error:',e) #打印错误