~canonical-livepatch-dependencies/canonical-livepatch-service-dependencies/twisted

« back to all changes in this revision

Viewing changes to docs/mail/tutorial/smtpclient/smtpclient-8.tac

  • Committer: Free Ekanayaka
  • Date: 2016-07-01 12:22:33 UTC
  • Revision ID: free.ekanayaka@canonical.com-20160701122233-nh55w514zwzoz1ip
Tags: upstream-16.2.0
ImportĀ upstreamĀ versionĀ 16.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import StringIO
 
2
 
 
3
from twisted.application import service
 
4
 
 
5
application = service.Application("SMTP Client Tutorial")
 
6
 
 
7
from twisted.application import internet
 
8
from twisted.internet import protocol
 
9
from twisted.mail import smtp
 
10
 
 
11
class SMTPTutorialClient(smtp.ESMTPClient):
 
12
    mailFrom = "tutorial_sender@example.com"
 
13
    mailTo = "tutorial_recipient@example.net"
 
14
    mailData = '''\
 
15
Date: Fri, 6 Feb 2004 10:14:39 -0800
 
16
From: Tutorial Guy <tutorial_sender@example.com>
 
17
To: Tutorial Gal <tutorial_recipient@example.net>
 
18
Subject: Tutorate!
 
19
 
 
20
Hello, how are you, goodbye.
 
21
'''
 
22
 
 
23
    def getMailFrom(self):
 
24
        result = self.mailFrom
 
25
        self.mailFrom = None
 
26
        return result
 
27
 
 
28
    def getMailTo(self):
 
29
        return [self.mailTo]
 
30
 
 
31
    def getMailData(self):
 
32
        return StringIO.StringIO(self.mailData)
 
33
 
 
34
    def sentMail(self, code, resp, numOk, addresses, log):
 
35
        print 'Sent', numOk, 'messages'
 
36
 
 
37
        from twisted.internet import reactor
 
38
        reactor.stop()
 
39
 
 
40
class SMTPClientFactory(protocol.ClientFactory):
 
41
    protocol = SMTPTutorialClient
 
42
 
 
43
    def buildProtocol(self, addr):
 
44
        return self.protocol(secret=None, identity='example.com')
 
45
 
 
46
smtpClientFactory = SMTPClientFactory()
 
47
 
 
48
smtpClientService = internet.TCPClient('localhost', 25, smtpClientFactory)
 
49
smtpClientService.setServiceParent(application)