import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from email.header import Header #分发邮箱地址 fromaddr = 'xxxxxxx@163.com' #分发邮箱授权码 password = 'xxxxxxxx' #收件邮箱地址 toaddrs = ['xxxxxxxx@qq.com', 'xxxxxxx@qq.com'] #邮件内容 content = '请查收您的小红书笔记' textApart = MIMEText(content) #邮件类型,这里目前不做修改,如果需要分发不同类型的邮件,可以修改这里 k=1 #邮件标题 kind_d={1:'小红书笔记',} kind=kind_d[k] if kind=='小红书笔记': #邮件附件路径 File = '/root/autodl-tmp/Content_detector/四季梦幻1.zip' Apart = MIMEApplication(open(File, 'rb').read()) #邮件附件名称 Apart.add_header('Content-Disposition', 'attachment', filename='附件:小红书笔记.zip') m = MIMEMultipart() m.attach(textApart) m.attach(Apart) m['Subject'] = kind m['From'] = Header(fromaddr) m['To'] = Header(",".join(toaddrs)) try: # 使用163邮箱的SMTP服务器 server = smtplib.SMTP('smtp.163.com', 25) # 或使用SSL连接 # server = smtplib.SMTP_SSL('smtp.163.com', 465) server.login(fromaddr, password) server.sendmail(fromaddr, toaddrs, m.as_string()) print('success') server.quit() except smtplib.SMTPException as e: print('error:', e)