annotate Doc/includes/email-alternative.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 817d8b28cd6b
children ef48e48f066e fcfe324663a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45108
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
1 #! /usr/bin/python
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
2
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
3 import smtplib
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
4
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
5 from email.mime.multipart import MIMEMultipart
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
6 from email.mime.text import MIMEText
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
7
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
8 # me == my email address
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
9 # you == recipient's email address
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
10 me = "my@email.com"
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
11 you = "your@email.com"
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
12
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
13 # Create message container - the correct MIME type is multipart/alternative.
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
14 msg = MIMEMultipart('alternative')
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
15 msg['Subject'] = "Link"
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
16 msg['From'] = me
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
17 msg['To'] = you
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
18
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
19 # Create the body of the message (a plain-text and an HTML version).
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
20 text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
21 html = """\
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
22 <html>
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
23 <head></head>
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
24 <body>
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
25 <p>Hi!<br>
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
26 How are you?<br>
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
27 Here is the <a href="http://www.python.org">link</a> you wanted.
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
28 </p>
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
29 </body>
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
30 </html>
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
31 """
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
32
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
33 # Record the MIME types of both parts - text/plain and text/html.
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
34 part1 = MIMEText(text, 'plain')
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
35 part2 = MIMEText(html, 'html')
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
36
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
37 # Attach parts into message container.
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
38 # According to RFC 2046, the last part of a multipart message, in this case
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
39 # the HTML message, is best and preferred.
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
40 msg.attach(part1)
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
41 msg.attach(part2)
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
42
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
43 # Send the message via local SMTP server.
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
44 s = smtplib.SMTP('localhost')
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
45 # sendmail function takes 3 arguments: sender's address, recipient's address
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
46 # and message to send - here it is sent as one string.
64091243c07b Add email example how to send a multipart message.
Georg Brandl <georg@python.org>
parents:
diff changeset
47 s.sendmail(me, you, msg.as_string())
53588
817d8b28cd6b Merged revisions 71873-71874,71882,71890,71893,71898-71900,71910,71914-71923,71925-71929,71931-71934,71937 via svnmerge from
Jeroen Ruigrok van der Werven <asmodai@in-nomine.org>
parents: 45108
diff changeset
48 s.quit()