problem in using sendmail in multi thread

gganesh ganesh.gbg at gmail.com
Tue May 5 14:29:13 CEST 2009


hi,
I'm a beginner in using Python script
I'm trying to send mails using multi-thread
I wrote



FROM = 'ganeshxxxx at xxxx.com'
# for more mail add';' the another mail id
listTo = ['g.xxx at gmail.com', 'xxjango.py at gmail.com',
'xxxxx at yahoo.co.in']
SUBJECT = 'This is the subject'
MSGBODY = 'This the body of the message '
MAILSERVER = 'mail.xxxx.com'
port = 25
username = 'xxxxx'
password = 'xxxxx'

# trim the strings of any leading or trailing spaces
FROM = FROM.strip()
SUBJECT = SUBJECT.strip()
MSGBODY = MSGBODY.strip()
MAILSERVER = MAILSERVER.strip()
username = username.strip()
password = password.strip()



#Connect to server
print 'Connecting to mail server ', MAILSERVER
try:
	s = smtplib.SMTP(MAILSERVER,port)
	print 'connected'
#s.set_debuglevel(1)
except:
	 print 'ERROR: Unable to connect to mail server', sys.exc_info	()[0]
	 sys.exit(1)

#login to server
if password <> '':
	print 'Logging into mail server'
try:
	s.login(username,password)
except:
	print 'ERROR: Unable to login to mail server', MAILSERVER
	print 'Please recheck your password'
	sys.exit(1)

#--------------------------------------------------
print "Starting Multi Thread Method"


class MyThread(Thread):

    def __init__(self, site, s, FROM, MSGBODY):
        Thread.__init__(self)
        self.site = site
	self.s=s
	self.FROM=FROM
	self.MSGBODY=MSGBODY

    def run(self):
	print "running for %s " %self.site
	s.sendmail(self.FROM, self.site, self.MSGBODY)
	print "Emailed for  site %s" %self.site



a= time.time()
threads = []

for site in listTo:
    T = MyThread(site,s,FROM,MSGBODY)
    threads.append(T)
    T.start()


for i in threads:
    i.join()
s.quit()
s.close()
print "Took %s seconds" %str(time.time()-a)

#-----------------------------------------------------

Error:
There is no problem with mail ids
I'm using python2.5 ,Ubuntu
I got an error like



Traceback (most recent call last):
  File "/usr/lib/python2.5/threading.py", line 486, in
__bootstrap_inner
    self.run()
  File "threadmailmul.py", line 85, in run
    s.sendmail(self.FROM, self.site, self.MSGBODY)
  File "/usr/lib/python2.5/smtplib.py", line 703, in sendmail
    raise SMTPRecipientsRefused(senderrs)
SMTPRecipientsRefused: {'g.xxx at gmail.com': (503, '5.5.1 Error: nested
MAIL command')}

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.5/threading.py", line 486, in
__bootstrap_inner
    self.run()
  File "threadmailmul.py", line 85, in run
    s.sendmail(self.FROM, self.site, self.MSGBODY)
  File "/usr/lib/python2.5/smtplib.py", line 704, in sendmail
    (code,resp) = self.data(msg)
  File "/usr/lib/python2.5/smtplib.py", line 487, in data
    raise SMTPDataError(code,repl)
SMTPDataError: (503, '5.5.1 Error: need MAIL command')

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python2.5/threading.py", line 486, in
__bootstrap_inner
    self.run()
  File "threadmailmul.py", line 85, in run
    s.sendmail(self.FROM, self.site, self.MSGBODY)
  File "/usr/lib/python2.5/smtplib.py", line 692, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
SMTPSenderRefused: (503, '5.5.1 Error: need RCPT command',
'ganeshxxxx at xxx.com')

Exception in thread Thread-4:
Traceback (most recent call last):
  File "/usr/lib/python2.5/threading.py", line 486, in
__bootstrap_inner
    self.run()
  File "threadmailmul.py", line 85, in run
    s.sendmail(self.FROM, self.site, self.MSGBODY)
  File "/usr/lib/python2.5/smtplib.py", line 702, in sendmail
    self.rset()
  File "/usr/lib/python2.5/smtplib.py", line 453, in rset
    return self.docmd("rset")
  File "/usr/lib/python2.5/smtplib.py", line 378, in docmd
    return self.getreply()
  File "/usr/lib/python2.5/smtplib.py", line 355, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed

Traceback (most recent call last):
  File "threadmailmul.py", line 102, in <module>
    i.join()
  File "/usr/lib/python2.5/threading.py", line 594, in join
    self.__block.wait()
  File "/usr/lib/python2.5/threading.py", line 216, in wait
    waiter.acquire()


If someone could point out the (probably silly)
mistake I'm making , I would be very grateful.
Thanks in advance



More information about the Python-list mailing list