Skip to content

Commit

Permalink
added
Browse files Browse the repository at this point in the history
  • Loading branch information
gokulapap committed Jun 17, 2021
0 parents commit 9086d5e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn app:app
62 changes: 62 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import email
import time
import imaplib
import traceback
from flask import Flask

app = Flask(__name__)

ORG_EMAIL = "@gmail.com"
FROM_EMAIL = "apgokul008" + ORG_EMAIL
FROM_PWD = "gokul!!8"
SMTP_SERVER = "imap.gmail.com"
SMTP_PORT = 993

@app.route('/')
def home():
return "<h1>App is working</h1>"

@app.route('/readmail')
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[10])

res = ''

print("="*50, '\n')
res = res + "--------- *Inbox of {}* ---------\n\n".format(FROM_EMAIL)
res = res + "="*52
res = res + "\n\n"
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']
res = res + 'From : ' + email_from + '\n'
res = res + 'Subject : ' + email_subject + '\n\n'
res = res + "="*52
res = res + "\n\n"
print('From : ' + email_from + '\n')
print('Subject : ' + email_subject + '\n')
print("="*52)
except Exception as e:
traceback.print_exc()
print(str(e))

return res


if __name__ == "__main__":
app.run(port=5000)

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gunicorn
flask

0 comments on commit 9086d5e

Please sign in to comment.