python 3.6
#!/usr/bin/env python #coding=utf-8 import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header import datetime import time mail_host = "mail.test.com:465" mail_user = "[email protected]" mail_pass = "test" mail_postfix = "test.com" mail_to = "[email protected]" def send_mail_test(mail_to,sub,filename): me = "[email protected]" msg = MIMEMultipart() msg['From'] = me msg['To'] = mail_to subject = sub msg['Subject'] = Header(subject, 'utf-8') filemsg = open(file=filename, encoding='utf8').read() ##filemsg = open(name=filename).read() python 2.7的使用方法 msg.attach(MIMEText(filemsg, 'plain', 'utf-8')) try: s = smtplib.SMTP_SSL(mail_host) s.login(mail_user,mail_pass) s.sendmail(me, mail_to.split(','), msg.as_string()) s.quit() return True except Exception as e: print(str(e)) return False if __name__ == '__main__': now_time = datetime.datetime.now() now_date = str(now_time.strftime('%Y%m%d')) sub = "测试redis慢查询" + '('+now_date+')' filename='./log/test_slowquery_' + now_date send_mail_test(mail_to, sub, filename)
python 2.7
#!/usr/bin/env python #coding=utf-8 import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header import datetime import time import io mail_host = "mail.test.com:465" mail_user = "[email protected]" mail_pass = "test" mail_postfix = "test.com" mail_to = "[email protected]" def send_mail(mail_to,sub,filename): me = "[email protected]" msg = MIMEMultipart() msg['From'] = me msg['To'] = mail_to subject = sub msg['Subject'] = Header(subject, 'utf-8') print(filename) filemsg = open(name=filename).read() msg.attach(MIMEText(filemsg, 'plain', 'utf-8')) try: s = smtplib.SMTP_SSL(mail_host) s.login(mail_user,mail_pass) s.sendmail(me, mail_to.split(','), msg.as_string()) s.quit() return True except Exception,e: print(e) return False if __name__ == '__main__': now_time = datetime.datetime.now() now_date = str(now_time.strftime('%Y%m%d')) sub = "【test监控-试运行】慢查询:" + '('+now_date+')' filename='./log/zjs_slowquery_' + now_date send_mail(mail_to,sub,filename)
标签:发邮件,python,filename,msg,import,test,mail,now,邮件 From: https://www.cnblogs.com/hxlasky/p/18112963