Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create fetchEmails.py #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Add Code Here/PYTHON/fetchEmails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Python program to Fetch your gmail emails from a particular user
import imaplib, email

user = 'USER_EMAIL_ADDRESS'
password = 'USER_PASSWORD'
imap_url = 'imap.gmail.com'

def get_body(msg):
if msg.is_multipart():
return get_body(msg.get_payload(0))
else:
return msg.get_payload(None, True)

def search(key, value, con):
result, data = con.search(None, key, '"{}"'.format(value))
return data

def get_emails(result_bytes):
msgs = [] # all the email data are pushed inside an array
for num in result_bytes[0].split():
typ, data = con.fetch(num, '(RFC822)')
msgs.append(data)

return msgs

con = imaplib.IMAP4_SSL(imap_url)

con.login(user, password)

con.select('Inbox')

msgs = get_emails(search('FROM', 'MY_ANOTHER_GMAIL_ADDRESS', con))

for msg in msgs[::-1]:
for sent in msg:
if type(sent) is tuple:

content = str(sent[1], 'utf-8')
data = str(content)

try:
indexstart = data.find("ltr")
data2 = data[indexstart + 5: len(data)]
indexend = data2.find("</div>")

print(data2[0: indexend])

except UnicodeEncodeError as e:
pass