TABLE 20.2 The SMTP Class Methods
To send an email message, you should follow these steps:
- Instantiate an SMTP class object, using the remote server connection information.
- Send an EHLO message to the remote server.
- Place the connection into TSL security mode.
- Send the login information for the server.
- Create the message to send.
- Send the message.
- Quit the connection.
The next section walks through each of these steps in creating a simple Python script for sending out
email messages.
Using the smtplib Library
The first step in the process of creating a Python script for sending out email messages is to instantiate
the SMTP object class. When you do this, you need to provide the host name and port address
required to connect to your email server, like this:
Click here to view code image
import smtplib
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
By the Way: Remote Email Servers
This example shows the host name and port used for the popular Gmail email server.
Most email servers have specific host names and ports that you can use to connect to
send email messages via email clients such as smart phone apps instead of using the
web interface. You should be able to find that information on the Frequently Asked
Questions (FAQ) webpages for your email server. If not, you may have to contact the
tech support group for your email server to get that information.
After you instantiate the SMTP object class, you can start the login process. For the login()