ich möchte eine Email mit Anhang versenden habe mir hierfür auch ein kleines Skript geschrieben bei dem ich in der Konsole die Datei übergeben muss.
Mein Code:
Code: Alles auswählen
#!/usr/bin/python
#Sends all files per Mail
#Example usage: python sendMail.py file1.txt, file2.txt ...
import sys, smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import formatdate
from email import Encoders
_encoding = 'utf-8'
class SendMail(object):
mailtoadress = 'marcel.fromm@bwi-it.de'
mailfromadress = 'DGUV-V3@web.de'
smtpserver = 'smtp.web.de'
username = 'DGUV-V3'
password = 'Hollywood10'
def send(self, files):
# Gather information, prepare mail
to = self.mailtoadress
From = self.mailfromadress
#Subject contains preview of filenames
if len(files) <= 3: subjAdd = ','.join(files)
if len(files) > 3: subjAdd = ','.join(files[:3]) + '...'
subject = 'Pruefauftrag'
msg = self.prepareMail(From, to, subject, files)
#Connect to server and send mail
server = smtplib.SMTP(self.smtpserver)
server.ehlo() #Has something to do with sending information
server.starttls() # Use encrypted SSL mode
server.ehlo() # To make starttls work
server.login(self.username, self.password)
failed = server.sendmail(From, to, msg.as_string())
server.quit()
def prepareMail(self, From, to, subject, attachments):
msg = MIMEMultipart()
msg['From'] = From
msg['To'] = to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
# The Body message is empty
msg.attach( MIMEText("Test") )
for file in attachments:
#We could check for mimetypes here, but I'm too lazy
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
msg.attach(part)
#Delete created Tar
return msg
if __name__ == '__main__':
mail = SendMail()
# Send all files included in command line arguments
mail.send(sys.argv[1:])
python: can't open file '/home/pi/SendMail.py Pruefauftrag.xlsx': [Errno 2]No such file or directory:
Mein Skript hierfür sieht so aus:
Code: Alles auswählen
Import subprocess
Sub = subprocess.Popen (['python', '/home/pi/SendMail.py Pruefauftrag.xlsx'])
#Sub.kill()