生成文件时自动发送电子邮件

时间:2016-07-29 18:54:17

标签: python bash scripting

我正在尝试编写一个python脚本,在生成特定文件时自动发送电子邮件。我想我有发送电子邮件的代码,但我不知道如何监视查找特定文件的目录。

高级示例是:

从目录foo / 当填充文件baz时,请执行sendEmail()

3 个答案:

答案 0 :(得分:0)

import os.path
if os.path.exists(file_path) and os.path.isfile(fname):
   send_email()

答案 1 :(得分:0)

import smtplib,time

函数,用于监视生成your_file.txt的时间

def search_file():
    try:
        my_file=open('your_file.txt','r')
        content=my_file.read()
        my_file.close()
        return content
    except:
        print "Waiting..."
        time.sleep(20)
        search_log()                                                                                                     

服务器和端口

mail=smtplib.SMTP('smtp.gmail.com',587)

识别服务器ESMTP

mail.ehlo()

启动TLS模式=加密smtp进行登录

mail.starttls()
mail.login('user','pwd')



mail.sendmail('originator','receiver',search_log())

mail.close()

答案 2 :(得分:0)

另一种方式,它独立于文件所在的路径/目录:

import os
if 'your_file.txt' in os.listdir(os.getcwd()):
#do something
相关问题