Send Email Using Python

Kailash Chandra Behera | Monday, January 11, 2021

Introduction

This article provides code snippets to send mail using python, the python email module is used in the code example to send an email with python.

email python

Python Gmail send email


 

Getting Started

The python email module(smtplib module) defines an SMTP client (called python SMTP client) session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

This module is a native module (inbuild with python), it does not require to install an external library to send email through python.

Send Mail Using Python

The following is the python code to send email with plain text and not attachment.

 # Sending a emails without attachments using Python.  
 # importing the required library(python email module).   
 import smtplib   
   
 # email sender python  
 sender = 'sender_email_id'  
   
 # Sender password  
 password="sender_password"  
   
 #email receiver python  
 receivers = ['receiver_email_id']  
 
 # message to be sent   
 message = "Content to be sent" 
   
 # creates python smtp server object  
 email = smtplib.SMTP('python smtp email:smtp_host_name', Port_Number)   
    
 # smtp login python  
 email.login(sender, password)   
        
 # python send email smtp   
 email.sendmail(sender, receivers, message)   
    
 # terminating the session   
 email.quit()  
   
   

Python Send Email

Python Send Email with Attachment

To send email via python with attachment, along with the smtplib library the following libraries need to be imported.

 from email.mime.multipart import MIMEMultipart   
 from email.mime.text import MIMEText   
 from email.mime.base import MIMEBase   
 from email import encoders   

The following code constain the python script to send email with attachment. The code contains the steps how to read the attachment from local computer and attachech it with mail body.

 # python send email with attachment  
   
 # importing the required library(python email module).  
 import smtplib   
 from email.mime.multipart import MIMEMultipart   
 from email.mime.text import MIMEText   
 from email.mime.base import MIMEBase   
 from email import encoders   
   
 # email sender python  
 sender = 'sender_email_id'  
   
 # Sender password  
 password="sender_password"  
   
 #email receiver python  
 receivers = ['receiver_email_id']  
   
 # MIMEMultipart   
 _Mail = MIMEMultipart()   
   
 # senders email address   
 _Mail['From'] = sender   
   
 # receivers email address   
 _Mail['To'] = receivers   
   
 # the subject of mail  
 _Mail['Subject'] = "subject_of_the_mail"  
 
  filename = "Path of file (file_name_with_extension)"  
   
 # the body of the mail   
 body = "body_of_the_mail"  
   
 #The body and the attachments for the mail  
 _Mail.attach(MIMEText(body, 'plain'))   
   

 # Open the file as binary mode with readonly  
 # rb is a flag for readonly   
 attachment = open(filename, "rb")   
   
 # MIMEBase  
 payload = MIMEBase('application', 'octet-stream')   
   
 # To change the payload into encoded form   
 payload .set_payload((attachment).read())   
   
 # encode into base64   
 encoders.encode_base64(payload )   
   
 #add payload header with filename  
 payload .add_header('Content-Disposition', "attachment; filename= %s" % filename)   
   
 # attach the instance 'p' to instance '_Mail'   
 _Mail.attach(payload )   
   
 #python smtp client  
 # creates python smtp client object  
 email = smtplib.SMTP('email_host_name', email_port_number)   
   
 #enable security  
 email.starttls()   
   
 # smtp login python  
 email.login(fromaddr, password)   
   
 # Converts the Multipart _Mail into a string   
 message = _Mail.as_string()   
   
 # python send email smtp   
 email.sendmail(sender, receivers, message)   
   
 # terminating the session   
 email.quit()      

Python Send Email

In the above code, the MIME objects are created in order to store the sender and receiver information and some other details(subject, body of mail). MIME is also needed to set the attachment with the mail.

SMTP Python Example

The following example contains python code to send mail from python using gmail SMTP server with attachment.

 # python send email with attachment  
   
 # importing the required library(python email module).  
 import smtplib   
 from email.mime.multipart import MIMEMultipart   
 from email.mime.text import MIMEText   
 from email.mime.base import MIMEBase   
 from email import encoders   
   
 # email sender python  
 sender = 'kailashsblogs@gmail.com'  
   
 # Sender password  
 password="###########"  
   
 #email receiver python  
 receivers = ['kailashsblogs@hotmail.com']  
   
 # MIMEMultipart   
 _Mail = MIMEMultipart()   
   
 # senders email address   
 _Mail['From'] = sender   
   
 # receivers email address   
 _Mail['To'] = receivers   
   
 # the subject of mail  
 _Mail['Subject'] = "python send email test"  
   
 # the body of the mail   
 body = "Test Python Gmail Send Email"  
   
 #The body and the attachments for the mail  
 _Mail.attach(MIMEText(body, 'plain'))   
   
 filename = "D:\document\testfile.txt"  
 # Open the file as binary mode with readonly  
 # rb is a flag for readonly   
 attachment = open(filename, "rb")   
   
 # MIMEBase  
 payload = MIMEBase('application', 'octet-stream')   
   
 # To change the payload into encoded form   
 payload .set_payload((attachment).read())   
   
 # encode into base64   
 encoders.encode_base64(payload )   
   
 #add payload header with filename  
 payload .add_header('Content-Disposition', "attachment; filename= %s" % filename)   
   
 # attach the instance 'p' to instance '_Mail'   
 _Mail.attach(payload )   
   
 #python smtp gmail  
 email = smtplib.SMTP('smtp.gmail.com', 587)   
   
 #enable security  
 email.starttls()   
   
 # smtp login python  
 email.login(fromaddr, password)   
   
 # Converts the Multipart _Mail into a string   
 message = _Mail.as_string()   
   
 # python send email gmail  
 email.sendmail(sender, receivers, message)   
   
 # terminating the session   
 email.quit()   
   

python smtp multiple recipients

Thanks