problem in using sendmail in multi thread

Jean-Paul Calderone exarkun at divmod.com
Wed May 6 10:10:40 EDT 2009


On Tue, 5 May 2009 22:17:35 -0700 (PDT), gganesh <ganesh.gbg at gmail.com> wrote:
>On May 5, 9:25 pm, Piet van Oostrum <p... at cs.uu.nl> wrote:
>> >>>>> gganesh <ganesh.... at gmail.com> (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 = 'ganeshx... at xxxx.com'
>> >g> # for more mail add';' the another mail id
>> >g> listTo = ['g.... at gmail.com', 'xxjango... at gmail.com',
>> >g> 'xx... at yahoo.co.in']
>> >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> #-----------------------------------------------------
>>
>> --
>> Piet van Oostrum <p... at cs.uu.nl>
>> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
>> Private email: p... at vanoostrum.org
>
>
>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?

Here's another way:

    from twisted.mail.smtp import sendmail
    from twisted.internet import reactor
    from twisted.python.log import err

    MAILSERVER = ...
    listTo = [...]
    FROM = ...
    MSGBODY = ...

    done = sendmail(MAILSERVER, FROM, listTo, MSGBODY)
    done.addErrback(err)
    done.addCallback(lambda ignored: reactor.stop())
    reactor.run()

Jean-Paul



More information about the Python-list mailing list