jeudi 14 juin 2018

New to Python, trying to create a program that prints Gmail to a web page

This is my code currently, I am using Google Cloud SDK and Python 2.7:

import webapp2
import smtplib
import time
import imaplib
import email


class ReadMail(webapp2.RequestHandler):
    def get(self):

        mail = imaplib.IMAP4_SSL(imap.gmail.com)
        mail.login('xxx@gmail.com','xxx')
        mail.select('inbox')

        type, data = mail.search(None, 'ALL')
        mail_ids = data[0]

        id_list = mail_ids.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):
            typ, data = mail.fetch(i, '(RFC822)' )

            for response_part in data:
                if isinstance(response_part, tuple):
                    msg = email.message_from_string(response_part[1])
                    email_subject = msg['subject']
                    email_from = msg['from']
                    self.response.headers['Content-Type'] = 'text/plain'
                    self.response.write('Hello World?')
                    self.response.write('From :') + email_from + '\n'
                    self.response.write('Subject :') + email_subject + '\n'




app = webapp2.WSGIApplication([
    ('/', ReadMail),
], debug=True)

However I get this error, and the web-page does not load (HTML 500 error):

ERROR    2018-06-14 07:30:02,217 wsgi.py:279]
Traceback (most recent call last):
  File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 267, in Handle
    result = handler(dict(self._environ), self._StartResponse)
TypeError: 'module' object is not callable
INFO     2018-06-14 10:30:02,878 module.py:846] default: "GET / HTTP/1.1" 500 -
INFO     2018-06-14 10:30:08,711 module.py:414] [default] Detected file changes:
  hello.pyc

I see the issue is with calling the modules, but since I am completely new to python it is hard for me to understand how this works. Any help would be appreciated!




Aucun commentaire:

Enregistrer un commentaire