read emails from gmail python

# Python 3.8.0
import smtplib
import time
import imaplib
import email
import traceback 
# -------------------------------------------------
#
# Utility to read email from Gmail Using Python
#
# ------------------------------------------------
ORG_EMAIL = "@gmail.com" 
FROM_EMAIL = "your_email" + ORG_EMAIL 
FROM_PWD = "your-password" 
SMTP_SERVER = "imap.gmail.com" 
SMTP_PORT = 993

def read_email_from_gmail():
    try:
        mail = imaplib.IMAP4_SSL(SMTP_SERVER)
        mail.login(FROM_EMAIL,FROM_PWD)
        mail.select('inbox')

        data = mail.search(None, 'ALL')
        mail_ids = data[1]
        id_list = mail_ids[0].split()   
        first_email_id = int(id_list[0])
        latest_email_id = int(id_list[-1])

        for i in range(latest_email_id,first_email_id, -1):
            data = mail.fetch(str(i), '(RFC822)' )
            for response_part in data:
                arr = response_part[0]
                if isinstance(arr, tuple):
                    msg = email.message_from_string(str(arr[1],'utf-8'))
                    email_subject = msg['subject']
                    email_from = msg['from']
                    print('From : ' + email_from + '\n')
                    print('Subject : ' + email_subject + '\n')

    except Exception as e:
        traceback.print_exc() 
        print(str(e))

read_email_from_gmail()

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
send gmail from python read gmail email api with python check emails using gmail api python python send mail using gmail read email from gmail using python how to send mail in gmail using python python read email gmail gmail api to read emails python send the email to gmail using python send email with gmail python send gmail email with python gmail send mail python how to read gmail emails with python send gmail from python send gmail python python send email to gmail load gmail email python send email through python gmail sending mail through gmail using python send email using gmail in python how to read gmail using python python send mail through gmail send email to gmail from python python read emails from gmail send email with python gmail python send gmail email python read email from gmail check read email gmail python read gmail emails with python how to send email in python gmail how to read gmail messages python gmail send email python python read gmail emails download email from gmail python gmail email sender python get email Python Gmail download emails from gmail python send gmail email python python send email through gmail send email using python gmail python gmail email python check gmail email python read gmail how to mail using python gmail send email using gmail python python read gmail email gmail send mail with python how to read email using python gmail python gmail get email gmail email python read gmail python check gmail mail using python python gmail send email how to send email python gmail send email gmail python send email via python gmail python mailer gmail Send email Python Gmail read email gmail python python send emails gmail gmail email client python gmail api read email python gmail api python read email send gmail email from python python send email via gmail get gmail emails python gmail send email in python read gmail emails python connect python to gmail to read emails python send email gmail read gmail with python read gmail from python gmail python read email gmail Emails by python python get emails from gmail python3 read gmail python gmail read email read email from gmail with python send email via python gmail' python sending email gmail read gmail using python auto read gmail python read emails from gmail python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source