Email Versender (Mit Anhängen)
Verfasst: Samstag 20. April 2024, 16:26
Auch wenn es davon schon viele gibt, wollte ich trotzdem meinen Email Versender vorstellen:
Eingelesen wird eine ini Datei der Art:
to, cc, bcc, anhang können mehrfach eingegeben werden
Code: Alles auswählen
#!/usr/bin/python3
import smtplib
import sys
ininame = sys.argv[1]
from email.message import EmailMessage
p_subject=""
p_from=""
p_to=[]
p_cc=[]
p_bcc=[]
p_txt=""
p_anhang=[]
with open(ininame) as datei:
inhalt=datei.read()
saetze=inhalt.splitlines()
for satz in saetze:
worte=satz.split("=")
if len(worte) >= 2:
parm=worte[0]
wert=worte[1]
if parm=="subject":
p_subject=wert
if parm=="from":
p_from=wert
if parm=="to":
x=["x"]
x[0] = wert
p_to += x
if parm=="cc":
x=["x"]
x[0] = wert
p_cc += x
if parm=="bcc":
x=["x"]
x[0] = wert
p_bcc += x
if parm=="txt":
p_txt = wert
if parm=="anhang":
x=["x"]
x[0] = wert
p_anhang += x
with open(p_txt) as datei:
p_txt=datei.read()
msg=EmailMessage()
msg.set_content(p_txt)
msg["Subject"]=p_subject
msg["From"]=p_from
msg["To"]=p_to
msg["Cc"]=p_cc
msg["Bcc"]=p_bcc
for file in p_anhang:
teile=file.split("/")
with open(file, 'rb') as fp:
data = fp.read()
msg.add_attachment(data,maintype='application',subtype='octet',filename=teile[-1])
with smtplib.SMTP("smtps.udag.de",587) as server:
server.starttls()
server.login("christian@myoggradio.org","???")
erg=server.send_message(msg)
print(erg)
Code: Alles auswählen
subject=Eine Test Test Mail=
from=christian@myoggradio.org=
to=christian@myoggradio.org=
cc=christian@edv-ehm.de=
bcc=myoggradio@gmail.com
txt=/home/christian/python/PythonMail/pythonmail.txt=
anhang=/home/christian/python/PythonMail/pythonmail.txt=