From: teja on
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.
From: Alf P. Steinbach on
* 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>
From: teja on
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.
From: Thomas Jollans on
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

From: Grant Edwards on
On 2010-06-15, teja <tejaskohok(a)gmail.com> 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!!