Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / mail / examples / smtpclient_simple.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Demonstrate sending mail via SMTP.
6 """
7
8 import sys
9 from email.mime.text import MIMEText
10
11 from twisted.python import log
12 from twisted.mail.smtp import sendmail
13 from twisted.internet import reactor
14
15
16 def send(message, subject, sender, recipients, host):
17     """
18     Send email to one or more addresses.
19     """
20     msg = MIMEText(message)
21     msg['Subject'] = subject
22     msg['From'] = sender
23     msg['To'] = ', '.join(recipients)
24
25     dfr = sendmail(host, sender, recipients, msg.as_string())
26     def success(r):
27         reactor.stop()
28     def error(e):
29         print e
30         reactor.stop()
31     dfr.addCallback(success)
32     dfr.addErrback(error)
33
34     reactor.run()
35
36
37 if __name__ == '__main__':
38     msg = 'This is the message body'
39     subject = 'This is the message subject'
40
41     host = 'smtp.example.com'
42     sender = 'sender@example.com'
43     recipients = ['recipient@example.com']
44
45     log.startLogging(sys.stdout)
46     send(msg, subject, sender, recipients, host)
47