copying or moving the mail message using imaplib

Raghul raghulj at gmail.com
Mon Feb 21 23:29:38 EST 2005


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()




More information about the Python-list mailing list