[Twisted-Python] SMTP with authentication

Hi, I am new to twisted but google and the Oreilly book have failed me. I am trying to create a client to send to a smtp server. All the examples that I have found only work if your SMTP server does not require authentication. I am guessing that I need to use http://twistedmatrix.com/documents/current/api/twisted.mail.smtp.ESMTPSender... Unfortunately, I can find no examples that use it. I did find something from 2004 http://twistedmatrix.com/pipermail/twisted-python/2004-April/007433.html That is prior to the commit for ESMTPSenderFactory http://twistedmatrix.com/pipermail/twisted-commits/2004-July/011608.html Thanks -- Alvin

On Fri, 3 Nov 2006 16:17:15 -0800, Alvin Wang <alvinwang@gmail.com> wrote:
This should still work.
That is prior to the commit for ESMTPSenderFactory http://twistedmatrix.com/pipermail/twisted-commits/2004-July/011608.html
This changed ESMTPSenderFactory, but authentication was supported before this commit. Give the code at the previous URL a try and post again if you run into problems. Jean-Paul

I modified this example http://twistedmatrix.com/projects/mail/documentation/tutorial/smtpclient/smt... I am still not completely clear on what is happening Thanks for the help Alvin import StringIO from twisted.application import service application = service.Application("SMTP Client Tutorial") from twisted.application import internet from twisted.internet import protocol from twisted.mail import smtp from twisted.mail.imap4 import CramMD5ClientAuthenticator class SMTPTutorialClient(smtp.ESMTPClient): mailFrom = "from@sender.com" mailTo = "to@dest.com" mailData = '''\ Date: Fri, 6 Feb 2006 10:14:39 -0800 From: from@sender.com To: to@dest.com Subject: Tutorate! Hello, how are you, goodbye. ''' def getMailFrom(self): result = self.mailFrom self.mailFrom = None return result def getMailTo(self): return [self.mailTo] def getMailData(self): return StringIO.StringIO(self.mailData) def sentMail(self, code, resp, numOk, addresses, log): print 'Sent', numOk, 'messages' class SMTPClientFactory(protocol.ClientFactory): protocol = SMTPTutorialClient def buildProtocol(self, addr): return self.protocol(secret=None, identity='sender.com') class SMTPClientFactory(protocol.ClientFactory): protocol = SMTPTutorialClient #def __init__(self, *a, **kw): # self.a, self.kw = a, kw def buildProtocol(self, addr): p = smtp.ESMTPClient('smtp_pw') p.factory = self p.requireAuthentication = True p.authenticators = {'CRAM-MD5': CramMD5ClientAuthenticator('smtp_user')} return p smtpClientFactory = SMTPClientFactory() smtpClientService = internet.TCPClient('smtp.1and1.com', 25, smtpClientFactory) smtpClientService.setServiceParent(application) On 11/3/06, Jean-Paul Calderone <exarkun@divmod.com> wrote:

Alvin Wang wrote:
I modified this example
You broke the example. Try this, hopefully it is clearly what is happening: import StringIO from twisted.application import service application = service.Application("SMTP Client Tutorial") from twisted.application import internet from twisted.python import log from twisted.internet import defer, reactor from twisted.mail import smtp mailFrom = "from@example.com" mailTo = ["to@example.com",] mail = StringIO.StringIO("""Date: Fri, 3 Nov 2006 230:14:39 +0000 From: from@example.com To: to@example.com Subject: Tutorate! Hello, how are you, goodbye. """) whenDone = defer.Deferred() def send(r): print "message sent:", r whenDone.addCallbacks(sent, log.err) def done(d): from twisted.internet import reactor reactor.callLater(1, reactor.stop) whenDone.addBoth(done) sender = smtp.ESMTPSenderFactory( 'YOURUSERNAME', 'YOURPASSWORD', mailFrom, mailTo, mail, whenDone, ) smtpClientService = internet.TCPClient('YOURSERVER', 25, sender) smtpClientService.setServiceParent(application)

Thanks!! That worked great. minor typo def sent(r): print "message sent:", r A question. In Phil Mayer's example and Marcin Kasperski's example, it seems like there is a new tcp/ip connection made for each email. Suppose, I want to send multiple emails to the same smtp server. How would that work? Thanks Alvin * *On 11/4/06, Phil Mayers <p.mayers@imperial.ac.uk> wrote:
-- Alvin Wang Gigachannels 650 248 9416

On Fri, 3 Nov 2006 16:17:15 -0800, Alvin Wang <alvinwang@gmail.com> wrote:
This should still work.
That is prior to the commit for ESMTPSenderFactory http://twistedmatrix.com/pipermail/twisted-commits/2004-July/011608.html
This changed ESMTPSenderFactory, but authentication was supported before this commit. Give the code at the previous URL a try and post again if you run into problems. Jean-Paul

I modified this example http://twistedmatrix.com/projects/mail/documentation/tutorial/smtpclient/smt... I am still not completely clear on what is happening Thanks for the help Alvin import StringIO from twisted.application import service application = service.Application("SMTP Client Tutorial") from twisted.application import internet from twisted.internet import protocol from twisted.mail import smtp from twisted.mail.imap4 import CramMD5ClientAuthenticator class SMTPTutorialClient(smtp.ESMTPClient): mailFrom = "from@sender.com" mailTo = "to@dest.com" mailData = '''\ Date: Fri, 6 Feb 2006 10:14:39 -0800 From: from@sender.com To: to@dest.com Subject: Tutorate! Hello, how are you, goodbye. ''' def getMailFrom(self): result = self.mailFrom self.mailFrom = None return result def getMailTo(self): return [self.mailTo] def getMailData(self): return StringIO.StringIO(self.mailData) def sentMail(self, code, resp, numOk, addresses, log): print 'Sent', numOk, 'messages' class SMTPClientFactory(protocol.ClientFactory): protocol = SMTPTutorialClient def buildProtocol(self, addr): return self.protocol(secret=None, identity='sender.com') class SMTPClientFactory(protocol.ClientFactory): protocol = SMTPTutorialClient #def __init__(self, *a, **kw): # self.a, self.kw = a, kw def buildProtocol(self, addr): p = smtp.ESMTPClient('smtp_pw') p.factory = self p.requireAuthentication = True p.authenticators = {'CRAM-MD5': CramMD5ClientAuthenticator('smtp_user')} return p smtpClientFactory = SMTPClientFactory() smtpClientService = internet.TCPClient('smtp.1and1.com', 25, smtpClientFactory) smtpClientService.setServiceParent(application) On 11/3/06, Jean-Paul Calderone <exarkun@divmod.com> wrote:

Alvin Wang wrote:
I modified this example
You broke the example. Try this, hopefully it is clearly what is happening: import StringIO from twisted.application import service application = service.Application("SMTP Client Tutorial") from twisted.application import internet from twisted.python import log from twisted.internet import defer, reactor from twisted.mail import smtp mailFrom = "from@example.com" mailTo = ["to@example.com",] mail = StringIO.StringIO("""Date: Fri, 3 Nov 2006 230:14:39 +0000 From: from@example.com To: to@example.com Subject: Tutorate! Hello, how are you, goodbye. """) whenDone = defer.Deferred() def send(r): print "message sent:", r whenDone.addCallbacks(sent, log.err) def done(d): from twisted.internet import reactor reactor.callLater(1, reactor.stop) whenDone.addBoth(done) sender = smtp.ESMTPSenderFactory( 'YOURUSERNAME', 'YOURPASSWORD', mailFrom, mailTo, mail, whenDone, ) smtpClientService = internet.TCPClient('YOURSERVER', 25, sender) smtpClientService.setServiceParent(application)

Thanks!! That worked great. minor typo def sent(r): print "message sent:", r A question. In Phil Mayer's example and Marcin Kasperski's example, it seems like there is a new tcp/ip connection made for each email. Suppose, I want to send multiple emails to the same smtp server. How would that work? Thanks Alvin * *On 11/4/06, Phil Mayers <p.mayers@imperial.ac.uk> wrote:
-- Alvin Wang Gigachannels 650 248 9416
participants (4)
-
Alvin Wang
-
Jean-Paul Calderone
-
Marcin Kasperski
-
Phil Mayers