Python - SMTP

Das Simple Mail Transfer Protocol (SMTP) ist ein Protokoll, das das Senden einer E-Mail und das Weiterleiten von E-Mails zwischen Mailservern übernimmt.

Python bietet smtplib Modul, das ein SMTP-Client-Sitzungsobjekt definiert, mit dem E-Mails an jeden Internetcomputer mit einem SMTP- oder ESMTP-Listener-Daemon gesendet werden können.

Ein SMTP-Objekt hat eine Instanzmethode namens sendmail, die normalerweise zum Versenden einer Nachricht verwendet wird. Es werden drei Parameter benötigt -

  • Der Absender - Eine Zeichenfolge mit der Adresse des Absenders.

  • Die Empfänger - Eine Liste von Zeichenfolgen, eine für jeden Empfänger.

  • Die Nachricht - Eine Nachricht als Zeichenfolge, die wie in den verschiedenen RFCs angegeben formatiert ist.

Beispiel

Hier ist eine einfache Möglichkeit, eine E-Mail mit dem Python-Skript zu senden. Probieren Sie es einmal aus -

#!/usr/bin/python3
import smtplib
sender = '[email protected]'
receivers = ['[email protected]']
message = """From: From Person <[email protected]>
To: To Person <[email protected]>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

Hier haben Sie eine einfache E-Mail mit einem dreifachen Anführungszeichen in die Nachricht eingefügt und dabei darauf geachtet, die Überschriften korrekt zu formatieren. Eine E-Mail erfordert aFrom, To, and a Subject header, separated from the body of the e-mail with a blank line.

To send the mail you use smtpObj to connect to the SMTP server on the local machine. Then use the sendmail method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these are not always used to route the mail).

If you are not running an SMTP server on your local machine, you can use smtplib client to communicate with a remote SMTP server. Unless you are using a webmail service (such as gmail or Yahoo! Mail), your e-mail provider must have provided you with the outgoing mail server details that you can supply them, as follows −

mail = smtplib.SMTP('smtp.gmail.com', 587)

Sending an HTML e-mail using Python

When you send a text message using Python, then all the content is treated as simple text. Even if you include HTML tags in a text message, it is displayed as simple text and HTML tags will not be formatted according to the HTML syntax. However, Python provides an option to send an HTML message as actual HTML message.

While sending an e-mail message, you can specify a Mime version, content type and the character set to send an HTML e-mail.

Example

Following is an example to send the HTML content as an e-mail. Try it once −

#!/usr/bin/python3
import smtplib
message = """From: From Person <[email protected]>
To: To Person <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test
This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""
try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"