source: python/vendor/Python-2.7.6/Doc/includes/email-alternative.py

Last change on this file was 388, checked in by dmik, 11 years ago

python: Update vendor to 2.7.6.

  • Property svn:eol-style set to native
File size: 1.3 KB
Line 
1#!/usr/bin/env python
2
3import smtplib
4
5from email.mime.multipart import MIMEMultipart
6from email.mime.text import MIMEText
7
8# me == my email address
9# you == recipient's email address
10me = "my@email.com"
11you = "your@email.com"
12
13# Create message container - the correct MIME type is multipart/alternative.
14msg = MIMEMultipart('alternative')
15msg['Subject'] = "Link"
16msg['From'] = me
17msg['To'] = you
18
19# Create the body of the message (a plain-text and an HTML version).
20text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
21html = """\
22<html>
23 <head></head>
24 <body>
25 <p>Hi!<br>
26 How are you?<br>
27 Here is the <a href="http://www.python.org">link</a> you wanted.
28 </p>
29 </body>
30</html>
31"""
32
33# Record the MIME types of both parts - text/plain and text/html.
34part1 = MIMEText(text, 'plain')
35part2 = MIMEText(html, 'html')
36
37# Attach parts into message container.
38# According to RFC 2046, the last part of a multipart message, in this case
39# the HTML message, is best and preferred.
40msg.attach(part1)
41msg.attach(part2)
42
43# Send the message via local SMTP server.
44s = smtplib.SMTP('localhost')
45# sendmail function takes 3 arguments: sender's address, recipient's address
46# and message to send - here it is sent as one string.
47s.sendmail(me, you, msg.as_string())
48s.quit()
Note: See TracBrowser for help on using the repository browser.