Let us learn how to send email in Python using SMTP (Simple Mail Transfer Protocol) via Gmail and Outlook email client with the complete configuration for the SMTP server.
What is the SMTP Server?
The acronym SMTP stands for Simple Mail Transfer Protocol. This protocol helps to transfer emails from one application to another application using IMAP/POP servers. Find more about how SMTP works here.
Python applications (clients) can use Python SMTP server libraries to send email from a client application using Gmail, Outlook and other email services.
We shall demonstrate how to send email using Python via Gmail and Outlook services. However, it is much easier to send emails in Python using Outlook service as compared to Gmail as the configurations are easier. Therefore, we will suggest you use Outlook email client.
What is SMTPLIB?
Python provides the smtplib module for sending emails. The smtplib module is an SMTP protocol client. It basically defines an SMTP client session object which can be used to send emails with an SMTP or ESMTP listener daemon.
Let us see the syntax to create an SMTP object in Python scripting. We shall be using the same Python smtplib object to send emails in Python using SMTP server.
1 2 3 | import smtplib smtpObj = smtplib.SMTP([host_name[, port_number[, local_host_name[, timeout]]]]) |
It is important to define the following two objects while sending emails in Python. Let us have a look at each of them.
- Host Name: A hostname is a label that is assigned to a machine connected to the Internet. The hostname can take up either the IP address or the domain name of the target system. We have two popular hosts for sending emails in Python via SMTP server mentioned below.
- smtp.gmail.com
- smtp-mail.outlook.com
- Port Number: A port number is a method of identifying a specific process to which an Internet or other network message is to be forwarded when it arrives at a server. We generally use port number 587 for sending emails.

Steps To Configure Google SMTP Server
Step 1: Enable Less Secure Apps feature in your Google account
Step 2: Configuring IMAP settings
- Go to Settings in your Gmail account.
- Click the Forwarding and POP/IMAP tab.
- Enable IMAP in the “IMAP Access” section and save the configuration.
Step 3: Configuring Python client code
- Server name: smtp.gmail.com
- Port: 587
- Encryption method: STARTTLS
Steps To Configure Outlook SMTP Server
Step 1: Configuring POP and IMAP settings
- Go to the security settings in the Outlook application. Ensure that POP Options is checked with YES.
- Once you’re done with enabling POP and IMAP settings, you have to use the following mentioned SMTP server and port number in your Python code to send emails.
Step 2: Configuring Python client code
- Server name: smtp-mail.outlook.com
- Port: 587
- Encryption method: STARTTLS
Note: This python script is written using Python IDLE on the Microsoft Windows operating system and the version is Python 3.6.
Method 1: Send Email in Python using Outlook service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders sender_email_address = 'SENDER EMAIL ADDRESS' sender_email_password = 'SENDER EMAIL PASSWORD' receiver_email_address = 'RECEIVER EMAIL ADDRESS' email_subject_line = 'Sample Python Email' msg = MIMEMultipart() msg['From'] = sender_email_address msg['To'] = receiver_email_address msg['Subject'] = email_subject_line email_body = 'Hello World. This is a Python Email using SMTP server with Outlook service.' msg.attach(MIMEText(email_body, 'plain')) email_content = msg.as_string() server = smtplib.SMTP('smtp-mail.outlook.com:587') server.starttls() server.login(sender_email_address, sender_email_password) server.sendmail(sender_email_address, receiver_email_address, email_content) server.quit() |
Method 2: Sending emails with attachment
Here, we have written the attached file in text format and placed it in the C drive where Python is installed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders sender_email_address = 'SENDER EMAIL ADDRESS' sender_email_password = 'SENDER EMAIL PASSWORD' receiver_email_address = 'RECEIVER EMAIL ADDRESS' email_subject_line = 'subject' msg = MIMEMultipart() msg['From'] = sender_email_address msg['To'] = receiver_email_address msg['Subject'] = email_subject_line email_body = 'Hello World. This is Python email sender application with Attachments.' msg.attach(MIMEText(email_body, 'plain')) filename = 'sample_file.txt' attachment_file = open('sample_file.txt', 'rb') part = MIMEBase('application', 'octet-stream') part.set_payload((attachment_file).read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment_file; filename = "+filename) msg.attach(part) email_body_content = msg.as_string() server = smtplib.SMTP('smtp-mail.outlook.com:587') server.starttls() server.login(sender_email_address, sender_email_password) server.sendmail(sender_email_address, receiver_email_address, email_body_content) server.quit() |
Method 3: Send Email in Python using Gmail service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders sender_email_address = 'SENDER EMAIL ADDRESS' sender_email_password = 'SENDER EMAIL PASSWORD' receiver_email_address = 'RECEIVER EMAIL ADDRESS' email_subject_line = 'Sample Python Email' msg = MIMEMultipart() msg['From'] = sender_email_address msg['To'] = receiver_email_address msg['Subject'] = email_subject_line email_body = 'Hello World.\nThis is a Python script to send emails using Gmail service.\n' msg.attach(MIMEText(email_body, 'plain')) email_content = msg.as_string() server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(sender_email_address, sender_email_password) server.sendmail(sender_email_address, receiver_email_address, email_content) server.quit() |
If you have any issues while writing a script to send email in Python using SMTP server, let us know about it in the comment section below.
More Python Programs
- Find() method in Python
- Vernam Cipher Python Program
- Python Database Connection
- Range Method in Python
- Python Sleep Method
Great tips for sharing
thank you life saver//