E-Mails mit Python versenden
Durch die vielen verfügbaren Python-Module ist es auch ohne weiteres möglich, in einem Python-Skript eine E-Mail zu generieren und an einen beliebigen SMTP-Server für die Zustellung zu übergeben. Das folgende Beispiel soll zeigen wie Sie in Python eine Nachricht nach RFC 2822 erzeugen, eine Verbindung zum SMTP-Mailserver aufbauen, diese mit TLS verschlüsseln, sich mit Ihrem Account am Mailserver authentifizieren und letztendlich Ihre Nachricht an den SMTP-Server übergeben. Da das folgende Python-Skript sehr einfach gehalten wurde und ausführlich dokumentiert ist, möchte ich an dieser Stelle gar nicht weiter auf die einzelnen Befehle eingehen.
Für das folgende Python-Skript erstellen Sie mit einem Editor die Datei mail.py.
pi@raspberrypi ~ $ vi mail.py
Fügen Sie in diese Datei den folgenden Python-Code ein und speichern die Datei ab.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Creation: 16.08.2013
# Last Update: 15.01.2017
#
# Copyright (c) 2013-2017 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
#
# import required modules
#
from email.mime.text import MIMEText
import smtplib
import sys
#
# declaration of the default mail settings
#
# mail address of the sender
sender = 'sender@domain.de'
# fully qualified domain name of the mail server
smtpserver = 'smtp.domain.de'
# username for the SMTP authentication
smtpusername = 'MyMailAccount'
# password for the SMTP authentication
smtppassword = 'MyMailPassword'
# use TLS encryption for the connection
usetls = True
#
# function to send a mail
#
def sendmail(recipient,subject,content):
# generate a RFC 2822 message
msg = MIMEText(content)
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = subject
# open SMTP connection
server = smtplib.SMTP(smtpserver)
# start TLS encryption
if usetls:
server.starttls()
# login with specified account
if smtpusername and smtppassword:
server.login(smtpusername,smtppassword)
# send generated message
server.sendmail(sender,recipient,msg.as_string())
# close SMTP connection
server.quit()
#
# main function
#
def main():
# call sendmail() and generate a new mail with specified subject and content
sendmail('empfaenger@domain.de','Test','Dies ist eine Testnachricht.')
# quit python script
sys.exit(0)
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
#
# Creation: 16.08.2013
# Last Update: 15.01.2017
#
# Copyright (c) 2013-2017 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
#
# import required modules
#
from email.mime.text import MIMEText
import smtplib
import sys
#
# declaration of the default mail settings
#
# mail address of the sender
sender = 'sender@domain.de'
# fully qualified domain name of the mail server
smtpserver = 'smtp.domain.de'
# username for the SMTP authentication
smtpusername = 'MyMailAccount'
# password for the SMTP authentication
smtppassword = 'MyMailPassword'
# use TLS encryption for the connection
usetls = True
#
# function to send a mail
#
def sendmail(recipient,subject,content):
# generate a RFC 2822 message
msg = MIMEText(content)
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = subject
# open SMTP connection
server = smtplib.SMTP(smtpserver)
# start TLS encryption
if usetls:
server.starttls()
# login with specified account
if smtpusername and smtppassword:
server.login(smtpusername,smtppassword)
# send generated message
server.sendmail(sender,recipient,msg.as_string())
# close SMTP connection
server.quit()
#
# main function
#
def main():
# call sendmail() and generate a new mail with specified subject and content
sendmail('empfaenger@domain.de','Test','Dies ist eine Testnachricht.')
# quit python script
sys.exit(0)
if __name__ == '__main__':
main()
Nachdem Sie Das Python-Skript erstellt haben, ändern Sie die Rechte der Datei.
pi@raspberrypi ~ $ chmod 0755 mail.py
Jetzt können Sie mit dem folgenden Befehl eine E-Mail generieren und an den eingestellten SMTP-Mailserver übergeben.
pi@raspberrypi ~ $ ./mail.py
Dieser Eintrag wurde am 16.08.2013 erstellt und zuletzt am 15.01.2017 bearbeitet.
Direkter Link zu dieser Seite: http://www.gtkdb.de/index_31_2297.html
[ Zur Startseite ] [ Zur Kategorie ]
© 2004-2021 by Georg Kainzbauer