Ist es möglich mit Python ein Script zu bauen das automatisch eine EMail versendet (inkl. Datenanhang - 1 TXT Datei) und die nötigen Parameter mitzugeben (Absender, Empfänger, etc. ...) ?
falls ja womit mach ich das am besten (Befehlsmäßig)
mfg
KRIS
EMail Versand mit Parametern und Attachment
pwneD ! ;(
-
- User
- Beiträge: 39
- Registriert: Donnerstag 9. Dezember 2004, 22:02
- Wohnort: Muenchen
dumdidum. erster
Code: Alles auswählen
#!/usr/bin/python
# Copyright 2000 by Philippe Possemiers (ppossemiers@tijd.com)
# This script works together with the 'motion' program, written by Jeroen Vreeken and
# can be activated with his '-E' option (or through '/etc/motion.conf'). It mails the pictures
# that were generated to a predefined e-mail address. A dial-up connection is established
# when needed and is also properly closed afterwards.
# Change the variables in 'init' to match your setup.
# This software is licensed under the terms of the GNU General Public License (GPL).
import string, sys, types, tempfile, time, os
import mimetypes,mimetools,MimeWriter
import smtplib, shutil
class SmtpWriter:
def __init__(self):
#The mail server
self.__server = 'some_server.some_domain.com'
#From address
self.__fromAddress = 'some_user@some_domain.com'
#To Address
self.__toAddress = 'some_user@some_domain.com'
#Subject
self.__subject = 'Some fitting subject'
#Body text. This can be empty.
self.__body = 'Some text'
#The directory in which the images are saved (see /etc/motion.conf).
#!!!There should be no subdirectories in this directory!!!
#The path should be ended with a slash(/)
self.__imageDir = '/Some_directory/'
#The directory to which the images are moved after being sent (otherwise all of them would be sent every time)
#The path should be ended with a slash(/)
self.__ImageDirMoved = '/Some_directory2/'
def Message(self, sender="", subject="", recipient="", body="", attachments=[]):
tempFileName = tempfile.mktemp()
tempFile = open(tempFileName,'wb')
message = MimeWriter.MimeWriter(tempFile)
message.addheader("From",sender)
message.addheader("To", recipient)
message.addheader("Subject", subject)
message.flushheaders()
message.startmultipartbody('mixed')
submessage = message.nextpart()
fp = submessage.startbody('text/plain')
fp.write(body)
for attachFile in attachments:
fileName = attachFile
submessage = message.nextpart()
submessage.addheader("Content-Disposition", "attachment; filename=%s" % fileName)
ctype,prog = mimetypes.guess_type(fileName)
enctype = 'base64'
submessage.addheader("Content-Transfer-Encoding",enctype)
fp = submessage.startbody(ctype)
afp = open(self.__imageDir + fileName,'rb')
mimetools.encode(afp, fp, enctype)
message.lastpart()
tempFile.close()
return tempFileName
def getImages(self):
time.sleep(5) #Wait for some more pictures
attachList = os.listdir(self.__imageDir)
return attachList
def sendMessage(self):
aList = self.getImages()
if len(aList) > 0:
message = self.Message(self.__fromAddress, self.__subject, self.__toAddress, self.__body, self.getImages())
for f1 in aList:
shutil.copy(self.__imageDir + f1, self.__imageDirMoved)
for f2 in aList:
os.remove(self.__imageDir + f2)
sendFile = open(message, 'r')
sendString = sendFile.read()
sendFile.close()
os.remove(message)
o = os.system('/etc/ppp/ppp-on &')
k = os.system('netstat -rn | grep -F pp0')
while(k != 0) :
k = os.system('netstat -rn | grep -F pp0')
time.sleep(5) #Give mailserver some time
try:
server = smtplib.SMTP(self.__server)
server.sendmail(self.__fromAddress, self.__toAddress, sendString)
print "SmptWriter: Message sent\n"
finally:
server.quit()
o = os.system('/etc/ppp/ppp-off &')
writer = SmtpWriter()
writer.sendMessage()
sys.exit()
was soll ich sagen ...
danke sehr =)
ist zwar ein wenig komplex aber mal schauen was ich wie gebrauchen kann
danke sehr =)
ist zwar ein wenig komplex aber mal schauen was ich wie gebrauchen kann
pwneD ! ;(
-
- Python-Forum Veteran
- Beiträge: 16025
- Registriert: Freitag 20. Juni 2003, 16:30
- Kontaktdaten:
Gerold hat dazu auch schon ein Modul geschrieben, kannst es dir ja mal ansehen.Kris hat geschrieben:ist zwar ein wenig komplex aber mal schauen was ich wie gebrauchen kann
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice