Skip to main content
This article demonstrates how to send e-mail messages using Python.
ImportantYou cannot use external SMTP servers to send e-mail messages if you have one of the following hosting packages:
  • Shared web hosting
  • Reseller hosting
  • Managed WordPress hosting
For these hosting packages, you must use hosting.com servers. Other hosting packages have fewer restrictions, and can use some external SMTP servers to send e-mail messages.

Using Python to send e-mail messages

Python’s smtplib module makes it easy to send e-mail messages from a script. To do this, you create an instance of the SMTP class, which contains all of the functionality you need to connect to a mail server and send messages. The following sample code demonstrates how to send an e-mail message from a hosting.com server using the SMTP class. In your own code, replace recipient@example.com with the intended message recipient, and sender@example.com with the return e-mail address. For the login() method, specify the account username and password that you want to use to authenticate to the SMTP server:
This script specifies the sender, recipient, and message. It then constructs an instance of the SMTP class. After the class instance is created, all you need to do is log in to the server and send the message using the sendmail() method. This is a very simple example that demonstrates basic functionality provided by the SMTP class. To view the official documentation for the smtplib module (including the SMTP class), please visit https://docs.python.org/3/library/smtplib.html.