python 邮件发送
import smtplib
import time
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from activeMq.log import log_mail
from activeMq.monitor.acars_analysis.enterprise_wechat import wechat
log = log_mail.get_log()
def send_email(filename):
smtpserver = 'redair.icoremail.net'
user = '******@qq.comn'
password = '*****'
receives = ['******@qq.comn']
msg = MIMEMultipart()
subject = Header("欢迎光临", 'utf-8').encode()
msg["Subject"] = subject
msg["From"] = user
msg["To"] = ','.join(receives)
msg.attach(MIMEText('附件为,请查收!,',
_subtype='html', _charset='utf-8'))
part = MIMEApplication(open(filename, 'rb').read())
file = filename.split('/')[-1]
part.add_header('Content-Disposition', 'attachment', filename=file)
msg.attach(part)
nowTime = time.strftime('%Y-%m-%d %H:%M:%S')
try:
s = smtplib.SMTP(smtpserver)
s.login(user, password)
s.sendmail(user, receives, msg.as_string())
log.info("%s,文件%s发送到邮件%s:" % (nowTime, file, receives))
print("%s,文件%s发送到邮件%s:" % (nowTime, file, receives))
s.close()
except Exception as e:
print(nowTime, "邮件发送失败:" + str(e))
log.info("%s,文件%s发送到邮件%s失败,异常为%s:" % (nowTime, file, receives, str(e)))
msg = nowTime + "给PTMD.AIRTRAVEL@Honeywell.com邮件发送失败,异常为" + str(e)
wechat.wechat_msg(msg).person()
if __name__ == '__main__':
send_email('E:/RedAir/code/redair/Acars/Acars_monitor/activeMq/acarsXLS/acars 20200718172955_20200718112955.xls')