Archiving emails in Gmail

Discussion in 'Python' started by teja, Jun 15, 2010.

  1. teja

    teja Guest

    Hi,

    I have a requirement that I want to log-in into a gmail account read
    all unread mails, mark them as read and then archive them.
    I am using libgmail (version 0.1.11) library to do so, using which I
    am able to log-in into a gmail account fetch all unread message and
    then read them one by one.
    Now my problem is that I am not able to mark the unread mail as read
    and archive it.

    Below is sample code that I am using.


    from libgmail import *

    ARCHIVE_ACTION='rc_^i' #the action string to archive a message
    UNREAD_MAILS = "is:unread"

    def ArchiveAll():
    ga = GmailAccount(name='username', pw='password')
    print 'logging in...',
    ga.login()
    print 'successful'

    def _getAllUnreadMails():
    return ga.getMessagesByQuery(UNREAD_MAILS, True)

    def _readMail(email):
    emailData = ga.getRawMessage(email)

    def _archiveAndMarkRead(email):
    ga._doThreadAction(ARCHIVE_ACTION, email)
    ga._doThreadAction(U_MARKREAD_ACTION, email)

    emails = _getAllUnreadMails()
    for email in emails:
    eData = _readMail(email)
    #Process email data
    _archiveAndMarkRead(email)

    print 'done'

    if __name__ == '__main__':
    ArchiveAll()

    after executing this code I am getting following error

    HTTP Error 500: Internal Server Error
    Traceback (most recent call last):
    File "test_libgmail.py", line 30, in <module>
    ArchiveAll()
    File "test_libgmail.py", line 26, in ArchiveAll
    [_archiveAndMarkRead(t) for t in sr[1]]
    File "test_libgmail.py", line 21, in _archiveAndMarkRead
    ga._doThreadAction(ARCHIVE_ACTION, thread)
    File "/home/3rdparty/libgmail/libgmail.py", line 669, in
    _doThreadAction
    items = self._parsePage(_buildURL(**params))
    File "/home/3rdparty/libgmail/libgmail.py", line 383, in _parsePage
    items = _parsePage(self._retrievePage(urlOrRequest))
    File "/home/3rdparty/libgmail/libgmail.py", line 99, in _parsePage
    lines = pageContent.splitlines()
    AttributeError: 'NoneType' object has no attribute 'splitlines'

    Can anyone help me in figuring out what's going wrong here?
    I guess google has deprecated this way of marking emails, correct me
    if I am wrong here.
    But if its true, is there any other way or library in Python to meet
    my requirements?

    Thanks in advance.
    teja, Jun 15, 2010
    #1
    1. Advertising

  2. * teja, on 15.06.2010 09:03:
    > Hi,
    >
    > I have a requirement that I want to log-in into a gmail account read
    > all unread mails, mark them as read and then archive them.
    > I am using libgmail (version 0.1.11) library to do so, using which I
    > am able to log-in into a gmail account fetch all unread message and
    > then read them one by one.
    > Now my problem is that I am not able to mark the unread mail as read
    > and archive it.
    >
    > Below is sample code that I am using.
    >
    >
    > from libgmail import *
    >
    > ARCHIVE_ACTION='rc_^i' #the action string to archive a message
    > UNREAD_MAILS = "is:unread"
    >
    > def ArchiveAll():
    > ga = GmailAccount(name='username', pw='password')
    > print 'logging in...',
    > ga.login()
    > print 'successful'
    >
    > def _getAllUnreadMails():
    > return ga.getMessagesByQuery(UNREAD_MAILS, True)
    >
    > def _readMail(email):
    > emailData = ga.getRawMessage(email)
    >
    > def _archiveAndMarkRead(email):
    > ga._doThreadAction(ARCHIVE_ACTION, email)
    > ga._doThreadAction(U_MARKREAD_ACTION, email)
    >
    > emails = _getAllUnreadMails()
    > for email in emails:
    > eData = _readMail(email)
    > #Process email data
    > _archiveAndMarkRead(email)
    >
    > print 'done'
    >
    > if __name__ == '__main__':
    > ArchiveAll()
    >
    > after executing this code I am getting following error
    >
    > HTTP Error 500: Internal Server Error
    > Traceback (most recent call last):
    > File "test_libgmail.py", line 30, in<module>
    > ArchiveAll()
    > File "test_libgmail.py", line 26, in ArchiveAll
    > [_archiveAndMarkRead(t) for t in sr[1]]


    In the code you show above there is no line

    [_archiveAndMarkRead(t) for t in sr[1]]

    Instead you have

    _archiveAndMarkRead(email)

    I tentatively conclude from this that the error is in your real code, as opposed
    to the code you've shown.



    > File "test_libgmail.py", line 21, in _archiveAndMarkRead
    > ga._doThreadAction(ARCHIVE_ACTION, thread)
    > File "/home/3rdparty/libgmail/libgmail.py", line 669, in
    > _doThreadAction
    > items = self._parsePage(_buildURL(**params))
    > File "/home/3rdparty/libgmail/libgmail.py", line 383, in _parsePage
    > items = _parsePage(self._retrievePage(urlOrRequest))
    > File "/home/3rdparty/libgmail/libgmail.py", line 99, in _parsePage
    > lines = pageContent.splitlines()
    > AttributeError: 'NoneType' object has no attribute 'splitlines'
    >
    > Can anyone help me in figuring out what's going wrong here?


    You're passing a None value.

    It originates somewhere in your real code, which it appears that you have not shown.


    > I guess google has deprecated this way of marking emails, correct me
    > if I am wrong here.
    > But if its true, is there any other way or library in Python to meet
    > my requirements?


    You can access GMail via an ordinary e-mail client. What's that called, POP
    protocol? I think the protocol for sending is SMTP. Any library handling those
    two protocols can be used.


    Cheers & hth.,

    - Alf, who, unfortunately, wrt. to your real code, is not a telepath :)

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach, Jun 15, 2010
    #2
    1. Advertising

  3. teja

    teja Guest

    Ohh my bad...

    thanks a lot for replying Alf..

    The error which I've pasted above, was thrown before I modified the
    code a bit..

    Here's the error thrown on running the code I've pasted above..
    there's not much of a difference in the error though.

    HTTP Error 500: Internal Server Error
    Traceback (most recent call last):
    File "test_libgmail.py", line 40, in <module>
    ArchiveAll()
    File "test_libgmail.py", line 35, in ArchiveAll
    _archiveAndMarkRead(email)
    File "test_libgmail.py", line 28, in _archiveAndMarkRead
    ga._doThreadAction(ARCHIVE_ACTION, email)
    File "/home/3rdparty/test/libgmail/libgmail.py", line 670, in
    _doThreadAction
    items = self._parsePage(_buildURL(**params))
    File "/home/3rdparty/test/libgmail/libgmail.py", line 384, in
    _parsePage
    items = _parsePage(self._retrievePage(urlOrRequest))
    File "/home/3rdparty/test/libgmail/libgmail.py", line 99, in
    _parsePage
    lines = pageContent.splitlines()
    AttributeError: 'NoneType' object has no attribute 'splitlines'

    I tried to debug the issue here, when self._retrievePage(urlOrRequest)
    is called (it is called internally in libgmail) a 500 internal server
    error is thrown. And hence further processing on the response can not
    be done.
    I also caught the url which throws 500:
    https://mail.google.com/mail/?ui=1&search=all&t=129374b41acebfcd&view=up&at=&act=rc_^i

    Now I don't know why 500 is thrown and on which side the exact problem
    lies, i.e. the libgmail side or the gmail side.

    And there's a email library provided in Python (supports both POP and
    SMTP) but I dont know whether it allows us to archive mails or mark
    them as read for that matter.
    teja, Jun 15, 2010
    #3
  4. On 06/15/2010 11:07 AM, teja wrote:
    > And there's a email library provided in Python (supports both POP and
    > SMTP) but I dont know whether it allows us to archive mails or mark
    > them as read for that matter.


    POP (the protocol) only allows downloading messages, AFAIK. SMTP is for
    sending. A more powerful e-mail protocol is IMAP, which gmail also
    supports. I don't know about Python libraries, you'll probably find a
    reasonably full-featured one, but IMAP supports marking messages as read.

    As for archiving, I'm guessing that this'd be implemented by moving
    messages to an "archive" folder.

    Maybe you should connect to gmail with an email client (Thunderbird is
    nice) via IMAP and check
    1. whether you can archive mails from there
    2. how exactly gmail archiving maps to IMAP
    and then implement it using IMAP, having your script just do the same
    thing you did in Thunderbird, WLM, Sylpheed, Mail.app, or whatever
    you're using.

    Have fun,
    Thomas
    Thomas Jollans, Jun 15, 2010
    #4
  5. On 2010-06-15, teja <> wrote:

    > I have a requirement that I want to log-in into a gmail account read
    > all unread mails, mark them as read and then archive them.
    > I am using libgmail (version 0.1.11) library to do so, using which I
    > am able to log-in into a gmail account fetch all unread message and
    > then read them one by one.
    > Now my problem is that I am not able to mark the unread mail as read
    > and archive it.


    I don't know what libgmail is, but I use IMAP for stuff like that
    (with Gmail and other servers). Python's imaplib is a bit low-level
    (and IMAP sucks rather badly as a protocol), so you might want to take
    a look at imaplib2.

    http://docs.python.org/library/imaplib.html

    http://www.janeelix.com/piers/python/imaplib.html

    --
    Grant Edwards grant.b.edwards Yow! Was my SOY LOAF left
    at out in th'RAIN? It tastes
    gmail.com REAL GOOD!!
    Grant Edwards, Jun 15, 2010
    #5
  6. teja

    Tim Golden Guest

    On 15/06/2010 15:10, Grant Edwards wrote:
    > On 2010-06-15, teja<> wrote:
    >
    >> I have a requirement that I want to log-in into a gmail account read
    >> all unread mails, mark them as read and then archive them.
    >> I am using libgmail (version 0.1.11) library to do so, using which I
    >> am able to log-in into a gmail account fetch all unread message and
    >> then read them one by one.
    >> Now my problem is that I am not able to mark the unread mail as read
    >> and archive it.

    >
    > I don't know what libgmail is, but I use IMAP for stuff like that
    > (with Gmail and other servers). Python's imaplib is a bit low-level
    > (and IMAP sucks rather badly as a protocol), so you might want to take
    > a look at imaplib2.
    >
    > http://docs.python.org/library/imaplib.html
    >
    > http://www.janeelix.com/piers/python/imaplib.html


    Or imapclient:

    http://imapclient.freshfoo.com/

    TJG
    Tim Golden, Jun 15, 2010
    #6
  7. On 2010-06-15, Tim Golden <> wrote:
    > On 15/06/2010 15:10, Grant Edwards wrote:
    >> On 2010-06-15, teja<> wrote:
    >>
    >>> I have a requirement that I want to log-in into a gmail account read
    >>> all unread mails, mark them as read and then archive them.
    >>> I am using libgmail (version 0.1.11) library to do so, using which I
    >>> am able to log-in into a gmail account fetch all unread message and
    >>> then read them one by one.
    >>> Now my problem is that I am not able to mark the unread mail as read
    >>> and archive it.

    >>
    >> I don't know what libgmail is, but I use IMAP for stuff like that
    >> (with Gmail and other servers). Python's imaplib is a bit low-level
    >> (and IMAP sucks rather badly as a protocol), so you might want to take
    >> a look at imaplib2.
    >>
    >> http://docs.python.org/library/imaplib.html
    >>
    >> http://www.janeelix.com/piers/python/imaplib.html

    >
    > Or imapclient:
    >
    > http://imapclient.freshfoo.com/


    That looks promising though it's still a bit incomplete (e.g. doesn't
    support 'idle' or 'examine' commands). But, for simple apps it looks
    like a good options.

    --
    Grant Edwards grant.b.edwards Yow! What UNIVERSE is this,
    at please??
    gmail.com
    Grant Edwards, Jun 15, 2010
    #7
  8. teja

    Tim Golden Guest

    On 15/06/2010 16:15, Grant Edwards wrote:
    > On 2010-06-15, Tim Golden<> wrote:
    >> On 15/06/2010 15:10, Grant Edwards wrote:
    >>> On 2010-06-15, teja<> wrote:
    >>>
    >>>> I have a requirement that I want to log-in into a gmail
    >>>> account read all unread mails, mark them as read and then
    >>>> archive them. I am using libgmail (version 0.1.11) library to
    >>>> do so, using which I am able to log-in into a gmail account
    >>>> fetch all unread message and then read them one by one. Now my
    >>>> problem is that I am not able to mark the unread mail as read
    >>>> and archive it.
    >>>
    >>> I don't know what libgmail is, but I use IMAP for stuff like that
    >>> (with Gmail and other servers). Python's imaplib is a bit
    >>> low-level (and IMAP sucks rather badly as a protocol), so you
    >>> might want to take a look at imaplib2.
    >>>
    >>> http://docs.python.org/library/imaplib.html
    >>>
    >>> http://www.janeelix.com/piers/python/imaplib.html

    >>
    >> Or imapclient:
    >>
    >> http://imapclient.freshfoo.com/

    >
    > That looks promising though it's still a bit incomplete (e.g. doesn't
    > support 'idle' or 'examine' commands). But, for simple apps it looks
    > like a good options.


    I think that sums it up fairly well. I've used it for doing various things
    with my personal email (generating whitelists etc.). It is actively
    maintained by its owner Menno Smits and lately by Mark Hammond, but
    presumably only so far as their own requirements demand.

    TJG
    Tim Golden, Jun 15, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ALuPin
    Replies:
    1
    Views:
    504
    Ken Smith
    Oct 8, 2004
  2. phil hunt
    Replies:
    0
    Views:
    308
    phil hunt
    Jan 23, 2004
  3. Replies:
    1
    Views:
    280
    Fredrik Lundh
    Dec 14, 2004
  4. bahoo

    recursively archiving files

    bahoo, Apr 9, 2007, in forum: Python
    Replies:
    1
    Views:
    229
    Larry Bates
    Apr 9, 2007
  5. Guest
    Replies:
    5
    Views:
    317
    Walter Wang [MSFT]
    Aug 27, 2007
Loading...

Share This Page