Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

email
pyhton

How to send emails using Python

Educative Answers Team

Python is a very strong language that does not require an external library to send emails. It offers a native library SMTP lib ( Simple Mail Transfer Protocol). In this article, a Gmail account is going to be used.

svg viewer
svg viewer

Note: the smtplib module defines an SMTP client session object that can be used to send emails by creating a session. For a Gmail account, port 587 is used.


Example code

The example below shows how to send email without attachment

# Sending emails without attachments using Python.
# importing the required library. 
import smtplib 
  
# creates SMTP session 
email = smtplib.SMTP('smtp.gmail.com', 587) 
  
# TLS for security 
email.starttls() 
  
# authentication
# compiler gives an error for wrong credential. 
email.login("sender_email_id", "sender_password") 
  
# message to be sent 
message = "message_to_be_send"
  
# sending the mail 
email.sendmail("sender_email_id", "receiver_email_id", message) 
  
# terminating the session 
email.quit()

Example code

The below code shows how to send emails using an attachment.

In the code snippet below, the MIMEultipart object is created in order to append the subject, the body, and attachment(s) to the emails.

# Sending emails with attachments using Python  

# libraries to be imported 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.base import MIMEBase 
from email import encoders 

fromaddr = "email_address_of_the_sender"
toaddr = "email_address_of_the_receiver"

# MIMEMultipart 
msg = MIMEMultipart() 

# senders email address 
msg['From'] = fromaddr 

# receivers email address 
msg['To'] = toaddr 

# the subject of mail
msg['Subject'] = "subject_of_the_mail"

# the body of the mail 
body = "body_of_the_mail"

# attaching the body with the msg 
msg.attach(MIMEText(body, 'plain')) 

# open the file to be sent
# rb is a flag for readonly 
filename = "file_name_with_extension"
attachment = open("Path of the file", "rb") 

# MIMEBase
attac= MIMEBase('application', 'octet-stream') 

# To change the payload into encoded form 
attc.set_payload((attachment).read()) 

# encode into base64 
encoders.encode_base64(attc) 

attc.add_header('Content-Disposition', "attachment; filename= %s" % filename) 

# attach the instance 'p' to instance 'msg' 
msg.attach(attc) 

# creates SMTP session 
email = smtplib.SMTP('smtp.gmail.com', 587) 

# TLS for security 
email.starttls() 

# authentication 
email.login(fromaddr, "Password_of_the_sender") 

# Converts the Multipart msg into a string 
message = msg.as_string() 

# sending the mail 
email.sendmail(fromaddr, toaddr, message) 

# terminating the session 
s.quit() 

Strong recommendation: reset the setting once you are done sending the email. In order to run the above code in a perfect manner, the Gmail account needs the security permission shown below::

svg viewer

RELATED TAGS

email
pyhton
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring