我需要弄清楚如何制作一个扫描目录中新文件的脚本,当有新文件时,通过电子邮件发送文件。
有人一直偷我的公寓大楼里的自行车!首先,这是我的错(我要锁定它),现在通过切割链升级的骗子。骗子在切断1/2英寸的飞机线后偷走了我的第二辆自行车。
无论如何,使用树莓派作为动作激活的安全摄像头,我希望它在视频节目完成录制后立即向我发送视频文件。这是因为他们偷了pi。
我正在查看这些示例,但我无法确定如何使脚本连续运行(每分钟)或如何使其扫描文件夹以获取新文件。
How do I send attachments using SMTP?
确定 我把它归结为扫描然后尝试发送电子邮件。尝试附加视频文件时失败。你能帮我吗?以下是修订后的代码:
失败的是:
msg = MIMEMultipart()
TypeError:' LazyImporter'对象不可调用,第38行
import time, glob
import smtplib
import email.MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders, MIMEMultipart
import os
#Settings:
fromemail= "Jose Garcia <somerandomemail@gmail.com>"
loginname="somerandomemail@gmail.com"
loginpassword="somerandomepassword"
toemail= "Jose Garcia <somerandomemail@gmail.com>"
SMTPserver='smtp.gmail.com'
SMTPort=587
fileslocation="/Users/someone/Desktop/Test/*.mp4"
subject="Security Notification"
def mainloop():
files=glob.glob(fileslocation) #Put whatever path and file format you're using in there.
while 1:
new_files=glob.glob(fileslocation)
if len(new_files)>len(files):
for x in new_files:
if x in files:
print("New file detected "+x)
print("about to call send email")
sendMail(loginname, loginpassword, toemail, fromemail, subject, gethtmlcode(), x, SMTPserver, SMTPort)
files=new_files
time.sleep(1)
def sendMail(login, password, to, frome, subject, text, filee, server, port):
# assert type(to)==list
# assert type(filee)==list
msg = MIMEMultipart()
msg['From'] = frome
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
# #for filee in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(filee,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(filee))
msg.attach(part)
smtp = smtplib.SMTP(SMTPserver, SMTPort)
smtp.sendmail(frome, to, msg.as_string() )
server.set_debuglevel(1)
server.starttls()
server.ehlo()
server.login(login, password)
server.sendmail(frome, to, msg)
server.quit()
def gethtmlcode():
print("about to assemble html")
html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '
html +='"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">'
html +='<body style="font-size:12px;font-family:Verdana"><p>A new video file has been recorded </p>'
html += "</body></html>"
return(html)
#Execute loop
mainloop()
答案 0 :(得分:1)
我终于开始工作了。我不得不使用python 2.5来摆脱LazyImporter错误。每次将新文件添加到安全文件夹时,都会通过电子邮件发送给我。记录已被破坏。
import time, glob
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEMultipart import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
import datetime
from email import Encoders
import os
import sys
#Settings:
fromemail= 'RaspberryPI Security Camera <someone>'
loginname='someone@gmail.com'
loginpassword='something'
toemail= 'Jose Garcia < someone@gmail.com>'
SMTPserver='smtp.gmail.com'
SMTPort=587
fileslocation='/Users/someone/Desktop/Test/*.*'
subject="Security Notification"
log='logfile.txt'
def mainloop():
files=glob.glob(fileslocation) #Put whatever path and file format you're using in there.
while 1:
f = open(log, 'w')
sys.stdout = Tee(sys.stdout, f)
new_files=glob.glob(fileslocation)
if len(new_files)>len(files):
for x in new_files:
if x in files:
print(str(datetime.datetime.now()) + "New file detected "+x)
sendMail(loginname, loginpassword, toemail, fromemail, subject, gettext(), x, SMTPserver, SMTPort)
files=new_files
f.close()
time.sleep(1)
def sendMail(login, password, send_to, send_from, subject, text, send_file, server, port):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
part = MIMEBase('application', "octet-stream")
part.set_payload( open(send_file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(send_file))
msg.attach(part)
smtp = smtplib.SMTP(SMTPserver, SMTPort)
smtp.set_debuglevel(1)
smtp.ehlo()
smtp.starttls()
smtp.login(login, password)
smtp.sendmail(send_from, send_to, msg.as_string() )
smtp.close()
def gettext():
text = "A new file has been added to the security footage folder. \nTime Stamp: "+ str(datetime.datetime.now())
return(text)
class Tee(object):
def __init__(self, *files):
self.files = files
def write(self, obj):
for f in self.files:
f.write(obj)
#Execute loop
mainloop()
答案 1 :(得分:1)
It looks like the email module has been refactored over time.这解决了我在Python 2.7上的'LazyImporter' object not callable
错误:
from email.mime.text import MIMEText
值得注意的是,我对import email; email.mime.text.MIMEText(...)
答案 2 :(得分:0)
将脚本放入循环中,让它睡眠60秒。您可以使用glob
获取目录中的文件列表。 in
对于查看列表中的内容(即目录中的文件列表)非常有用。
import time, glob
files=glob.glob("/home/me/Desktop/*.mp4") #Put whatever path and file format you're using in there.
while 1:
new_files=glob.glob("/home/me/Desktop/*.mp4")
if len(new_files)>len(files):
for x in new_files:
if x not in files:
print("New file: "+x) #This is where you would email it. Let me know if you need help figuring that out.
files=new_files
time.sleep(60)