mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Cleaned up the examples.
This commit is contained in:
parent
5db478fa29
commit
ea66abc6e2
1 changed files with 22 additions and 27 deletions
|
@ -289,15 +289,14 @@ First, let's see how to create and send a simple text message:
|
||||||
# Import smtplib for the actual sending function
|
# Import smtplib for the actual sending function
|
||||||
import smtplib
|
import smtplib
|
||||||
|
|
||||||
# Here are the email pacakge modules we'll need
|
# Import the email modules we'll need
|
||||||
from email import Encoders
|
|
||||||
from email.MIMEText import MIMEText
|
from email.MIMEText import MIMEText
|
||||||
|
|
||||||
# Open a plain text file for reading
|
# Open a plain text file for reading. For this example, assume that
|
||||||
fp = open(textfile)
|
# the text file contains only ASCII characters.
|
||||||
# Create a text/plain message, using Quoted-Printable encoding for non-ASCII
|
fp = open(textfile, 'rb')
|
||||||
# characters.
|
# Create a text/plain message
|
||||||
msg = MIMEText(fp.read(), _encoder=Encoders.encode_quopri)
|
msg = MIMEText(fp.read())
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
# me == the sender's email address
|
# me == the sender's email address
|
||||||
|
@ -306,16 +305,16 @@ msg['Subject'] = 'The contents of %s' % textfile
|
||||||
msg['From'] = me
|
msg['From'] = me
|
||||||
msg['To'] = you
|
msg['To'] = you
|
||||||
|
|
||||||
# Send the message via our own SMTP server. Use msg.as_string() with
|
# Send the message via our own SMTP server, but don't include the
|
||||||
# unixfrom=0 so as not to confuse SMTP.
|
# envelope header.
|
||||||
s = smtplib.SMTP()
|
s = smtplib.SMTP()
|
||||||
s.connect()
|
s.connect()
|
||||||
s.sendmail(me, [you], msg.as_string(0))
|
s.sendmail(me, [you], msg.as_string())
|
||||||
s.close()
|
s.close()
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
Here's an example of how to send a MIME message containing a bunch of
|
Here's an example of how to send a MIME message containing a bunch of
|
||||||
family pictures:
|
family pictures that may be residing in a directory:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
# Import smtplib for the actual sending function
|
# Import smtplib for the actual sending function
|
||||||
|
@ -323,15 +322,15 @@ import smtplib
|
||||||
|
|
||||||
# Here are the email pacakge modules we'll need
|
# Here are the email pacakge modules we'll need
|
||||||
from email.MIMEImage import MIMEImage
|
from email.MIMEImage import MIMEImage
|
||||||
from email.MIMEBase import MIMEBase
|
from email.MIMEMultipart import MIMEMultipart
|
||||||
|
|
||||||
COMMASPACE = ', '
|
COMMASPACE = ', '
|
||||||
|
|
||||||
# Create the container (outer) email message.
|
# Create the container (outer) email message.
|
||||||
|
msg = MIMEMultipart()
|
||||||
|
msg['Subject'] = 'Our family reunion'
|
||||||
# me == the sender's email address
|
# me == the sender's email address
|
||||||
# family = the list of all recipients' email addresses
|
# family = the list of all recipients' email addresses
|
||||||
msg = MIMEBase('multipart', 'mixed')
|
|
||||||
msg['Subject'] = 'Our family reunion'
|
|
||||||
msg['From'] = me
|
msg['From'] = me
|
||||||
msg['To'] = COMMASPACE.join(family)
|
msg['To'] = COMMASPACE.join(family)
|
||||||
msg.preamble = 'Our family reunion'
|
msg.preamble = 'Our family reunion'
|
||||||
|
@ -340,7 +339,7 @@ msg.epilogue = ''
|
||||||
|
|
||||||
# Assume we know that the image files are all in PNG format
|
# Assume we know that the image files are all in PNG format
|
||||||
for file in pngfiles:
|
for file in pngfiles:
|
||||||
# Open the files in binary mode. Let the MIMEIMage class automatically
|
# Open the files in binary mode. Let the MIMEImage class automatically
|
||||||
# guess the specific image type.
|
# guess the specific image type.
|
||||||
fp = open(file, 'rb')
|
fp = open(file, 'rb')
|
||||||
img = MIMEImage(fp.read())
|
img = MIMEImage(fp.read())
|
||||||
|
@ -350,7 +349,7 @@ for file in pngfiles:
|
||||||
# Send the email via our own SMTP server.
|
# Send the email via our own SMTP server.
|
||||||
s = smtplib.SMTP()
|
s = smtplib.SMTP()
|
||||||
s.connect()
|
s.connect()
|
||||||
s.sendmail(me, family, msg.as_string(unixfrom=0))
|
s.sendmail(me, family, msg.as_string())
|
||||||
s.close()
|
s.close()
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
|
@ -394,7 +393,7 @@ import mimetypes
|
||||||
from email import Encoders
|
from email import Encoders
|
||||||
from email.Message import Message
|
from email.Message import Message
|
||||||
from email.MIMEAudio import MIMEAudio
|
from email.MIMEAudio import MIMEAudio
|
||||||
from email.MIMEBase import MIMEBase
|
from email.MIMEMultipart import MIMEMultipart
|
||||||
from email.MIMEImage import MIMEImage
|
from email.MIMEImage import MIMEImage
|
||||||
from email.MIMEText import MIMEText
|
from email.MIMEText import MIMEText
|
||||||
|
|
||||||
|
@ -428,7 +427,7 @@ def main():
|
||||||
recips = args[1:]
|
recips = args[1:]
|
||||||
|
|
||||||
# Create the enclosing (outer) message
|
# Create the enclosing (outer) message
|
||||||
outer = MIMEBase('multipart', 'mixed')
|
outer = MIMEMultipart()
|
||||||
outer['Subject'] = 'Contents of directory %s' % os.path.abspath(dir)
|
outer['Subject'] = 'Contents of directory %s' % os.path.abspath(dir)
|
||||||
outer['To'] = COMMASPACE.join(recips)
|
outer['To'] = COMMASPACE.join(recips)
|
||||||
outer['From'] = sender
|
outer['From'] = sender
|
||||||
|
@ -440,9 +439,9 @@ def main():
|
||||||
path = os.path.join(dir, filename)
|
path = os.path.join(dir, filename)
|
||||||
if not os.path.isfile(path):
|
if not os.path.isfile(path):
|
||||||
continue
|
continue
|
||||||
# Guess the Content-Type: based on the file's extension. Encoding
|
# Guess the content type based on the file's extension. Encoding
|
||||||
# will be ignored, although we should check for simple things like
|
# will be ignored, although we should check for simple things like
|
||||||
# gzip'd or compressed files
|
# gzip'd or compressed files.
|
||||||
ctype, encoding = mimetypes.guess_type(path)
|
ctype, encoding = mimetypes.guess_type(path)
|
||||||
if ctype is None or encoding is not None:
|
if ctype is None or encoding is not None:
|
||||||
# No guess could be made, or the file is encoded (compressed), so
|
# No guess could be made, or the file is encoded (compressed), so
|
||||||
|
@ -465,7 +464,7 @@ def main():
|
||||||
else:
|
else:
|
||||||
fp = open(path, 'rb')
|
fp = open(path, 'rb')
|
||||||
msg = MIMEBase(maintype, subtype)
|
msg = MIMEBase(maintype, subtype)
|
||||||
msg.add_payload(fp.read())
|
msg.set_payload(fp.read())
|
||||||
fp.close()
|
fp.close()
|
||||||
# Encode the payload using Base64
|
# Encode the payload using Base64
|
||||||
Encoders.encode_base64(msg)
|
Encoders.encode_base64(msg)
|
||||||
|
@ -473,14 +472,10 @@ def main():
|
||||||
msg.add_header('Content-Disposition', 'attachment', filename=filename)
|
msg.add_header('Content-Disposition', 'attachment', filename=filename)
|
||||||
outer.attach(msg)
|
outer.attach(msg)
|
||||||
|
|
||||||
fp = open('/tmp/debug.pck', 'w')
|
|
||||||
import cPickle
|
|
||||||
cPickle.dump(outer, fp)
|
|
||||||
fp.close()
|
|
||||||
# Now send the message
|
# Now send the message
|
||||||
s = smtplib.SMTP()
|
s = smtplib.SMTP()
|
||||||
s.connect()
|
s.connect()
|
||||||
s.sendmail(sender, recips, outer.as_string(0))
|
s.sendmail(sender, recips, outer.as_string())
|
||||||
s.close()
|
s.close()
|
||||||
|
|
||||||
|
|
||||||
|
@ -556,7 +551,7 @@ def main():
|
||||||
counter = 1
|
counter = 1
|
||||||
for part in msg.walk():
|
for part in msg.walk():
|
||||||
# multipart/* are just containers
|
# multipart/* are just containers
|
||||||
if part.get_main_type() == 'multipart':
|
if part.get_content_maintype() == 'multipart':
|
||||||
continue
|
continue
|
||||||
# Applications should really sanitize the given filename so that an
|
# Applications should really sanitize the given filename so that an
|
||||||
# email message can't be used to overwrite important files
|
# email message can't be used to overwrite important files
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue