Bist du auf der suche nach einem Python- oder Shellskript, was automatische E-Mails verschicken kann? Dann bist du hier genau richtig!
Als aller erstes muss das folgende Skript wie beschrieben angepasst werden.
Welche Variablen müssen geändert werden?
- sender_email => Beispiel: [email protected]
- recipient_emails => Beispiel: [email protected]
- smtp_server => Beispiel: smtp.example.com (kommt auf Anbieter an)
- smtp_port => Beispiel: 587 (kommt auf Anbieter an)
- smtp_username => Beispiel: [email protected]
- smtp_password => Beispiel: x1bf4wqp8xd2nvih (=> Anleitung siehe Video)
- subject => Beispiel: Wartungsarbeiten
- html_content => Beliebig anpassbar
#!/usr/bin/env python3
# E-Mail - example
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# configuration
sender_email = '[YOUR_EMAIL_SENDER]' # SMTP E-mail sender
recipient_emails = ['[YOUR_EMAIL_RECEIVER_1]', '[YOUR_EMAIL_RECEIVER_2]'] # List of all recipients of the email
smtp_server = 'smtp.example.com' # SMTP Server
smtp_port = 587 # SMTP Server Port
smtp_username = '[email protected]' # SMTP E-mail sender
smtp_password = 'x1x2x3x4x5' # SMTP App-password
subject = 'Lorem ipsum' # Email subject
# HTML message
html_content = """
<!DOCTYPE html>
<html>
<head>
<style>
table.top {
padding: 25px;
border: 1px solid black;
background-color: #F4F6F7;
margin: 0;
border-radius: 10px;
text-align: left;
align-items: center;
justify-content: center;
width: 85%;
margin-left: auto;
margin-right: auto;
}
#status_link:link {
color: #1F618D;
}
#status_link:visited {
color: #1F618D;
}
#status_link:hover {
color: red;
}
#status_link:active {
color: green;
}
#mail_link:link {
text-decoration: none;
color: grey;
}
#mail_link:visited {
text-decoration: none;
color: grey;
}
#mail_link:hover {
text-decoration: underline;
color: blue;
}
#mail_link:active {
text-decoration: underline;
color: blue;
}
</style>
</head>
<body>
<table class="top">
<tr>
<td colspan="2" style="text-align: center; font-size: 25px; font-weight: 900; color: #229954">DEINE ÜBERSCHRIFT</td>
</tr>
<tr>
<td colspan="2" style="text-align: center; font-size: 20px; font-weight: 800; color: #2980B9">DEINE UNTERÜBERSCHRIFT</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td class="p1" colspan="2">Sehr geehrte Damen und Herren,</td>
</tr>
<tr>
<td class="p1" colspan="2">lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</td>
</tr>
<tr>
<td class="p1" colspan="2">At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</td>
</tr>
<tr>
<td class="p2" colspan="2"> </td>
</tr>
<tr>
<td class="p1" colspan="2">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</td>
</tr>
<tr>
<td class="p2" colspan="2"> </td>
</tr>
<tr>
<td class="p1" colspan="2">At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</td>
</tr>
<tr>
<td class="p1" colspan="2"><a id="status_link" href="#" target="_blank">Lorem ipsum</a></td>
</tr>
<tr>
<td class="p2" colspan="2"> </td>
</tr>
<tr>
<td class="p1" colspan="2">At vero eos et accusam et justo duo dolores et ea rebum.</td>
</tr>
<tr>
<td class="p2" colspan="2"> </td>
</tr>
<tr>
<td class="p1" colspan="2">Mit freundlichen Grüßen,<br>Ihr Team</td>
</tr>
<tr>
<td class="p2" colspan="2"> </td>
</tr>
<tr>
<td colspan="2" style="color: grey">
<p class="p1">Diese E-Mail wurde automatisch versandt.<br>Bitte nicht auf diese E-Mail antworten.<br>Verwenden Sie für Fragen diese E-Mail: <a id="mail_link" href="mailto:[email protected]" target="_blank">[email protected]</a></p>
</td>
</tr>
</table>
<div style="padding: 5px; text-align:center; color: grey; font-size: 10px">© 2023</div>
</body>
</html>
"""
# Connect to the SMTP server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(smtp_username, smtp_password)
for recipient_email in recipient_emails:
# Create the MIME message
msg = MIMEMultipart('alternative')
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = subject
# Adding the HTML content to the message
html_part = MIMEText(html_content, 'html')
msg.attach(html_part)
# Sending the e-mail
server.sendmail(sender_email, recipient_email, msg.as_string())
# Close connection to SMTP server
server.quit()
Welche Variablen müssen geändert werden?
- SMTPFROM => Beispiel: [email protected]
- SMTPTO => Beispiel: [email protected]
- SMTPSERVER => Beispiel: smtp.example.com (kommt auf Anbieter an)
- SMTPUSER => Beispiel: [email protected]
- SMTPPASS => Beispiel: x1bf4wqp8xd2nvih (=> Anleitung siehe Video)
- SUBJECT => Beispiel: Wartungsarbeiten
- MESSAGEBODY => Beliebig anpassbar
#!/bin/bash
# SKRIPT - prototyp
SMTPFROM="[email protected]"
SMTPTO="[email protected]"
SMTPSERVER="smtp.example.com:587"
SMTPUSER="[email protected]"
SMTPPASS="x1x2x3x4x5"
SUBJECT="Lorem ipsum"
#
MESSAGEBODY=$(cat <<EOF
<!DOCTYPE html>
<html>
<head>
<style>
table.top {
padding: 25px;
border: 1px solid black;
background-color: #F4F6F7;
margin: 0;
border-radius: 10px;
text-align: left;
align-items: center;
justify-content: center;
width: 85%;
margin-left: auto;
margin-right: auto;
}
#status_link:link {
color: #1F618D;
}
#status_link:visited {
color: #1F618D;
}
#status_link:hover {
color: red;
}
#status_link:active {
color: green;
}
#mail_link:link {
text-decoration: none;
color: grey;
}
#mail_link:visited {
text-decoration: none;
color: grey;
}
#mail_link:hover {
text-decoration: underline;
color: blue;
}
#mail_link:active {
text-decoration: underline;
color: blue;
}
</style>
</head>
<body>
<table class="top">
<tr>
<td colspan="2" style="text-align: center; font-size: 25px; font-weight: 900; color: #229954">DEINE ÜBERSCHRIFT</td>
</tr>
<tr>
<td colspan="2" style="text-align: center; font-size: 20px; font-weight: 800; color: #2980B9">DEINE UNTERÜBERSCHRIFT</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td class="p1" colspan="2">Sehr geehrte Damen und Herren,</td>
</tr>
<tr>
<td class="p1" colspan="2">lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</td>
</tr>
<tr>
<td class="p1" colspan="2">At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</td>
</tr>
<tr>
<td class="p2" colspan="2"> </td>
</tr>
<tr>
<td class="p1" colspan="2">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</td>
</tr>
<tr>
<td class="p2" colspan="2"> </td>
</tr>
<tr>
<td class="p1" colspan="2">At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</td>
</tr>
<tr>
<td class="p1" colspan="2"><a id="status_link" href="#" target="_blank">Lorem ipsum</a></td>
</tr>
<tr>
<td class="p2" colspan="2"> </td>
</tr>
<tr>
<td class="p1" colspan="2">At vero eos et accusam et justo duo dolores et ea rebum.</td>
</tr>
<tr>
<td class="p2" colspan="2"> </td>
</tr>
<tr>
<td class="p1" colspan="2">Mit freundlichen Grüßen,<br>Ihr Team</td>
</tr>
<tr>
<td class="p2" colspan="2"> </td>
</tr>
<tr>
<td colspan="2" style="color: grey">
<p class="p1">Diese E-Mail wurde automatisch versandt.<br>Bitte nicht auf diese E-Mail antworten.<br>Verwenden Sie für Fragen diese E-Mail: <a id="mail_link" href="mailto:[email protected]" target="_blank">[email protected]</a></p>
</td>
</tr>
</table>
<div style="padding: 5px; text-align:center; color: grey; font-size: 10px">© 2023</div>
</body>
</html>
EOF
)
# SEND E-MAIL
sendEmail -f $SMTPFROM -t $SMTPTO -u $SUBJECT -m $MESSAGEBODY -s $SMTPSERVER -xu $SMTPUSER -xp $SMTPPASS
Wenn nun das Pythonskript bearbeitet wurde, müssen wir jetzt nur noch Unraid einrichten.
Unraid benötigt hier nun Python-Pakete.
Diese können wir über (mindestens) zwei Wege installieren.
Bei der ersten Möglichkeit installieren wir ein zusätzliches Tool aus den Unraid Community Applications. Das Tool was ich meine, nennt sich NerdTools.
Dafür müssen wir nur im Unraid Apps-Tab nach diesem Tool suchen.
Wenn ihr dieses Tool (auch siehe Bild) gefunden habt, installiert es.

Jetzt sollte dieses Plugin erfolgreich auf euerem Unraid Server installiert sein.
Doch ein Schritt ist noch nötig.
Geht jetzt unter eueren „SETTINGS“ auf „NerdTools“.
Hier müssen wir jetzt nach drei Paketen Ausschau halten:
- python-pip-xx.x.x
- python-setuptools-xx.x.x
- python3-x.x.xx

https://raw.githubusercontent.com/ich777/un-get/master/un-get.plg
Bestätige das Laden mit dem klick auf „INSTALL“.
Nachdem das Plugin nun erfolgreich installiert sein sollte, öffne ein neues Terminal von Unraid und gebe folgenden Befehl ein:
un-get
Jetzt nutzen wir den folgenden Befehl, um das Plugin zu aktualisieren:
un-get update
Jetzt können wir python3 mit folgendem Befehl installieren:
un-get install python3 python-pip
Nachdem wir nun Python3 über einer der zwei Möglichkeiten installiert haben, müssen wir nur noch das Skript in Unraid einfügen.
Hierfür nutzen wir ein weiteres Plugin, dieses nennt sich „User Scripts“ und ist ebenfalls im Apps-Tab, also den Community Applications von Unraid verfügbar.
Das Menü hierfür erreichen wir, in dem wir unter dem Tab „SETTINGS“ auf „User Scripts“ navigieren.
Hier können wir jetzt ein neues Skript hinzufügen. Dies geht mit einem klick auf „ADD NEW SCRIPT“.
Noch einen Namen festlegen, wie zum Beispiel „PythonMail“, und das Skript ist angelegt.
Jetzt können wir das bearbeitete Skript vom Anfang einfügen, in dem wir unter dem gerade angelegtem Skript auf das Zahnrad klicken und hier „EDIT SCRIPT“ auswählen.
Jetzt kommt noch eine Wichtige Sache: Unraid schreibt in die erste Zeile Standardmäßig „#!/bin/bash“. Diese Zeile muss entfernt werden.
In den nun leeren Editor können wir das kopierte Skript einfügen.
Jetzt speichern wir das Skript mit „SAVE CHANGES“.
Das sollte es jetzt auch schon gewesen sein.
Wenn wir nun das Skript ausführen wollen, können wir entweder einen Cron-job dafür anlegen, um es regelmäßig zu starten, oder wir starten es manuell.
Für den ersten Test können wir es manuell über den Button „RUN SCRIPT“ starten.
Im E-Mail-Postfach sollte das wie folgt aussehen:
So einfach ist es, E-Mails automatisiert -oder auch manuell- über ein Pythonskript bzw. Shell-/Bashskript zu versenden.