read emails from gmail python

Solutions on MaxInterview for read emails from gmail python by the best coders in the world

showing results for - "read emails from gmail python"
Antoine
25 Jul 2018
1# Python 3.8.0
2import smtplib
3import time
4import imaplib
5import email
6import traceback 
7# -------------------------------------------------
8#
9# Utility to read email from Gmail Using Python
10#
11# ------------------------------------------------
12ORG_EMAIL = "@gmail.com" 
13FROM_EMAIL = "your_email" + ORG_EMAIL 
14FROM_PWD = "your-password" 
15SMTP_SERVER = "imap.gmail.com" 
16SMTP_PORT = 993
17
18def read_email_from_gmail():
19    try:
20        mail = imaplib.IMAP4_SSL(SMTP_SERVER)
21        mail.login(FROM_EMAIL,FROM_PWD)
22        mail.select('inbox')
23
24        data = mail.search(None, 'ALL')
25        mail_ids = data[1]
26        id_list = mail_ids[0].split()   
27        first_email_id = int(id_list[0])
28        latest_email_id = int(id_list[-1])
29
30        for i in range(latest_email_id,first_email_id, -1):
31            data = mail.fetch(str(i), '(RFC822)' )
32            for response_part in data:
33                arr = response_part[0]
34                if isinstance(arr, tuple):
35                    msg = email.message_from_string(str(arr[1],'utf-8'))
36                    email_subject = msg['subject']
37                    email_from = msg['from']
38                    print('From : ' + email_from + '\n')
39                    print('Subject : ' + email_subject + '\n')
40
41    except Exception as e:
42        traceback.print_exc() 
43        print(str(e))
44
45read_email_from_gmail()
46
queries leading to this page
python send mail through gmailsending email in python using gmailsend gmail email pythonhow to send email in python gmailgmail email pythonsend gmail pythonopen gmail settings python emailreading my gmail mails pythonpython check gmail emailsend email with gmail pythonpython send emails gmailsending gmail mail from pythonsend email using python gmailsend gmail from pythonsend gmail email from pythonpython sending email gmailpython send gmail emailread gmail emails pythongmail api python read emailgmail python send emailhow to read gmail email using pythoncheck emails using gmail api pythonsend a mail from python gmailcheck read email gmail pythonhow to read email using python gmailread email gmail pythonfetch emails python gmailread gmail emails with pythonsend emails based on email in gmail pythonextracting email from gmail via pythonsend emails with gmail pythonpython send emails from gmailread email from gmail with pythonpython download emails from gmailpython gmail send email examplepython gmail get emailpython read gmail emailspython read gmailhow to send email python gmailsend email using gmail pythonpython send email gmailread emails in gmail pythonpython send email via gmailsend email to gmail pythonpython gmail send emailread gmail pythonsend gmail from pythongmail send email in pythonsend email python gmailpython get emails from gmailgmail api to read emails pythoncheck gmail mail using pythonhow to mail using python gmailsend email to gmail from pythonpython email gmailconnect python to gmail to read emailssend email using gmail in pythongmail python read emailgmail send email pythonhow to email using gmail smtp in pythongmail emails by pythonsend email with python gmailget email python gmailpython read gmail emailsend email python gmail import mailsend gmail email with pythonread gmail using pythonsend email via python gmailpython gmail emailread email from gmail using pythonpython send email to gmailread gmail from pythonmail from python to gmailpython send email through gmailsending mail through gmail using pythonsend email gmail pythonpython send mail gmailhow to read gmail using pythonsend the email to gmail using pythonread emails from gmail pythonsend email through python gmailread gmail with pythonread emails from python gmaildownload emails from gmail pythonpython gmail api read emailget gmail emails pythonpython mailer gmailhow to send mail in gmail using pythonhow to read gmail emails with pythonpython read email gmailgmail api python read emailpython gmail read emailpython read email from gmailgmail email client python download email from gmail pythonhow to read gmail with pythonload gmail email pythongmail send mail pythonpython read emails from gmailgmail email sender pythonsend email via python gmail 27read gmail messages pythongmail api read email pythonpython3 read gmailgmail send mail with pythonauto read gmail pythonhow to read emails from gmail using gmail api in pythonhow to read gmail messages pythonread gmail email api with pythonpython send mail using gmailread emails from gmail python