Ticket #6524: mailish.py

File mailish.py, 1.2 KB (added by Jean-Paul Calderone, 6 years ago)

It does not work.

Line 
1import StringIO
2
3from OpenSSL.SSL import SSLv3_METHOD, TLSv1_METHOD
4
5from twisted.mail.smtp import ESMTPSenderFactory
6from twisted.python.usage import Options, UsageError
7from twisted.internet.ssl import ClientContextFactory
8from twisted.internet.defer import Deferred
9from twisted.internet import reactor
10
11
12def sendmail(
13    authenticationUsername, authenticationSecret,
14    fromAddress, toAddress,
15    messageFile,
16    smtpHost="email-smtp.us-east-1.amazonaws.com", smtpPort=587
17    ):
18    contextFactory = ClientContextFactory()
19    contextFactory.method = TLSv1_METHOD
20
21    resultDeferred = Deferred()
22
23    senderFactory = ESMTPSenderFactory(
24        authenticationUsername,
25        authenticationSecret,
26        fromAddress,
27        toAddress,
28        messageFile,
29        resultDeferred,
30        contextFactory=contextFactory,heloFallback=True
31        )
32
33    reactor.connectTCP(smtpHost, smtpPort, senderFactory)
34
35    return resultDeferred, senderFactory
36
37def main(reactor):
38    d, f = sendmail(
39        "user", "password", "alice@example.com", "bob@example.com",
40        StringIO.StringIO("monkeys"))
41    return d
42
43from twisted.internet.task import react
44react(main, [])