3 from twisted.application import service
5 application = service.Application("SMTP Client Tutorial")
7 from twisted.application import internet
8 from twisted.internet import protocol
9 from twisted.internet import defer
10 from twisted.mail import smtp
12 class SMTPTutorialClient(smtp.ESMTPClient):
13 mailFrom = "tutorial_sender@example.com"
14 mailTo = "tutorial_recipient@example.net"
16 Date: Fri, 6 Feb 2004 10:14:39 -0800
17 From: Tutorial Guy <tutorial_sender@example.com>
18 To: Tutorial Gal <tutorial_recipient@example.net>
21 Hello, how are you, goodbye.
24 def getMailFrom(self):
25 result = self.mailFrom
32 def getMailData(self):
33 return StringIO.StringIO(self.mailData)
35 def sentMail(self, code, resp, numOk, addresses, log):
36 print 'Sent', numOk, 'messages'
38 from twisted.internet import reactor
41 class SMTPClientFactory(protocol.ClientFactory):
42 protocol = SMTPTutorialClient
44 def buildProtocol(self, addr):
45 return self.protocol(secret=None, identity='example.com')
47 def getMailExchange(host):
48 return defer.succeed('localhost')
50 def cbMailExchange(exchange):
51 smtpClientFactory = SMTPClientFactory()
53 smtpClientService = internet.TCPClient(exchange, 25, smtpClientFactory)
54 smtpClientService.setServiceParent(application)
56 getMailExchange('example.net').addCallback(cbMailExchange)