Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Archiving emails in Gmail

Reply
Thread Tools

Archiving emails in Gmail

 
 
teja
Guest
Posts: n/a
 
      06-15-2010
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-15-2010
* 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>
 
Reply With Quote
 
 
 
 
teja
Guest
Posts: n/a
 
      06-15-2010
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=&ac t=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.
 
Reply With Quote
 
Thomas Jollans
Guest
Posts: n/a
 
      06-15-2010
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

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      06-15-2010
On 2010-06-15, teja <(E-Mail Removed)> 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!!
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      06-15-2010
On 15/06/2010 15:10, Grant Edwards wrote:
> On 2010-06-15, teja<(E-Mail Removed)> 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
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      06-15-2010
On 2010-06-15, Tim Golden <(E-Mail Removed)> wrote:
> On 15/06/2010 15:10, Grant Edwards wrote:
>> On 2010-06-15, teja<(E-Mail Removed)> 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
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      06-15-2010
On 15/06/2010 16:15, Grant Edwards wrote:
> On 2010-06-15, Tim Golden<(E-Mail Removed)> wrote:
>> On 15/06/2010 15:10, Grant Edwards wrote:
>>> On 2010-06-15, teja<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does gmail bounce emails from certain nameservers? benzden Computer Support 9 12-16-2006 08:22 PM
Accessing GMail account from GMail Groups Ramkumar Computer Support 2 09-25-2005 09:21 AM
gmail blocks .rar files from non-gmail address kritaly Computer Support 3 08-18-2005 12:00 AM
6 gmail accounts available, post your email here or to gmail@thing.dyndns.org thing NZ Computing 6 12-01-2004 11:40 PM
Microsoft Outlook - Version Five - Saving or Archiving Emails and Newsgroup info bolero Computer Support 2 02-22-2004 03:46 PM



Advertisments