problem in using sendmail in multi thread

G

gganesh

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



FROM = '(e-mail address removed)'
# for more mail add';' the another mail id
listTo = ['(e-mail address removed)', '(e-mail address removed)',
'(e-mail address removed)']
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: {'(e-mail address removed)': (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',
'(e-mail address removed)')

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
 
P

Piet van Oostrum

gganesh said:
g> hi,
g> I'm a beginner in using Python script
g> I'm trying to send mails using multi-thread
g> I wrote


g> FROM = '(e-mail address removed)'
g> # for more mail add';' the another mail id
g> listTo = ['(e-mail address removed)', '(e-mail address removed)',
g> '(e-mail address removed)']
g> SUBJECT = 'This is the subject'
g> MSGBODY = 'This the body of the message '
g> MAILSERVER = 'mail.xxxx.com'
g> port = 25
g> username = 'xxxxx'
g> password = 'xxxxx'
g> # trim the strings of any leading or trailing spaces
g> FROM = FROM.strip()
g> SUBJECT = SUBJECT.strip()
g> MSGBODY = MSGBODY.strip()
g> MAILSERVER = MAILSERVER.strip()
g> username = username.strip()
g> password = password.strip()


g> #Connect to server
g> print 'Connecting to mail server ', MAILSERVER
g> try:
g> s = smtplib.SMTP(MAILSERVER,port)

You can't have a single SMTP connection that's used in multiple threads.
That is what causes the error. Each thread should have its own SMTP
connection. So move this code (above and below) into the run() method.
g> print 'connected'
g> #s.set_debuglevel(1)
g> except:
g> print 'ERROR: Unable to connect to mail server', sys.exc_info ()[0]
g> sys.exit(1)
g> #login to server
g> if password <> '':
g> print 'Logging into mail server'

I think this try except block should be inside the if statement, i.e.
indented 4 spaces.
g> try:
g> s.login(username,password)
g> except:
g> print 'ERROR: Unable to login to mail server', MAILSERVER
g> print 'Please recheck your password'
g> sys.exit(1)
g> #--------------------------------------------------
g> print "Starting Multi Thread Method"

g> class MyThread(Thread):
g> def __init__(self, site, s, FROM, MSGBODY):
g> Thread.__init__(self)
g> self.site = site
g> self.s=s
g> self.FROM=FROM
g> self.MSGBODY=MSGBODY

You give the s (connection) here as a parameter, store it in self.s and
never use that attribute.
g> def run(self):
g> print "running for %s " %self.site
g> s.sendmail(self.FROM, self.site, self.MSGBODY)

Here you use the global s, not self.s

As I said above you should do the SMTP connection setup, including the
login, here, and store the connection either in self.s or in a local
variable s in the method. As you don't use the s in another method, I
think a local variable is better.
g> print "Emailed for site %s" %self.site
g> a= time.time()
g> threads = []
g> for site in listTo:
g> T = MyThread(site,s,FROM,MSGBODY)
g> threads.append(T)
g> T.start()
g> for i in threads:
g> i.join()

Of course the next 2 lines should also be moved to run().
 
G

gganesh

gganesh <[email protected]> (g) wrote:
g> hi,
g> I'm a beginner in using Python script
g> I'm trying to send mails using multi-thread
g> I wrote
g> FROM = '(e-mail address removed)'
g> # for more mail add';' the another mail id
g> listTo = ['(e-mail address removed)', '(e-mail address removed)',
g> '(e-mail address removed)']
g> SUBJECT = 'This is the subject'
g> MSGBODY = 'This the body of the message '
g> MAILSERVER = 'mail.xxxx.com'
g> port = 25
g> username = 'xxxxx'
g> password = 'xxxxx'
g> # trim the strings of any leading or trailing spaces
g> FROM = FROM.strip()
g> SUBJECT = SUBJECT.strip()
g> MSGBODY = MSGBODY.strip()
g> MAILSERVER = MAILSERVER.strip()
g> username = username.strip()
g> password = password.strip()
g> #Connect to server
g> print 'Connecting to mail server ', MAILSERVER
g> try:
g>       s = smtplib.SMTP(MAILSERVER,port)

You can't have a single SMTP connection that's used in multiple threads.
That is what causes the error. Each thread should have its own SMTP
connection. So move this code (above and below) into the run() method.
g>       print 'connected'
g> #s.set_debuglevel(1)
g> except:
g>        print 'ERROR: Unable to connect to mail server', sys.exc_info  ()[0]
g>        sys.exit(1)
g> #login to server
g> if password <> '':
g>       print 'Logging into mail server'

I think this try except block should be inside the if statement, i.e.
indented 4 spaces.
g> try:
g>       s.login(username,password)
g> except:
g>       print 'ERROR: Unable to login to mail server', MAILSERVER
g>       print 'Please recheck your password'
g>       sys.exit(1)
g> #--------------------------------------------------
g> print "Starting Multi Thread Method"
g> class MyThread(Thread):
g>     def __init__(self, site, s, FROM, MSGBODY):
g>         Thread.__init__(self)
g>         self.site = site
g>       self.s=s
g>       self.FROM=FROM
g>       self.MSGBODY=MSGBODY

You give the s (connection) here as a parameter, store it in self.s and
never use that attribute.
g>     def run(self):
g>       print "running for %s " %self.site
g>       s.sendmail(self.FROM, self.site, self.MSGBODY)

Here you use the global s, not self.s

As I said above you should do the SMTP connection setup, including the
login, here, and store the connection either in self.s or in a local
variable s in the method. As you don't use the s in another method, I
think a local variable is better.
g>       print "Emailed for  site %s" %self.site
g> a= time.time()
g> threads = []
g> for site in listTo:
g>     T = MyThread(site,s,FROM,MSGBODY)
g>     threads.append(T)
g>     T.start()
g> for i in threads:
g>     i.join()

Of course the next 2 lines should also be moved to run().
g> s.quit()
g> s.close()
g> print "Took %s seconds" %str(time.time()-a)
g> #-----------------------------------------------------


Thanks Everyone By your guidance the code worked fine
I can send mails in multi threaded environment.
Is this only way to send mails concurrently or any other method
aviable?
regards
Ganesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top