1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
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
class SMTPTutorialClient(smtp.ESMTPClient):
mailFrom = "tutorial_sender@example.com"
mailTo = "tutorial_recipient@example.net"
mailData = '''\
Date: Fri, 6 Feb 2004 10:14:39 -0800
From: Tutorial Guy <tutorial_sender@example.com>
To: Tutorial Gal <tutorial_recipient@example.net>
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'
from twisted.internet import reactor
reactor.stop()
class SMTPClientFactory(protocol.ClientFactory):
protocol = SMTPTutorialClient
def buildProtocol(self, addr):
return self.protocol(secret=None, identity='example.com')
def getMailExchange(host):
return 'localhost'
smtpClientFactory = SMTPClientFactory()
smtpClientService = internet.TCPClient(
getMailExchange('example.net'), 25, smtpClientFactory)
smtpClientService.setServiceParent(application)
|