[Twisted-Python] SMTP with authentication

Alvin Wang alvinwang at gmail.com
Sat Nov 4 02:48:52 EST 2006


I modified this example
http://twistedmatrix.com/projects/mail/documentation/tutorial/smtpclient/smtpclient-7.tac

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 at sender.com"
    mailTo = "to at dest.com"
    mailData = '''\
Date: Fri, 6 Feb 2006 10:14:39 -0800
From: from at sender.com
To: to at 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 at divmod.com> wrote:
>
> On Fri, 3 Nov 2006 16:17:15 -0800, Alvin Wang <alvinwang at gmail.com> wrote:
> >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.ESMTPSenderFactory.html
> >
> >
> >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
>
> 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
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20061103/adb65a5a/attachment.htm 


More information about the Twisted-Python mailing list