×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Nokia Developer Expert
    Join Date
    Mar 2003
    Posts
    47

    imaplib.py for pys60?

    Hi,

    Anyone ported imaplib.py to S60 Python? Just copying it to device and using it resulted error, so some porting would be needed.

  2. #2
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: imaplib.py for pys60?

    Did you get the error when importing it, or when calling a function from it?

  3. #3
    Nokia Developer Expert
    Join Date
    Mar 2003
    Posts
    47

    Re: imaplib.py for pys60?

    import went ok but the next line:
    server= IMAP4("my.mail.server")

    resulted (in imaplib.py):
    AttibuteError:'dict' object has no attribute 'pop'

    Before that it asked IAP and obviously tried to get connected.

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2008
    Location
    Ahmedabad, Gujarat, India
    Posts
    3,852

    Re: imaplib.py for pys60?

    hi havital
    if it was asking for the access point then i dont think there is any problem with the copying or porting of the imaplib.py. there can be problem with your code have a check.
    thanks

  5. #5
    Super Contributor
    Join Date
    Mar 2003
    Location
    Espoo, Finland
    Posts
    976

    Re: imaplib.py for pys60?

    Quote Originally Posted by haviital View Post
    AttibuteError:'dict' object has no attribute 'pop'
    Quite accurate error code! PyS60 is based on python 2.2.2, which does NOT have pop for dictionaries. It was added in python 2.3.

    pop() raises a KeyError when no default value is given and the key is not found. New in version 2.3.
    Some porting is needed.

    Cheers,

    --jouni

  6. #6
    Nokia Developer Expert
    Join Date
    Mar 2003
    Posts
    47

    Re: imaplib.py for pys60?

    Thanks Jouni, I indeed took that from Python 2.5.

    Now I tried with imaplib.py from Python 2.2.2 packet and it works!

    In case someone is interested here are the steps to get started:
    1) Copy imaplib.py to the E:\Python\lib\ folder in your memory card. The imaplib.py is found from here:
    http://www.python.org/ftp/python/2.2.2/Python-2.2.2.tgz

    2) Run the following script in Python shell. That giver you a list of your IMAP mailboxes.
    Add you mail server and account information.

    from imaplib import *
    server = IMAP4("your.mail.server")
    a,b=server.login("your_username","your_password")
    print "a=",a," b=",b
    mboxes = server.list()[1]
    print "mboxes=",mboxes


    Br,
    Hannu

  7. #7
    Regular Contributor
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    176

    Re: imaplib.py for pys60?

    thanks haviital!
    copied your imaplib.py to my phone and it works perfectly. However, IMAP4_SSL isnt available in this version.
    many web email accounts require SSL to access. does anyone know of any method to use IMAP over SSL in PyS60 v1.4.5?

    in python shell:
    Code:
    from imaplib import *
    m=IMAP4("imap.server.com")
    m.login("username","password")
    m.list()[1] #list available mailboxes
    m.select("INBOX") #select Inbox
    m.search(None,"ALL") #list mail ids of all messages in inbox
    m.search(None,"UNSEEN") #list mail ids of all unread messages in inbox
    
    m.fetch(mail_id,"(BODY[HEADER.FIELDS (FROM)])")[1]  #print sender details of specified message id
    m.fetch(mail_id,"(BODY[HEADER.FIELDS (SUBJECT)])")[1] #print subject of specified message id
    
    m.logout()

  8. #8
    Regular Contributor
    Join Date
    Oct 2007
    Posts
    114

    Re: imaplib.py for pys60?

    You can try the latest Python for S60 v1.9.3 which is based on Python 2.5.4 core. It has imaplib and should support SSL as well.
    import antigravity

  9. #9
    Regular Contributor
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    176

    Re: imaplib.py for pys60?

    ok so now the latest versions support IMAP. does anyone know how to extract the properly extract the plain text part of the message?

    Code:
    import email
    
    data=m.fetch(mail_id,"(RFC822)")[1][0][1]
    msg=rfc822.Message(data)
    
    msg=email.message_from_string(msg)
    msg1=msg.get_payload()[0]  #assuming it is multipart
    
    print msg1.get_payload() #output the message body to screen, but charset is us-ascii so wont print properly to screen
    Last edited by the86hitman; 2009-07-03 at 23:46.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    9

    Re: imaplib.py for pys60?

    Here's the code I use to get both HTML and plain text from an e-mail. By the way, the calls to M=imaplib.... and M.login both popup a request to select which access point I want to use. Does anyone know how to set the default access point for imaplib since it seems to be separate from "btsocket.set_default_access_point(apo)". I'm using 1.9.7.

    Code:
        try:
            M=imaplib.IMAP4_SSL('imap.gmail.com', 993)
            M.login('xxx@gmail.com','xxxxxxxx')
            status, count = M.select('Inbox')
            if str(count[0]) != "0":
                status, data = M.fetch(count[0], '(RFC822)')
                messagePlainText = ''
                messageHTML = ''
                for response_part in data:
                    if isinstance(response_part, tuple):
                        msg = email.message_from_string(response_part[1])
                        for part in msg.walk():
                            if str(part.get_content_type()) == 'text/plain':
                                messagePlainText = messagePlainText + str(part.get_payload())
                            if str(part.get_content_type()) == 'text/html':
                                messageHTML = messageHTML + str(part.get_payload())
                round.set(u'' + messagePlainText)
            M.close()
            M.logout()
        except:
            appuifw.note(u"Couldn't retrieve e-mail ", "info")

  11. #11
    Regular Contributor
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    176

    Re: imaplib.py for pys60?

    thanks for the code, will test it out later!

    similar to recent discussion here:
    if you're using PyS60 modules, u should use "btsocket".
    if using core python modules, use "socket".

    imaplib is part of the core python, therefore use socket.set_default_access_point()
    The86Hitman
    PyS60 1.9.7 on E71
    http://www.drhtailor.com/pys60

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×