python发邮件(文件内容在邮件中展现+带附件)
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 = "test@test.com"
mail_pass = "test"
mail_postfix = "test.com"
mail_to = "test@test.com"
def send_mail_test(mail_to,sub,filename):
me = "yw@threegene.com"
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 = "test@test.com"
mail_pass = "test"
mail_postfix = "test.com"
mail_to = "test@test.com"
def send_mail(mail_to,sub,filename):
me = "yw@threegene.com"
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 2.7 邮件显示附件内容
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import datetime
import time
mail_to = "huangxueliang@hxl.com"
mail_host = "mail.hxl.com:465"
mail_user = "yw@hxl.com"
mail_pass = "123456"
mail_postfix = "hxl.com"
mail_to = "huangxueliang@hxl.com"
now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days=-1)
def send_attach(mail_to,sub,file_name,file_shortname):
me = "yw@hxl.com"
msg = MIMEMultipart()
msg['Subject'] = sub
msg['From'] = me
msg['To'] = mail_to
filemsg = open(name=file_name).read()
part = MIMEText(filemsg, _charset="utf-8")
msg.attach(part)
part = MIMEApplication(open(file_name, 'rb').read())
part.add_header('Content-Disposition', 'attachment',filename=file_shortname)
msg.attach(part)
try:
s = smtplib.SMTP_SSL(mail_host)
s.login(mail_user, mail_pass)
s.sendmail(me, mail_to.split(','), msg.as_string())
s.close()
except Exception as e:
print("error:", e)
def get_filecount(file_name):
count = -1
for count, line in enumerate(open(file_name, 'rU')):
pass
count += 1
return count
if __name__ == '__main__':
now_time = datetime.datetime.now()
now_date = str(now_time.strftime('%Y%m%d'))
##file_name="/home/yeemiao/scripts/sendmail/redis/slowquery/log/slowquery_20240407"
file_name = 'C:\\Users\\huangxueliang\\PycharmProjects\\python27\\redis\\log\\slowquery_' + now_date
file_shortname = "redis_slowquery_" + str(now_time.strftime('%Y%m%d')) + ".log"
sub = "【redis监控】redis慢查询-" + str(now_time.strftime('%Y%m%d'))
file_row = get_filecount(file_name)
print(file_row)
if file_row > 0:
send_attach(mail_to, sub, file_name, file_shortname)