python 备份后邮件通知

#/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
import string
import time
import os
import datetime

#日期主题
log_time = time.localtime()
logfile = time.strftime("gitsvnbackup.%Y%m%d.log",log_time)

#前一天日志名字
now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days=-1)
yes_time_nyr = yes_time.strftime("gitsvnbackup.%Y%m%d.log")

#切换到指定目录
os.getcwd()
os.chdir('/backup/GitSvnBackup/logs')


def gitsvnback():
    with open(yes_time_nyr,"r") as f:
        HOST = "mail.xxxxl.net"
        SUBJECT = time.strftime('%Y%m%d    Git_svn_backup_messages',log_time) #调用日期主题
        TO = "mazhenkai@xxx.net"
        FROM = "gitsvn_bak@xxx.net"
        text = f.read()     #读日志传给正文
        BODY=string.join((
              "From:%s" %FROM,
                "To:%s" %TO,
                  "Subject:%s"%SUBJECT,
                    "",
                      text
                      ),"\r\n")

        server = smtplib.SMTP()
        server.connect(HOST,"25")
        server.starttls()
        server.login("gitxxxx","password") #发送方邮箱user,passwd
        server.sendmail(FROM,TO,BODY)
        server.quit()

posted @ 2018-08-06 12:16  NeilyoX  阅读(108)  评论(0编辑  收藏  举报