python 发送附件
#!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: sksendmail.py #@time: 2018/8/20 13:37 import smtplib from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header import datetime import os import subprocess def sendmail(sbuject,text,to_addr,fj_rk_path,pdate): msg = MIMEMultipart() from_addr = 'frommail@outlook.com' password = 'xxxxx' msg['Subject'] = Header(sbuject, 'utf-8').encode() msg_text = MIMEText(text, 'plain', 'utf-8') msg.attach(msg_text) att1 = MIMEText(open(fj_rk_path, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = "attachment;filename=" + 'count'+str(pdate)+'.html' #添加附件 msg.attach(att1) smtp_server = 'smtp.office365.com' server = smtplib.SMTP(smtp_server, 587) server.starttls() server.login(from_addr, password) server.sendmail(from_addr, [to_addr], msg.as_string()) server.quit() if __name__ == '__main__': subprocess.call('/bin/bash /home/ok/count.sh', shell=True) cmd = 'find /home/ok/tmp/ -name *.html | sort -nr | head -1' file_name = subprocess.getoutput(cmd) mail_title = os.path.basename(file_name) now_time = datetime.datetime.now() pdate = now_time.strftime('%Y%m%d') sendmail( mail_title, '详见附件', 'xxxx@gmail.com', file_name, pdate) sendmail( mail_title, '详见附件', 'xxxx@163.com', file_name, pdate)