Discussion:
copying or moving the mail message using imaplib
Raghul
2005-02-22 04:29:38 UTC
Permalink
Thanks for ur message .How it is possible to copy the messages betwwen
folder ?I can't understand the copy function in python documentation.
Please Can u give me a sample example for this. And I also to copy
using uid but in vain. here is my program.

#!/usr/bin/env python
#Normal Library
import socket
import os
import re
import time
import sys
import getopt
import types
import traceback
import email
import mhlib,popen2


import string, random
import StringIO, rfc822

import email.Parser
from getpass import getpass
from email.Header import Header
from email.Utils import parsedate
from imaplib import IMAP4 as BaseIMAP
from email.Message import Message
from email.Header import Header
# Antispam database lib


class IMAPSession(BaseIMAP):

def __init__(self, server, port, debug=0, do_expunge=False):

BaseIMAP.__init__(self, server, port)



def getsizes(imap, msgs):
uidfetchbatchsize =25
res2=[]

# Python really needs do - while
while 1:
if len(msgs)==0: break
if len(msgs)>uidfetchbatchsize:
msgmore=msgs[uidfetchbatchsize:]
msgs=msgs[:uidfetchbatchsize]
else:
msgmore=[]
msgs=string.join(msgs, ',')
res=imap.uid("FETCH", msgs, "(UID RFC822.SIZE)")
assertok(res, "uid fetch", msgs, "(UID RFC822.SIZE)")
for i in res[1]:
mo=gsre.match(i)
if mo is None:
if verbose: print "getsize Eh?", i
else:
res2.append((mo.group(2), mo.group(1)))
msgs=msgmore
return res2




def run():
import getpass
#name = raw_input("Enter User name : ")
passwd = getpass.getpass ("Enter the password : ")
server ="mail.myhost.com"
port = 143
ss = IMAPSession(server,port)
sd = ss.login ("raghul",passwd)
uidfetchbatchsize =25
print ss.list()
print ss.select('Junk E-mail')
typ, data = ss.search(None,"ALL" )
for num in data[0].split():
typ, data = ss.fetch(num, '(RFC822)')
print 'Message %s\n%s\n' % (num, data[0][1])
print "_____"
dat = ss.uid('copy',num,'INBOX')
print "copied"
print dat
print num
print "_____"
ss.logout()











if __name__ == '__main__':
run()
Raghul
2005-02-22 03:49:11 UTC
Permalink
Hi Friends,

I am doing project in python.I am new to this. My problem
is I want to move the mail message to other folder in the mail using
imaplib (E.g) I want to move a message from the inbox to the Draft
folder in my account using python code. Can any one help me by giving a
sample code to do this?


Thanks in advance
Raghul
2005-02-22 04:56:13 UTC
Permalink
Thanx I got some clear view about this. In the above program I used uid
method to copy thje mail to "Inbox".This shows the out put of my mail
messages and In the copy of messages I got the output
when printing the "dat"

('OK', [None])
('OK', ['2 (UID 16 FLAGS (\\Seen))'])
2

I cannot see the message in the INBOX. Can anyone give me a solution.
Raghul
2005-02-22 04:58:29 UTC
Permalink
Thanx I got some clear view about this. In the above program I used uid
method to copy thje mail to "Inbox".This shows the out put of my mail
messages and In the copy of messages I got the output
when printing the "dat"

('OK', [None])
('OK', ['2 (UID 16 FLAGS (\\Seen))'])
2

I cannot see the message in the INBOX. Can anyone give me a solution.
Tony Meyer
2005-02-22 04:10:50 UTC
Permalink
My problem is I want to move the mail message to other folder in
the mail using imaplib (E.g) I want to move a message from
the inbox to the Draft folder in my account using python
code. Can any one help me by giving a sample code to do this?
IMAP is a terrible terrible protocol. It is not possible to move messages,
whatever language you're using to interact with the IMAP server.

You can copy the message to the new folder (copy() function) and then mark
the old copy as deleted (store() function) but you then either have lots of
/Deleted messages in the original folder, or need to purge/expunge it and
remove any other messages that are /Deleted as well.

The python imaplib is a very thin wrapper over the IMAP protocol. If you
want to use it, then I highly recommend that you do so with a copy of the
IMAP RFC handy (it's really just simple translation from the raw IMAP codes
to Python code).

=Tony.Meyer
Grant Edwards
2005-02-22 04:42:59 UTC
Permalink
Post by Raghul
Thanks for ur message .How it is possible to copy the messages betwwen
folder ?I can't understand the copy function in python documentation.
Maybe you should take a look at the RFC for IMAP4:

http://www.faqs.org/rfcs/rfc2060.html

The UID command is documented in section 6.4.8. It looks
fairly clear to me.
--
Grant Edwards grante Yow! Mr and Mrs PED, can
at I borrow 26.7
visi.com
Loading...